Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (6)
As of 2024-04-19 09:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found