Leocharre, you're right on in being cautious of the truth of the loop condition. Which is necessary due to the lack of while(<>)-style magic in a mere while($_=shift){}.

Consider how the usual looping variants work or mostly fail, or just ask Perl with a simple, cute and short one-liner:

perl -e 'use Data::Dumper; @a=(1,0,undef,2); print Dumper(@a); print " +1=$_=\n" foreach (@a); @ARGV=@a; print "2=$_=\n" while($_=shift @a); +@a=@ARGV; print "3=$_=\n" while($_=shift @a,defined $_); @a=@ARGV; pr +int "4=$_=\n" while($_=shift);'

PS: ad local: local($_) helps to not hurt the caller (but isn't e.g. necessary in a foreach(@a) scope), while my($_) (since 5.10) helps the current scope to not be hurt by called routines (and signal handlers, etc).

\Peter, who is wondering whether the line-eater did like the taste of his attempted .sig

In reply to Re: using shift and $_ in loop by jakobi
in thread using shift and $_ in loop by leocharre

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.