in reply to Getting data out of __DATA__ and __END__

Hey George.

At first I thought this was a no brainer and wondered why you posted it. Then I played with it a bit and realized its a bit more subtle than first glance and is a worthy post :-).

As far as I understand modifiers do not create a local scope, so regarding the first if the local() call affects the whole sub, the second if only effects the scope of the if.

sub test { local $\="NEWLINE" if 1; if (1) { local $\; } print "text:"; }
So at first glance this suggests that your code should work. But I believe that the my decl happens over and over. This sounds confused (cause I am a bit confused) but consider if you had multiple lines in your __DATA__ block? Which @ary that was declared should be used? Now you see the angle I am getting at. Personally I suspect that this is a bug, insofar that perls behaviour with regard to modifiers and my() doesnt seem quite right.

Consider the following:

# This works. my @ary; @ary = split (" ",$_) while (<DATA>); print join(" / ", @ary); #As does this my ($str,@ary); @ary = split (" ",$str) while ($str=<DATA>); print join(" / ", @ary); # This doesnt work. (as you pointed out) my @ary = split (" ",$_) while (<DATA>); print join(" / ", @ary); #Nor does this either my (@ary); @ary = split (" ",$str) while (my $str=<DATA>); print join(" / ", @ary);
So it looks like my doesnt work as part of a while modifier which is like the foreach modifier, but unlike foreach NO ERROR is raised. That to me makes it a bug. Incidentally the normal while() does not suffer this problem.

Anyway, if as you say you just want an array of all of the words in your __DATA__ segement then this is probably the smallest way to do it.. (uh oh did I say that? Shoot, now the obfus crew are gunna turn it into a bunch of line noise.. :-)

my @words = map { split " ",$_ } <DATA>; print "@words\n";
Of course you could also mess with $/ as so:
$/=" "; my @words=<DATA>;
But personally I wouldn't, cause you'll just end up cleaning off the newlines and spaces anyways.

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