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


In reply to using strict by NovMonk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.