EEEEeeeek! Please don't do that...

I'm sorry ambrus, but you have been badly mislead. Even pg has missed the point here. If he had gone on to say that you should only use <> to read from ARGV, he would have been on the mark. The correction you offer is no correction at all. ARGV doesn't maintain its magical properties outside a diamond operator (<>) at all. And, if you aren't using those properties, then you are probably better off not using ARGV.

To see what I mean, put your read() in a while loop, save it in script.pl, dump some data in two test files, and try calling your script as script.pl test1.txt test2.txt. You will find that your script never gets to the data in test2.txt.

Generally speaking, if you find yourself trying to use a useless call like () = eof(); in order to fix something... there is almost certainly a better way. And, in this case, even if your proposed fix did work without introducing the potential bugs that it does, you really wouldn't be buying much for the price you paid with obfuscated code.

Speaking of obfuscation... using 1<<12 instead of 4096 is, uhm, perhaps a bit misguided. I'm not recommending this, but even 2**12 would be better! Using notation like that makes some sense when you are, for example, enumerating bit flags¹ but otherwise it's needlessly confusing.

Going back to the issue at hand, though, you almost never need to reference ARGV explicitly. You can do it in your <> for clarity, of course. And doing an explicit close(ARGV); is another reason. You could pass it to a function that used <>, but that should be avoided because it's just a bug waiting to happen when someone goes and changes the implementation of that function to use read() or something. So, the bottom line is, if you want to use ARGV, just use <> and be happy you don't have to write more code. If you really need read(), then you'll have to do a bit more work.

1. I.e. something like:

use constant F_FOO => 1 << 0; use constant F_BAR => 1 << 1; use constant F_BAZ => 1 << 2; use constant F_QUX => 1 << 3;
And so on... It's okay here because it's obvious what you are doing and why. And the shift contains useful information: the position of the bit associated with the flag.

Update: changed "><" to the intended "<>" in last para.

-sauoq
"My two cents aren't worth a dime.";

In reply to Re: read ARGV ==> read on unopened filehandle by sauoq
in thread read ARGV ==> read on unopened filehandle by ambrus

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.