Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Error msg "requires explicit package name"

by Theo (Priest)
on Jun 16, 2003 at 22:59 UTC ( [id://266329]=perlquestion: print w/replies, xml ) Need Help??

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

This should be easy (for most Monks). This one of my first attempts at modifying someone else's code. The code creates a hyperlinked list of images and thumbnails in one long column. I'm trying to use a table sturcture to present the list in two columns. The var $counter is to distinguish between odd and even columns so I can properly open & close the table rows.

The first line returns the error message: Global symbol "$counter" requires explicit package name at TnImg.pl line 16.

It gives the same message if the var is preceeded by 'my'.
I'm using strict and the -w.

Can some kind soul point me towards the solution?

$counter = 0; # add link text to each filename foreach my $name (@filenames) { if ($counter % 2){ #odd rows $name = qq{<tr><td><img align="center" valign="center" src="TN/tn_ +$name" width="75" height="42"> &nbsp;&nbsp;<a href="$name">$name</a></td>\n}; }else{ #even rows $name = qq{<td><img align="center" valign="center" src="TN/tn_$nam +e" width="75" height="42"> &nbsp;&nbsp;<a href="$name">$name</a></td></tr>\n}; } $counter++; }

This is my first perl question, so I may have left out something important.

Replies are listed 'Best First'.
Re: Error msg "requires explicit package name"
by sauoq (Abbot) on Jun 16, 2003 at 23:07 UTC
    This is my first perl question, so I may have left out something important.

    You might have told us which is line 16. ;-)

    The variable should be declared with my() before it is used at all. Try declaring it right at the top of your program and seeing if the error goes away... chances are it will. I don't suggest leaving the declaration there though. Declare it where you use it... just be sure it is before you use it.

    -sauoq
    "My two cents aren't worth a dime.";
    
Re: Error msg "requires explicit package name"
by webfiend (Vicar) on Jun 16, 2003 at 23:09 UTC

    Lucky you - you found one of the sloppy behaviors that use strict was made to catch! You might want to change the $counter = 0 line so it reads:

    my $counter = 0;

    See, strict makes sure that you are only using variable names you know about. Every variable must be declared with my, our, or use vars (LIST).

Re: Error msg "requires explicit package name"
by Zaxo (Archbishop) on Jun 16, 2003 at 23:20 UTC

    sauoq and webfiend give you what's usually the best answer, to make the variable lexical with my.

    Other ways are our $counter; which produces a scoped global, use vars '$counter'; which adds its argument list to the current namespace, or always referring to $counter as $main::counter (perhaps) which is the literal cure to the strict complaint, but awkward.

    After Compline,
    Zaxo

      (Drat! I knew I was forgetting things)
      This covers questions from the first 3 replies.
      The first line of code displayed was line 16. The only non-comment lines before line 16 was the following:
      use strict; my @filenames = @ARGV;
      The version of the script before the one I posted did use the 'my' construct and it failed the same way.
Re: Error msg "requires explicit package name"
by sgifford (Prior) on Jun 17, 2003 at 03:52 UTC
    This exact program compiles and runs for me:
    #!/usr/bin/perl -w use strict; my @filenames = @ARGV; my $counter = 0; # add link text to each filename foreach my $name (@filenames) { if ($counter % 2){ #odd rows $name = qq{<tr><td><img align="center" valign="center" src="TN/tn_ +$name" width="75" height="42"> &nbsp;&nbsp;<a href="$name">$name</a></td>\n}; }else{ #even rows $name = qq{<td><img align="center" valign="center" src="TN/tn_$nam +e" width="75" height="42"> &nbsp;&nbsp;<a href="$name">$name</a></td></tr>\n}; } $counter++; }
    Does it for you?
      I can't try it till tomorrow when I get to work, sgifford, but if it does, I'm going to be mightily embarrassed. :-(

      I notice that you did not use 'my' for all instances of $counter. I thought that was part of the requirements for using 'my'. At least I think that's the way I've seen it used. (I think - now I'm not sure of anything)

      One other thing I see now is that the comments '#odd rows' and '#even rows' should have said columns, not rows.

        No, my is a variable declaration and should only be used the first time a lexical variable is referenced. Using two my's in the same scope creates two variables, and with -w will give this warning:
        "my" variable $i masks earlier declaration in same scope
        
        The reason you may see it more than once on the same variable is because the variable is being created new every time. For example:
        for(my $i=0;$i<10;$i++) { print $i,"\n"} for(my $i=9;$i>=0;$i--) { print $i,"\n"}
        uses two completely different $i variables that happen to have the same name, because the scope of $i is only inside the loop.

        Maybe that just confused you more, but hope it's helpful! :-)

Re: Error msg "requires explicit package name"
by dvergin (Monsignor) on Jun 17, 2003 at 07:21 UTC
    Copy-and-paste the entire program up to and including line 16 here for us to examine.

    The error message you report sometimes occurs if there is a punctuation error in a preceeding line.

    ------------------------------------------------------------
    "Perl is a mess and that's good because the
    problem space is also a mess.
    " - Larry Wall

Re: Error msg "requires explicit package name"
by Theo (Priest) on Jun 17, 2003 at 14:30 UTC
    Ok! It works! Using my $counter in line 16 but nowhere else did the trick. In previous versions I had used 'my' with every instance of $counter.
    I was asked to post the whole code, so, even though the problem is resolved, I will - the working version. :-)

    Many thanks to all who responded - especially to sgifford whose working sample 'gave me a clue'.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://266329]
Approved by Enlil
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (9)
As of 2024-03-28 10:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found