Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
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


Comment on Re: Re: opening a file destroys nulling entries in a list?!?!
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (8)
As of 2014-12-25 15:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (160 votes), past polls