Please get out of the habit of using expressions such as "...doesn't work." We need specifics. Doesn't work can mean a lot of things. I maintain that it does work according to defined behavior. It simply doesn't do what you expect, being unfamiliar with defined behavior.

To see an explanation of what behavior is defined for the <diamond> operator, see perlop: IO Operators. There's good info there dealing with how the diamond operator and $_ interact.

As for your question regarding the difference between:

while (<FILE>) {$somevar .= $_} works but while ($somevar .= <FILE>) {} doesn't. (It puts the program in a scary + infinite loop)

The first "works" because it's the right way to do things. The second puts the program into an infinate loop (Thank-you for describing what "doesn't work" means in this case) because 'while()' tests for truth. Once $somevar receives some value from <FILE>, and all future values from <FILE> are simply appended to $somevar, $somevar will always contain a value. Unless your file consists of nothing but zeros, that value will be true, so the loop will always evaluate truth, and thus will never exit. Note that...

while( 1 ) { # do something }

... will never exit either.


Dave


In reply to Re: $_ and filehandles by davido
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.