in reply to Re: Getting data out of __DATA__ and __END__
in thread Getting data out of __DATA__ and __END__

By writing my @ary= as part of the loop, the array will be cleared out on each iteration, with the effect that if your script finishes with a blank line the array will be replaced with split " ","" on the last iteration.

Sorry but as you said earlier I beg to differ. :-)

There is something deeper and funkier happening than what you have stated is going on. I dont claim to know what it is but its definately not the 'if the last line is blank' problem. You might want to take a look at my other reply in this thread for a couple of examples, but try this on for size:

my @ary=split(" ",$_) while (<DATA>); print "@ary"; __DATA__ These are words

Now this DOESNT work. Even though there is no blank line. (use an editor that can show you newlines to make sure.) Now make one tiny change and what happens

my @ary; @ary=split(" ",$_) while (<DATA>); print "@ary"; __DATA__ These are words
Voila it works! So now we KNOW that it isnt anything to do with those darn sneaky hidden blank lines.

Quite frankly until somebody at the wizard/god level tells me this isnt a bug and explains exactly what is going on my money is on the cockroaches...

Yves
--
You are not ready to use symrefs unless you already know why they are bad. -- tadmc (CLPM)

Replies are listed 'Best First'.
Re: Sorry but 'last line is blank' doesnt wash..
by rchiav (Deacon) on Sep 10, 2001 at 21:53 UTC
    Well I'm definately not at the wizard/god level, but I think I might know what's really going on. It's up to others to call it a bug or not.

    I threw this in a debugger to see what exactly happens where. With this code..

    my @ary=split(" ",$_) while (<DATA>); print "@ary"; __DATA__ These are words These are more words
    I set a breakpoint upon reaching the suspect line. When it hits the line, the my variable @ary is created. It's empty. This is done before the evaluation of the expression. Unfortunately the debugger I was using won't show the reiteration of that line. But needless to say, when I stepped off that line, @ary was still empty.

    Next, I used

    my @ary; @ary=split(" ",$_) while (<DATA>); print "@ary"; __DATA__ These are words These are more words
    I set the breakpoint on the my declaration. As before, when I hit that line, @ary was created. When I left the line, it contained the last line of text, each word in an array element.

    What that information, here's my opinion:

    Each time the line in question is accessed, @ary is redeclared (blank). This even happens when the line is executed just for <DATA> to return false. So the last iteration redeclares @ary, there's no more DATA and the program moves on.

    Is that a bug? I'll let someone else answer that :)

    Rich

      ++rchiav!!!

      Thats sounds like a damn good analysis to me.

      Each time the line in question is accessed, @ary is redeclared (blank). This even happens when the line is executed just for <DATA> to return false. So the last iteration redeclares @ary, there's no more DATA and the program moves on.

      I really have to get the debugger frame of mind happening, I almost never use it, preferring copious print statements and the like, but as you have done here it can work wonders.

      You might not consider yourself to be god/wizard level but I think this ones worth a bunch of votes. Good play sir!

      Yves
      --
      You are not ready to use symrefs unless you already know why they are bad. -- tadmc (CLPM)

      Very interesting. Now /me understands. In fact this is a question of how my works rather than how $_ while works (I confess I am a very recent convert to use strict, so this is key for me to get to grips with). I understand both better now, and I must say, at the risk of sounding like a broken record, how inspired I am by how this thread (and many others) worked. It's not just a matter of having the right answers presented clearly, but of reading them in the context of a live problem. These are lessons I shan't forget - I hope it was educational for others too. Muchas gracias to all who posted.

      § George Sherston