我是perl的新手,并且有一些范围或语法问题.
我正在尝试编写一段代码来读取文件中的行,将它们以特定的分隔符分成两部分,然后将每一半作为键值对存储在散列中.这是我的代码:
#!/usr/bin/perl
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}";
}
Run Code Online (Sandbox Code Playgroud)
当我尝试运行时,我得到:
$ perl test_config_reader.pl --config.txt
"my" variable %config_data masks earlier declaration in same scope at test_email_reader.pl …
Run Code Online (Sandbox Code Playgroud)