Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

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

by BrowserUk (Pope)
on Aug 07, 2003 at 03:21 UTC ( #281722=note: print w/ replies, xml ) Need Help??


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

I'm not surprised you are confused:) I get the same behaviour with AS 5.8.

This isn't a bug, and what's more it is a doosy. I'm amazed I didn't recognise this as the normal behaviour of while. this hasn't shown up earlier. Actually I did, after trying to sleep for about 2 hours later. D'oh. It's the heat.

The problem lies with the while statement. Somehow, the aliasing of the input to $_ is getting mixd up with the first element of @_. This can be seen by printing the contents of the array just before the while and again inside it. Inside the loop, the first element of the array has been overwritten by the input from the diamond operator. At the end of the while loop, this then gets set to null. The original contents have been blown away!!

#!perl -sw use strict; $^W=0; my @instances = ('a', 'b', 'c'); sub screwed { my ($in) = @_; open(IN, '<', $in); print "before while: @instances\n"; # while(local $_ = <IN>) { while(<IN>) { chomp; print "after while: @instances\n"; print "file $in contains $_\n"; } close(IN); } print "before $_\n" for @instances; screwed $_ for @instances; print "after $_\n" for @instances; __END__ P:\test>test before a before b before c before while: a b c after while: a1 b c file a contains a1 after while: a2 b c file a contains a2 before while: b c after while: b1 c file b contains b1 after while: b2 c file b contains b2 before while: c after while: c1 file c contains c1 after while: c2 file c contains c2 after while: c3 file c contains c3 after after after

A work around is to explicitly localise $_ in the while as shown commented out above.

I'll raise a perlbug against this.


Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller
If I understand your problem, I can solve it! Of course, the same can be said for you.


Comment on Re: opening a file destroys nulling entries in a list?!?!
Download Code
Re: Re: opening a file destroys nulling entries in a list?!?!
by chromatic (Archbishop) on Aug 07, 2003 at 03:32 UTC
    I'll raise a perlbug against this.

    Please don't. It's documented in perlvar. Search for nasty_break().

Re: Re: opening a file destroys nulling entries in a list?!?!
by matpalm (Initiate) on Aug 07, 2003 at 03:38 UTC
    wowzers! thanks for the super fast responses, i pop out to lunch and look what happens :)

    i also tried mapping <in>, as in...

    map { print "file $in contains $_"; } <IN>;
    and that works ok.

    mat

Re: Re: opening a file destroys nulling entries in a list?!?!
by IlyaM (Parson) on Aug 07, 2003 at 07:15 UTC
    Somehow, the aliasing of the input to $_ is getting mixd up with the first element of @_

    When you do screwed($_) variable $_ gets aliased to $_[0] inside screwed() as subroutine parameters always aliased to elements of @_ array inside the subroutine. So later when while loop modifies $_ it affects $_[0] too.

    Shorter demo:

    $_ = 1; test($_); sub test { $_ = 2; print $_[0], "\n"; } # output of script __END__ 2
    Yet another example why I think while(<>) { ... } considered harmful.

    --
    Ilya Martynov, ilya@iponweb.net
    CTO IPonWEB (UK) Ltd
    Quality Perl Programming and Unix Support UK managed @ offshore prices - http://www.iponweb.net
    Personal website - http://martynov.org

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (9)
As of 2014-12-25 21:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

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





    Results (163 votes), past polls