Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

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

by Errto (Vicar)
on Nov 13, 2005 at 07:22 UTC ( [id://508079]=note: print w/replies, xml ) Need Help??


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

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.

Replies are listed 'Best First'.
Re^4: Last undefines a for loop's itererator?
by SolidState (Scribe) on Nov 13, 2005 at 12:59 UTC

    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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://508079]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (4)
As of 2024-04-25 10:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found