Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Re: opening a file destroys nulling entries in a list?!?!

by sgifford (Prior)
on Aug 07, 2003 at 03:32 UTC ( [id://281724]=note: print w/replies, xml ) Need Help??


in reply to Re: opening a file destroys nulling entries in a list?!?!
in thread opening a file destroys nulling entries in a list?!?!

To elaborate a bit (it took me some experimenting to realize why cfreak is right), inside a map the variable $_ is magic. While each entry in the list is evaluated, $_ is an alias to that entry. Assigning a new value to $_ changes the value of the current list entry. Because you're calling screwed from within map without localizing $_, it's being assigned to each successive line in the file, and then set to undef to indicate end-of-file. When screwed returns, $_ is still set to undef, so all entries in the list are changed to undef.

You can verify this by changing screwed to:

sub screwed { $_ = 'hi'; }
in which case you'll get:
before a
before b
after hi
after hi

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://281724]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (3)
As of 2024-04-19 22:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found