I am new to perl and I have problems with scope or syntax.
I am trying to write a piece of code that reads lines from a file, splits them into a specific separator into two parts, and then saves each half as a pair of key values in a hash. This is my code:
use strict;
use warnings;
my $filename = $ARGV[0];
open(my $fh, '<:encoding(UTF-8)', $filename)
or die "Could not open file '$filename' $!";
my @config_pairs;
while (my $row = <$fh>) {
chomp ($row);
push (@config_pairs, $row);
}
my %config_data;
for my $pair (@config_pairs) {
my ($key, $value) = split(/\s*=\s*/, $pair);
%config_data{$key} = $value;
}
for my $k (%config_data) {
print "$k is %config_data{$k}";
}
When I try to run this, I get:
$ perl test_config_reader.pl --config.txt
"my" variable %config_data masks earlier declaration in same scope at test_email_reader.pl line 22.
syntax error at test_config_reader.pl line 19, near "%config_data{"
Global symbol "$value" requires explicit package name at test_email_reader.pl line 19.
Execution of test_config_reader.pl aborted due to compilation errors.
I'm not sure what I'm doing wrong. Clearly, I do not understand how perl works.
source
share