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

Hi, Can anyone work out why I am getting the message : Global symbol "$name" requires explicit package name at transform.pl line 20. For the code:
#!/usr/bin/perl -w use strict; my $input = "transfer_to_linux.txt"; open (INPUT, "<$input"); while (my $line = <INPUT>){ print "here\n"; if ($line =~ /^%(.{1,100})$/){ print "EEEEEEEEEEEEEEEEEEEEEEEEEEEE\n"; my $name = $&; } my $outfile = "outfile[$name].txt"; open (OUTFILE, "+>>$outfile"); if ($line =~ /^(\d{1,6})\t(\d{3})$/){ my $count = $1; print "COUNT: $count"; my $SPSS_figures = $2; print "SPSS_figures : $SPSS_figures"; my @split = split (//, $SPSS_figures); for (my $x = 0 ; $x <= $count; $x++){ print OUTFILE "$split[1]\t$split[2]\t$split[3]\n"; } } }

Replies are listed 'Best First'.
Re: requires explicit package name
by dws (Chancellor) on Dec 01, 2002 at 18:43 UTC
    Please, before pasting an entire script, take a few minutes to strip the script down to a small script that demonstrates the failure. 90% of the time, doing this will reveal the bug.

    In this case, your problem is here:

    ... { my $name = $&; } my $outfile = "outfile[$name].txt";
    The scope of the first $name ends at the end of the block that it's in. The $name used in the next statement is a different variable. That's clearly not what you intend. The fix is to declare my $name; higher up in the script.

Re: requires explicit package name
by Enlil (Parson) on Dec 01, 2002 at 18:49 UTC
    The variable $name in the line:
    my $outfile = "outfile[$name].txt";
    is not defined, as $name is defined within the if block, and no longer in scope (and hence no longer defined)once it leaves that block. try something like this:
    #!/usr/bin/perl -w use strict; my $input = "transfer_to_linux.txt"; open (INPUT, "<$input"); while (my $line = <INPUT>){ print "here\n"; my $name; if ($line =~ /^%(.{1,100})$/){ print "EEEEEEEEEEEEEEEEEEEEEEEEEEEE\n"; $name = $&; } my $outfile = "outfile[$name].txt"; open (OUTFILE, "+>>$outfile"); if ($line =~ /^(\d{1,6})\t(\d{3})$/){ my $count = $1; print "COUNT: $count"; my $SPSS_figures = $2; print "SPSS_figures : $SPSS_figures"; my @split = split (//, $SPSS_figures); for (my $x = 0 ; $x <= $count; $x++){ print OUTFILE "$split[1]\t$split[2]\t$split[3]\n"; } } }
    Note that I have not checked the rest of the code for errors, just what needed to remove the one error.

    -enlil

Re: requires explicit package name
by converter (Priest) on Dec 01, 2002 at 20:54 UTC

    I sincerely hope that you're using better indentation in your actual source code. If not, pick some kind of indentation style and follow it. Sloppy indentation leads to errors and makes code tedious to read and maintain. Most programmer's editors (take your pick, there are plenty of nodes discussing the merits of various editors) offer convenience features that either enforce or facilitate good indentation.

    Please try to make sure your code is indented consistently when posting examples. It is rude to expect others to spend time sifting through a mess of code just to spot an error that would have been immediately obvious if the code were well-formatted. Writeup Formatting Tips includes some useful information you can use to make the code you post a little more readable.

      Yes. It is indented. For some reason the indentation was lost when I posted the code. I will cut it down next time. I agree that presentation is all important. Thanks for the tips.