Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re^2: Last undefines a for loop's itererator?

by pg (Canon)
on Nov 13, 2005 at 02:39 UTC ( #508056=note: print w/ replies, xml ) Need Help??


in reply to Re: Last undefines a for loop's itererator?
in thread Last undefines a for loop's itererator?

Just add one thing, with use strict and warnings, Perl does give error: (I added my in front of $picture, so that the error at line 15 is isolated)

use strict; use warnings; my $match = '1995_Olga_goes_to_Egypt'; my @pictures = qw{1995/1995_Olga_first_time_in_Cyprus/06330004.JPG 1995/1995_Olga_goes_to_Egypt/QD0017004.JPG}; foreach my $picture (sort @pictures) { print "Debug: $picture\n"; if ($picture =~ m/$match/) { print "Matched, $picture\n"; last; } } print "Result: $picture\n";

This gives:

Global symbol "$picture" requires explicit package name at math1.pl li +ne 15. Execution of math1.pl aborted due to compilation errors.


Comment on Re^2: Last undefines a for loop's itererator?
Select or Download Code
Re^3: Last undefines a for loop's itererator?
by Errto (Vicar) on Nov 13, 2005 at 07:22 UTC
    That's not relevant to the issue here. The following strict-compliant program illustrates the issue:
    use strict; use warnings; my $x; for $x (1 .. 3) { last if $x == 2 } print $x;
    which prints
    Use of uninitialized value in print at - line 5.

      On the contrary, it's very relevant. In fact, the original question is I think a great example of the importance of using strict and warnings.

      In the original code, the loop variable $picture was not pre-declared outside the loop using my (as you did for your $x). Had the OP run his code under use strict;, it would have died at compile time, perhaps prompting the OP to take a closer look at his code - exactly the thing I want use strict; to do for me.

      Even with strict-compliant code similiar to yours, the OP would have gotten an informative message, rather then just an empty string, when using use warnings; - namely, the Use of uninitialized value in print message. There is it, staring him in the face - $picture is not initialized.

      It's true that the warning does not explain why there is a problem. But in my experience, understanding exactly where the problem lies, is half-way to solving it.

        The OP’s code is obviously just a snippet, not his entire source. How do you know he didn’t use strict and warnings?

        Makeshifts last the longest.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (7)
As of 2015-07-04 09:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (59 votes), past polls