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)


In reply to Re: Getting data out of __DATA__ and __END__ by demerphq
in thread Getting data out of __DATA__ and __END__ by George_Sherston

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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, details, 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, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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.