Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
Using a foreach loop on an AoH creates an alias of the hash which goes out of scope after the loop.

#!/usr/bin/perl use strict; use warnings; my $recs = [ # short records for brevity {id => 1}, {id => 2}, {id => 3}, ]; my $rec; for $rec (@{$recs}){ # $rec is an alias... if ($rec->{id} == 1){ last; } } # ... which is now out of scope :-( print "$rec->{id}\n"; # Use of uninitialized value... # what I should have done ($rec) = grep {$_->{id} == 1} @{$recs}; print "$rec->{id}\n"; # 1

In reply to Re: What I Most Recently Learned in Perl, But Should Have Already Known by wfsp
in thread What I Most Recently Learned in Perl, But Should Have Already Known by liverpole

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?
    [stevieb]: so, it's -34c today, and some of my water pipes are frozen and one burst. sigh
    [stevieb]: it doesn't seem as though "there's an app for that"
    [ambrus]: burst water pipe sucks
    [Corion]: ambrus: Ugh - yes, that really sucks!
    [stevieb]: good thing is that both sides of the break are completely frozen, so I was able to shut off the water to the building before any water leaks. I told the landlord last year to insulate that specific area better
    [Corion]: stevieb: Our SV overlords don't know what "frozen pipe" means and think it's something like "frozen yogurt"

    How do I use this? | Other CB clients
    Other Users?
    Others surveying the Monastery: (11)
    As of 2016-12-08 18:05 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      On a regular basis, I'm most likely to spy upon:













      Results (144 votes). Check out past polls.