N
Noel Sant
When I run my (test) program below, I get the following error:
Can't use string ("file") as a HASH ref while "strict refs" in use at
D:\Houseke
eping\BackupWithZip\Scripts\hashtest.pl line 28, <INPUT_FILE> line 2.
The camel book says this means only hard references are allowed by "use
strict refs", not symbolic ones. But this is more or less what the camel
book (2nd edition) says to do with arrays of hashes (pp268/9). Why is "file"
a symbolic ref? Where am I going wrong?
TIA
Noel
# Work out how to use an array of hashes with strict refs
#
use strict;
my $input_line; my $folder; my $file;
my @array; my %hash;
my $key; my $i;
# Lines in test.txt have two strings: a folder name and a file name,
# separated by spaces
open INPUT_FILE, "test.txt"
or die "Can't open test.txt\n";
while (!eof INPUT_FILE) {
$folder = "";
$file = "";
chomp($input_line = <INPUT_FILE>);
# Lots of checking of input line for double quotes and spaces,
# but with a simple line ("aaa bbb") ...
($folder, $file) = split (/\s+/, $input_line);
%hash = (
folder => $folder,
file => $file,
);
push @array, %hash;
}
%hash = ();
foreach $i (@array) {
for $key (keys %{$array[$i]}) { # <=== line 28
print "$key name is $array[$i]{$key}\n";
}
}
close INPUT_FILE;
Can't use string ("file") as a HASH ref while "strict refs" in use at
D:\Houseke
eping\BackupWithZip\Scripts\hashtest.pl line 28, <INPUT_FILE> line 2.
The camel book says this means only hard references are allowed by "use
strict refs", not symbolic ones. But this is more or less what the camel
book (2nd edition) says to do with arrays of hashes (pp268/9). Why is "file"
a symbolic ref? Where am I going wrong?
TIA
Noel
# Work out how to use an array of hashes with strict refs
#
use strict;
my $input_line; my $folder; my $file;
my @array; my %hash;
my $key; my $i;
# Lines in test.txt have two strings: a folder name and a file name,
# separated by spaces
open INPUT_FILE, "test.txt"
or die "Can't open test.txt\n";
while (!eof INPUT_FILE) {
$folder = "";
$file = "";
chomp($input_line = <INPUT_FILE>);
# Lots of checking of input line for double quotes and spaces,
# but with a simple line ("aaa bbb") ...
($folder, $file) = split (/\s+/, $input_line);
%hash = (
folder => $folder,
file => $file,
);
push @array, %hash;
}
%hash = ();
foreach $i (@array) {
for $key (keys %{$array[$i]}) { # <=== line 28
print "$key name is $array[$i]{$key}\n";
}
}
close INPUT_FILE;