Perl: Simple .ini parser

I wrote a simple .ini parsing function in Perl.

my $hash_ref = parse_ini("/tmp/config.ini");
sub parse_ini {
	open (my $INI, "<", $_[0]) or return undef;

	my $ret     = {};
	my $section = "_";

	while (my $line = readline($INI)) {
		if ($line =~ /^\[(.+?)\]/) { # Section heading
			$section = $1;
		} elsif ($line =~ /^(\w.*?)\s*=\s*"?(.*?)"?\s*$/) { # Key/Value pair
			$ret->{$section}->{$1} = $2;
		}
	}

	return $ret;
}
Leave A Reply
All content licensed under the Creative Commons License