jamaas has asked for the wisdom of the Perl Monks concerning the following question:

Newbie here again ...


I get the "Global symbol "$counter" requires explicit package name" error with this script, and no idea why! What concept have I missed? Thanks, Jim

#!/usr/bin/perl use strict; use warnings; $counter = 0; use vars qw($counter); open (INPUT, "< data") or die "Couldn't open data file for reading: $!\n"; while(<INPUT>) { my @a = split; # split record into fields $counter++; #increment counter print "Value($counter) = $_\n" for @a; # print out all fields print "\n"; # blank line between "records" } close (INPUT);
jamaas btinternet com

janitored by ybiC: Closed unbalanced <code> tag and removed <br> tags from codeblock

Replies are listed 'Best First'.
Re: perl doesn't like variable
by pg (Canon) on Oct 03, 2004 at 15:17 UTC

    By the way, there was no need to use $counter at all. Perl has a variable $. which tracks the counter of the last accessed file handler. Have more than one file open? okay, then use FOO->input_line_number(). Play with the following code (save this piece of code as test.pl):

    use IO::Handle; open(MYSELF1, "<", "test.pl"); open(MYSELF2, "<", "test.pl"); while (<MYSELF1>) { print "after reading from myself1: $.\n"; if ($. % 2) { <MYSELF2>; print "after reading from myself2: $.\n"; } print "returned values from input_line_number: ("; print MYSELF1->input_line_number(), ", "; print MYSELF2->input_line_number(), ")\n"; } close(MYSELF1); close(MYSELF2);
Re: perl doesn't like variable
by Eimi Metamorphoumai (Deacon) on Oct 03, 2004 at 14:11 UTC
    I think the problem is that you're using $counter before the use vars qw($counter) line. That line basically says "Henceforth, $counter refers to the global variable $MAIN::counter". If you refer to it before that, you get the same error as if you didn't have the use vars at all.
Re: perl doesn't like variable
by Dietz (Curate) on Oct 03, 2004 at 14:12 UTC
    Under use strict, your $counter is initialized before it is declared:
    $counter = 0; use vars qw($counter);

    So you have to declare it (outside of while(<INPUT>)):
    my $counter = 0;

    Note, that you don't need use vars qw($counter); afterwards.
    Otherwise you would declare just another variable called $counter living in the symbol table:
    my $counter = 0; # declare and initialize lexical variable use vars qw($counter); # declare global variable living in symbol tabl +e + + $counter = 0; # initialize again, just to show that $counter in 'use v +ars' is not affected print "\$main::counter: $main::counter\n"; # $counter living in symbol + table: not initialized $main::counter = 1; # initialize $counter in symbol table print "\$main::counter: $main::counter\n"; # prints 1 print "\$counter: $counter\n"; # prints 0

Re: perl doesn't like variable
by jbware (Chaplain) on Oct 03, 2004 at 13:47 UTC
    You need to declare the variable before usage. Try:
    my $counter = 0; # (comment-out added): use vars qw($counter);
    or (From davido)
    use vars qw($counter); $counter = 0;
    -jbWare

    Update: Per davido's comment, I've modified my answer.

      You need to declare the variable. Try:

      my $counter = 0;

      Why? He is properly declaring his variable to be a package global, and that is perfectly legit under strictures. It would be a mistake to try to also declare it to be a lexical variable, as that would create two separate variables.

      His problem is that he initialized $counter before declaring it. Here is his code:

      $counter = 0; use vars qw($counter);

      The proper sequence of events is to declare the variable, and then start using it (or initialize it), like this:

      use vars qw($counter); $counter = 0;

      HTH.


      Dave

        Good catch. Chalk that up to tunnel vision on my part. I saw the usage w/o declaration and didn't even look any further.

        -jbWare
      Thanks!


      I did that and now it does not increment within the loop.
      Do I need to declare it somewhere within the block?
      Thanks
      Jim

      jamaas btinternet com
Re: perl doesn't like variable
by johnnywang (Priest) on Oct 03, 2004 at 18:50 UTC
    You can also use "our":
    our $counter = 0;