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 ( #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
Node Status?
node history
Node Type: note [id://281724]
help
Chatterbox?
[Corion]: There are worse ways to enter a website ;)
[Lady_Aleena]: Corion, I'm just hoping Cookies doesn't get too annoying.
[Corion]: Naah, if people got annoyed, they'd tell you ;)

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (8)
As of 2017-04-26 08:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I'm a fool:











    Results (471 votes). Check out past polls.