http://qs321.pair.com?node_id=338761

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

Greetings, Monks.

I have been converted to "use strict, warnings, etc." (Wait, hold your applause....) I've read many of the FAQ's and nodes on the subject, and even found where they buried the initial discussion of "strict" on page 31 of my battered copy of Perl by Example (And they never mention it again in said examples, for all I can tell.)

Here's my remaining confusion-- it looks as if you can declare a variable as you assign it a value, thus:

 my $a = 5;

But, I'm assigning several variables at once to the results of a split, and "strict" is not happy with it until I declare them all at the top of the program thus:

 my ($a, $b, $c);

Here's my script prior to adding the my line declaring everything at the top -- what am I missing, or what should I go back and read a few more times?

#!/usr/bin/perl use strict; use warnings; open (IN, "infile") or die "Can't open input file: $!\n"; open (OUT, ">outfile") or die "Can't create outfile for write: $!\n "; while (<IN>) { if (/^##recstart/) { my ($junk,$recno) = split; $recno =~ s/'//g; } elsif (/^##v/) { my ($junk,$qno,$junk2,$vtext) = split (/ /,$_,4); $qno =~ s/'//g; $vtext =~ s/'//g; } else { print OUT "$recno^$qno^$vtext"; } }

Running this program as it is gives me the error:

Global symbol "$recno" requires explicit package name at verb.pl line +18. Global symbol "$qno" requires explicit package name at verb.pl line 18 +. Global symbol "$vtext" requires explicit package name at verb.pl line +18. Execution of verb.pl aborted due to compilation errors.

I don't know why it likes the first my ($junk, $recno); line but not the second.

Thanks for gentle enlightenment. If I can't get that, the other is fine too ;-).

Pax,

NovMonk