tags:

views:

45

answers:

1

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?

+4  A: 

There are three syntax errors in your script:

Global symbol "$keys" requires explicit package name at markov.pl line 12.

You didn't declare $keys, and because of "use strict", that is a fatal error. You probably meant:

my $index = rand(@keys);

The second error:

Global symbol "$prefixes" requires explicit package name at markov.pl line 36.

is the same thing: you meant:

if (@prefixes == 2) {

Finally, in line 30, you're missing a semicolon after:

my @prefixes

This confuses the parser, and causes all the other errors and warnings.

You may want to read the perldata documentation if you're unclear about the use of sigils ($, @, %).

mscha
I thought $keys would be interpreted in scalar context i.e. the number of elements in the list. I get it now though. Thanks.
floogads
Still problems with the other parts though. For example how is $path_name being masked in load_book?
floogads
Edit: Syntax error causes other errors.
floogads
This is a good example of why `use strict` is important. Without it, your program would have run; it just wouldn't have worked properly, and you wouldn't have any clue where the real problems were.
cjm