Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
DrHyde,

> When you do foreach (list_of_stuff), $_ is aliased to each entry in the list in turn. Hence, $_ *is* $u->{loc} and has the same address.

This is exactly my mistake! I believed that each element was copied from the list_of_stuff into $_ one at a time. Good to know.

> I betcha that foreach ($u->{loc}) is a bug

You are write that it's a scalar, and I know it looks like I forgot to do something like @{($u->{loc})}, but it's actually my intended affect. Maybe I'm on the wrong track with this, so I'll post it in case people might have some useful suggestions for me.

I'm using the foreach to do an implicit assignment into $_, and then pattern match on $_. In addition, the foreach loop has the added bonus of allowing flow control (without creating a block)

foreach ($u->{loc}) { if(/n/) { print "option n" } elsif(/m/) { print "option m" } elsif(/w/) { print "option w" } ... }

I'm not sure why I originally did it this way (maybe I read something in one of the O'Reilly books that used this technique?), so I adapted it. Is there a better way? If I don't need the flow control advantage, is it better to simply assign $_ = $u->{loc} prior to my conditional statements?


In reply to Re: Re: $_ scoping issues by swkronenfeld
in thread $_ scoping issues by swkronenfeld

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2023-02-07 08:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I prefer not to run the latest version of Perl because:







    Results (38 votes). Check out past polls.

    Notices?