in reply to Re: read ARGV ==> read on unopened filehandle
in thread read ARGV ==> read on unopened filehandle

You are saying that I shouldn't use read on ARGV. I think you are right here.

I was using it as a short notation to avoid an explicit open. This was a one-liner. It didn't matter if it could handle only one file, as the one-liner didn't even have a loop: it called read once only. However, it's quite stupid to do this, as it's much easier to read from STDIN instead and use shell redirection. In a script (not a one-liner), it's of course better to use an explicit open.

As for using 4096, I disagree with you. It doesn't really matter whether I use 2**12 or 1 << 12, they mean the same for me. (Except that 1 << 12 is a bit more verbose as it often needs to be parenthisized.) However, there's no way I'll use 4096, even in a constant definition like sub HEADER_SIZE { 4096 } instead of these. The reason is simple: once I wrote a script where I had to read a string of 256 records of 32 bytes each. I wrote 8092 instead of 8192, and I had a very bad time searching for the bug. So, I've learnt that if I want to read four kilobytes, I write 4*1024 or 4<<10 or 1<<12, but never calculate 4096 in my head.

Replies are listed 'Best First'.
Re^3: read ARGV ==> read on unopened filehandle
by eric256 (Parson) on Sep 19, 2005 at 16:07 UTC

    As a shorter notation I don't think you gained much unless I'm missing someing, which is entirly possible. From here it looks like you only saved 1 char give or take some spacing.

    () = eof(); read ARGV, $b, 1<<12 or die "cannot read ARGV: $!"; open(FH,shift) or die "cannot read file: $!";read FH, $b, 1<<12;

    ___________
    Eric Hodges
      open(FH,shift) or die "cannot read file: $!";read FH, $b, 1<<12;

      Why did you choose such an enormously long name for your filehandle? ;-)

      If you are really going for shortness though, you can still skip read() and set $/ to a reference. Granted that error handling isn't equivalent, but the following should do for use in a one-liner:

      $/=\4096;$b=<>;

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