Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1044865
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T15:53:45+00:00 2026-05-16T15:53:45+00:00

I’ve been learning Perl and whenever I write a non-trivial script I always get

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T15:53:46+00:00Added an answer on May 16, 2026 at 3:53 pm

    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 ($, @, %).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some data like this: 1 2 3 4 5 9 2 6
I have a bunch of posts stored in text files formatted in yaml/textile (from
I am trying to loop through a bunch of documents I have to put
I'm making a simple page using Google Maps API 3. My first. One marker

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.