Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

How do I set first set binmode on <> and then read from <> with sysread?

Thanks,
Dan

Replies are listed 'Best First'.
Re: binmode and sysread on <>
by Zaxo (Archbishop) on Sep 10, 2005 at 07:00 UTC

    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

      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.
Re: binmode and sysread on <> (open.pm)
by tye (Sage) on Sep 10, 2005 at 17:28 UTC

    The open.pm pragma is what should allow you to set binmode for the "magic" @ARGV-based <>, but it doesn't. A patch to make this happen would be pretty simple if <> were fixed to not be an exploit source.

    However, p5p insists that the input operator, <>, must be able to react to "perl -ne *" by opening output handles to create files (if there are any files whose names start with ">") or run arbitrary commands (if there are any files whose names start with "|").

    This stopped my working on the patch to fix open.pm to be useful for <>.

    - tye