Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
... using a previously defined lexical variable as a loop variable ... is a bad practice. ... you may be tempted to use that variable after the loop has finished and it almost certainly will not contain what you expect.

I agree this syntax is Bad Practice and should be avoided, but the variable after the loop has finished will always contain exactly what you expect if you expect it to be absolutely unchanged:

c:\@Work\Perl\monks>perl -wMstrict -le "my $x = 'foo'; print $x; ;; for $x (7 .. 9) { print qq{ $x}; } ;; print $x; " foo 7 8 9 foo
This is because the loop variable, whatever it may be ($x in this case), is "topicalized", i.e., localized upon entry to the loop and restored upon loop termination. This surprising bit of Perl-style for-loop behavior (it's completely absent in the C-style for-loop) is why this syntax [Note 1] should be avoided IMHO. Otherwise, it makes no difference whatsoever AFAIK.

Notes:
  1. ... this syntax ...   By which I mean
        my $x = ...;
        ...
        for $x (...) { ...;  do_something_with($x);  ...; }
    Otherwise, the Perl-style
        for my $x (...) { ...;  do_something_with($x);  ...; }
    is the greatest thing since sliced bread!

Update: Added Note 1.


Give a man a fish:  <%-{-{-{-<


In reply to Re^2: The error says the value is uninitialized, but it works anyway (updated) by AnomalousMonk
in thread The error says the value is uninitialized, but it works anyway by mizducky

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, 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, 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.
  • Log In?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others perusing the Monastery: (6)
    As of 2019-11-17 12:48 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      Strict and warnings: which comes first?



      Results (86 votes). Check out past polls.

      Notices?