Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

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.


In reply to Re: opening a file destroys nulling entries in a list?!?! by BrowserUk
in thread opening a file destroys nulling entries in a list?!?! by matpalm

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others scrutinizing the Monastery: (12)
    As of 2014-07-11 13:07 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      When choosing user names for websites, I prefer to use:








      Results (224 votes), past polls