Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

strict

by snowrider (Pilgrim)
on Feb 02, 2001 at 01:27 UTC ( #55844=perlquestion: print w/ replies, xml ) Need Help??
snowrider has asked for the wisdom of the Perl Monks concerning the following question:

fellow monks, i am currently tring to learn perl so im attempting to do the exercises from the tutorials. here is the code.
#!/opt/perl5/bin/perl -w use strict; while($line=<> and $line ne ".\n"){ push @lines,$line; } foreach(reverse @lines){ print; }
for reasons unknow to me i keep getting these errors
Global symbol "$line" requires explicit package name at day1_test.1.pl + line 5. Global symbol "$line" requires explicit package name at day1_test.1.pl + line 5. Global symbol "@lines" requires explicit package name at day1_test.1.p +l line 6. Global symbol "$line" requires explicit package name at day1_test.1.pl + line 6. Global symbol "@lines" requires explicit package name at day1_test.1.p +l line 8. Execution of day1_test.1.pl aborted due to compilation errors.
does anyone know why this program will not work correctly snowrider

Comment on strict
Select or Download Code
Re: strict
by arturo (Vicar) on Feb 02, 2001 at 01:30 UTC

    That's because you're not declaring your variables, as strict forces you to do.

    You need to add:

    my $line; my @lines;

    BEFORE you try to set them, for your code to work. Alternately, omit the FIRST of the declarations above and change that one line to while (my $line = <> ...)

    Philosophy can be made out of anything. Or less -- Jerry A. Fodor

      i can make the program work when i use
      my $line; my @lines;
      but i cant seem to make
      #!/opt/perl5/bin/perl -w use strict; #my $line; my @lines; while(my $line=<> and $line ne ".\n"){ push @lines,$line; } foreach(reverse @lines){ print; }
      work am i doing something wrong?? i keep getting this error
      Global symbol "$line" requires explicit package name at day1_test.1.pl + line 8. Value of <HANDLE> construct can be "0"; test with defined() at day1_te +st.1.pl line 8. Execution of day1_test.1.pl aborted due to compilation errors.
      snowrider

        Whoops. The problem is that the second way I gave you won't work because the my $line part of it will fail -- and the variable won't be declared -- if you read in a blank line from the file you're looking through. So you could get a loop with the same logic by changing it to:

        while (my $line = <> ) { push @lines, $line unless $line eq ".\n"; }

        HTH

        Philosophy can be made out of anything. Or less -- Jerry A. Fodor

Re: strict
by athomason (Curate) on Feb 02, 2001 at 01:34 UTC
    It's great you're using use strict and have warnings enabled; otherwise perl would never warn you about errors like this. The problem you're encountering is that when strict is enabled, perl requires that you declare your variables ($line and @lines, here) before using them. The simplest way is with the my keyword. Check out that link for more info. Other ways to declare variables include use vars and local, though my is the usual method for situations like yours. A quick fix might go like this:
    #!/opt/perl5/bin/perl -w use strict; my ($line, @lines); while($line=<> and $line ne ".\n"){ push @lines,$line; } foreach(reverse @lines){ print; }
      thanks for your help you just taught me that i can declare multiple variables at once. and that when you use strict you MUST declare your variables snowrider
Re: strict
by dsb (Chaplain) on Feb 02, 2001 at 01:44 UTC
    When you 'use strict' you have to declare your variables with 'my'.
    use strict; my $foo; # declared correctly my @bar; # declared correctly my $foobar = "declared correctly";
    - kel -
      thanks alot kel snowrider
Re: strict
by isotope (Chaplain) on Feb 02, 2001 at 06:20 UTC
    If you want it to be a lot cleaner, try:
    #!/opt/perl5/bin/perl -w use strict; my @lines =(); for(<>) { push(@lines); } for(reverse(@lines)) { print; }
    Or maybe that last for loop can look like:
    while(@lines) { print pop(@lines); }


    --isotope
    http://www.skylab.org/~isotope/

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://55844]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2014-09-03 05:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (35 votes), past polls