I think your misunder standing was that you belived that the items on the right hand side of the = coresponded with the items on the left hand side. In Perl if the left hand side looks like a list or an array (any time there is an array or parentheses) all arguments on the right hand side are flatend into a single list, and asigned to the left hand side from left to right with a scalar (or undef) consuming one item, and the first array or hash consuming all remaining items.

my (undef, $salt, @post, @time) = (<FILE>, <FILE>, (), ());

So on this line your using <FILE> in array context, the entire contents of the file will be read in and returned as a list. Then the first line is discarded, the second line placed in $salt and the rest of the lines in @post, because @post consumes all the lines there is no data to place in @time so @time is left empty. After that point there is no more data to read so your while loop's conditional evaluates to false on the first test and the split is never executed. The print statment displays the 3 lines contained in @post as would be expected.

A solution would be to not give the first 2 uses of <FILE> array context, either by directly asigning to a scalar, or by forcing scalar context with the scalar() function.

<FILE>; #discard a line (one line is read in void context) my $salt = <FILE>; #provide scalar context so only one line is read my (@post, @time); ($post[$#post + 1], $time[$#time + 1]) = split(/;/) while (<FILE>); print "@post\n@time\n";
my (undef, $salt, @post, @time) = (scalar(<FILE>), scalar(<FILE>)); #f +orce scalar context. ($post[$#post + 1], $time[$#time + 1]) = split(/;/) while (<FILE>); print "@post\n@time\n";

I find the first better as long as you comment that you want to discard the line when <FILE> is on a line by it self.


In reply to Re^5: $_ and filehandles by Ven'Tatsu
in thread $_ and filehandles by OnionKnight

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.