in reply to binmode and sysread on <>

The binmode and diamond ops are part of perl's buffered IO scheme. Sysread is a lower level system read, similar to the C library's read function. To use sysread as if <> were acting, you need to check if @ARGV has anything in it. If not, sysread from STDIN, otherwise open each filename in @ARGV and sysread until done.

After Compline,
Zaxo

Replies are listed 'Best First'.
Re^2: binmode and sysread on <>
by fizbin (Chaplain) on Sep 10, 2005 at 09:55 UTC
    Close, but not completely accurate.

    binmode is not part of the buffered IO scheme - in fact, the last bit of the perldoc for binmode explicitly says:

    binmode() is not only important for readline() and print() operations, but also when using read(), seek(), sysread(), syswrite() and tell() (see the perlport manpage for more details). See the "$/" and "$\" variables in the perlvar manpage for how to manually set your input and output line-termination sequences.

    Now, as far as the original post is concerned, if you are only ever worried about a single argument in ARGV, this would be sufficient:

    if (! @ARGV) { use open ':raw'; # implicit binmode on each open open(ARGV, '-'); } else { use open ':raw'; open(ARGV, shift @ARGV); # Yeah, it's the two-arg version, # because that's what perl does with <> # see perlopentut } # ... now go do all that sysread stuff.

    If, however, there are possibly multiple files available, you'll have to get more creative:

    my @files = (@ARGV)?@ARGV:qw(-); NEXTFILE: while (@files) { use open ':raw'; open(ARGV, shift @files); # do sysread stuff here # don't forget that an eof here may not be a "real" eof, # but a signal to go to the next file }
    Another possibility is to tie several files together into a single hash which does all the opening in the background. That's left as an exercise for the reader, of for some other response.

    Update: Oops, as Mr. Muskrat pointed out. Fixed.

    --
    @/=map{[/./g]}qw/.h_nJ Xapou cets krht ele_ r_ra/; map{y/X_/\n /;print}map{pop@$_}@/for@/

      In your first snippet, you've reversed what needs to happen.

      if (@ARGV) { # something's in @ARGV so we'll use it use open ':raw'; # implicit binmode on each open open(ARGV, shift @ARGV); # Yeah, it's the two-arg version, # because that's what perl does with <> # see perlopentut } else { # nothing's in @ARGV so we'll use STDIN use open ':raw'; # implicit binmode on each open open(ARGV, '-'); } # ... now go do all that sysread stuff.