I've been learning Perl and whenever I write a non-trivial script I always get this error message. I always think I have a good understanding of it, but I suppose I don't. Here is a sloppy markov chain example (not tested) with the errors below.
The
#!/usr/bin/perl -w
use strict;
sub croak { die "$0: @_: $!\n"; }
sub output {
my %chains = shift;
my @keys = keys %chains;
my $index = rand($keys);
my $key = $keys[$index];
my $out_buf = $key;
for (my $i = 0; $i < 100; ++$i) {
my $aref = $chains{$key};
my $word = @$aref[rand($aref)];
$out_buf .= " $word";
$key =~ s/.+ //;
$key .= " $word";
}
print $out_buf, "\n";
}
sub get_chains {
my %chains;
my @prefixes
while (my $line = <FILE>) {
my @words = split " ", $line;
foreach my $word (@words) {
if ($prefixes == 2) {
my $key = join " ", @prefixes;
my $arr_ref = $chains{$key};
push(@$arr_ref, $word);
shift @prefixes;
}
push(@prefixes, $word);
}
}
return %chains;
}
sub load_book {
my $path_name = shift @ARGV;
open(FILE, $path_name) || croak "File not found.\n";
}
load_book;
my %chains = get_chains;
output %chains;
----ERRORS----
"my" variable $line masks earlier declaration in same statement at markov.pl line 33.
"my" variable $path_name masks earlier declaration in same scope at markov.pl line 55.
Global symbol "$keys" requires explicit package name at markov.pl line 12.
syntax error at markov.pl line 32, near ") {"
Global symbol "$prefixes" requires explicit package name at markov.pl line 36.
Global symbol "%chains" requires explicit package name at markov.pl line 48.
syntax error at markov.pl line 49, near "}"
syntax error at markov.pl line 56, near "}"
Execution of markov.pl aborted due to compilation errors.
What mistake(s) am I making?