Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
A wise monk once advised me to avoid assigning directly to $_.

He counseled me that Perl uses $_ in a variety of ways that fledgelings such as myself may not understand. When through youthful exuberance we blow away the existing values stored in $_ we create problems for ourselves later.

For instance, what if the code you are writing is inside a loop which already uses $_ for something else? For instance:

while (<MYFILEHANDLE>){ ...your code here... print; }

All of a sudden your first way of doing it, which assigns to $_, breaks the existing program so that it no longer prints out all of the lines in a file.

"Does the second loop give you nasty C flashbacks?"

I would argue that we are programming in Perl. We are not programming in "Not-C". The second method is perfectly proper Perl and is preferable for those of us who are concerned about modifying $_ in a way which may interfere with other parts of a program.

Update: japhy informs me below that for(split ' '){} inside a  while (<FH>){} creates a local copy of $_ and does not use the same one that the while() uses. Hence, the use of the first method does not break the program I posted.

Since posting the above I have also noticed that LW, Merlyn et al in the Camel book sometimes assign directly to $_. For example, on page 103 of the second edition they say:

while (<>) { chomp; if (s/\\$//) { $_ .= <>; redo; } # now process $_ }

One ought not to be more Catholic than the Pope. If LW and Merlyn think that assigning to $_ is safe and proper Perl style then so be it. </code>


In reply to Re: What would you do? by sierrathedog04
in thread What would you do? by MeowChow

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?Last hourOther CB clients
Other Users?
Others examining the Monastery: (2)
As of 2024-04-20 03:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found