Making cosmetic changes in the presence of magic is rarely advisable. So I suppose I shouldn't have been surprised when changing <ARGV> to <> broke a working script.

The context here is the dumping of all input to a temporary file:

my ($fh, $nam) = File::Temp::tempfile; print $fh <ARGV>;

This works. But changing the second line to

print $fh <>;

causes it to fail with the message "syntax error ... near <>".

This puzzled me for a long time. Surely <> and <ARGV> are synonyms? Why would one work and the other fail?

For about five minutes after this dawned on me, I wished that Perl had just a little more magic in it, so that my change wouldn't have broken anything. But then I realised that adding more magic would just make the cases where it doesn't DWIM even more unexpected and confusing. I note in passing that Perl 6 will use a less ambiguous syntax that won't break this way. Which I like, even though it looks a bit ugly to me at the moment. I want the experience of programming to be magical, not the syntax.


In reply to <> and <ARGV> are the same thing, except when they aren't by Porculus

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.