my %action = ( LOCATION => sub { my( $dir ) = @_; $dir =~ s{^\.\.}{}; print $dir; }, ); ... while ... /^ # start of line \s* # optional padding ( [^:]+ ): # $1 key, not ':' \s* ([^\r\n]*) # $2 value, not line end [\r\n]* # end of line /xm and do { my( $key, $value ) = @_; my $action = $parser{ $key } || $parser{default}; $action->($value); };