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

I checked my script using "perl -w -c" and get a "Global symbol "..." requires explicit package name" error. I am using strict and understand I need to explicitly define each variable. However, when I assign either "my" or "local" to the variables, I still get the same message. The error refers to @allquestions, $anslength, @allanswers on lines 22, 27, 28, 34, 45, 46. Below is the script. Thanks.
#!/usr/bin/perl #c:\sambar50\perl\perl.exe use CGI qw(:standard); use strict; use warnings; print header; local $/ = "~\n"; # *** CHANGES INPUT RECORD SEPARATOR TO ~line brea +k print start_html (-title=>'quiz', -BGCOLOR=>'beige'); print start_form (-method=>'POST', -action=>"http://localhost/cgi-bin/ +gradequiz.cgi"); my $file = param('file'); open (FILE, $file) || die "Cannot open $file: $!"; while (<FILE>) { # ** READS EACH LINE OF TEXT FROM FILE chomp; # ** BY DEFAULT, CHOMPS OFF $/ FROM EACH LINE my ($type, $value) = split (/:/, $_); # ***** SPLITS EACH LINE BY +TYPE AND CONTENT if ($type =~ m/q/i) { # ** CHECKS FOR QUESTION LINE & PRINTS QUESTI +ON print "<B>$value</B><BR>"; local @allquestions = (@allquestions, $value); } if ($type =~ m/a/i) { # ** CHECKS FOR ANSWER LINE my @answers = split (/,/, $value); # ** SPLITS ANSWER LINE IN +TO ARRAY local $anslength = @answers; # ** LENGTH OF @ANSWERS local @allanswers = (@allanswers, $value); } #** CHECKS FOR SELECTION LINE AND if ($type =~ m/^s/) { # ** TYPE OF ANSWER BOX my %selections = split (/,/, $value); my @options = keys (%selections); if ($anslength > 1) { # ** DETERMINES TO USE RADIO OR CHECKB +OX print checkbox_group(-name=>$type, -values=>\@options, -linebre +ak=>'true', -labels=>\%selections); }else { print "<BR>"; print radio_group(-name=>$type, -values=>\@options, -linebre +ak=>'true', -labels=>\%selections, -default=>'-'); } print "<BR><HR><BR>"; } # END IF STATEMENT } # ** END WHILE STATEMENT close (FILE); print hidden(-name=>"answers", -default=>\@allanswers); print hidden(-name=>"questions", -default=>\@allquestions); print "<CENTER>"; print submit (-value=>'Grade') . "&nbsp; &nbsp;"; print reset (-value=>'Reset answers'); print "</CENTER>"; print end_form; print end_html;

Replies are listed 'Best First'.
Re: explicit package name
by Juerd (Abbot) on Dec 17, 2001 at 20:56 UTC
    "local" is not enough. You need "our" or "use vars"... Read about strict and find out. :)
    2;0 juerd@ouranos:~$ perl -e'undef christmas' Segmentation fault 2;139 juerd@ouranos:~$
      my is also an option.

      In fact the option of our is one I don't like. You can read Why is 'our' good? for a full explanation of what I don't like about it. (Of course a lot of people disagree with me on this one. *shrug*)

Re: explicit package name
by Hofmator (Curate) on Dec 17, 2001 at 22:36 UTC

    A small remark:

    @allanswers = (@allanswers, $value); # can be replaced by push @allanswers, $value;

    This is much more efficient, shorter to write and better to read :) (see push)

    -- Hofmator

Re: explicit package name
by Masem (Monsignor) on Dec 17, 2001 at 22:51 UTC
    When you use my in a block, that variable only exists inside the code block and disappears when you exit it. In the case of your @allanswers and @allquestion variables, you try to use these outside the codeblocks they're mean for. When you define something locally, you only override it's value for the extent of the block, and it reverts back to the original value after it. Thus, all your my and local definitions fail to exist after the code blocks they are in.

    What you probably want to do is to do:

    my (@allquestions, @allanswers);
    before your while (<FILE>) loop; you can then remove any local or my declarations inside that loop.

    -----------------------------------------------------
    Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
    "I can see my house from here!"
    It's not what you know, but knowing how to find it if you don't know that's important