Actually I was about to answer just that, but then I tested it - and it turned out I was wrong.

Consider this:

$ hexdump -C foo 00000000 61 0a 30 |a.0| 00000003 $ perl -wle 'print "read line" while (my $x = <>)' foo read line read line $

I have a file foo here with an 'a', a newline, and a '0' - no trailing newline.

I'd expect just one line of output, because 0 is false. But why am I getting two lines? That doesn't seem to be logical to me.

So I modified my test scrpt a bit:

$ perl -MDevel::Peek -wle 'while (my $x = <>){ print "read line"; pri +nt "and its true" if $x }' foo read line and its true read line

So, the second line evaluates to true in the while-condition, but not in the following if-condition. Is there some kind of special magic? If yes, where is it documented?

Update ok, got it. Thank you all. I didn't realize it works for arbitrary variable names, and thought that $_ was special cased.


In reply to Re^2: Doubt on defined-ness by moritz
in thread Doubt on defined-ness by waldner

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.