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

Is there any way to binmode the input <> without having to actually process the argument list yourself?

Since binmode needs to be done between the open and the first read, there doesn't seem to be a way to do it with <> since that combines the first read with the implicit open.

I tried

  use open IN =>":raw";

but that doesn't work.

Edit: chipmunk 2001-08-02

Replies are listed 'Best First'.
Re: Any way to binmode
by IDStewart (Acolyte) on Aug 02, 2001 at 19:27 UTC
    I'm not aware of any builtin methods for handling this. However, you could use overload and specify your own iteration function.

    Example:

    use strict; use overload "<>" => \&bin_iterate; while (<>) { . . . }
Re: Any way to binmode
by tachyon (Chancellor) on Aug 02, 2001 at 19:32 UTC

    You activate binmode like this binmode FILEHANDLE. Here are some examples:

    open FILE, "<file.bin" or die "Oops perl says $!\n"; binmode FILE; my $buffer; while (read(FILE,$buffer,1024)) { # do stuff to $buffer, a 1kB chunk of binary data } # you can also binmode standard file handles STDIN, STDOUT, STDERR binmode STDOUT; print $binary_data; binmode STDIN; # get binary stuff from STDIN

    Hope this helps

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

(crazyinsomniac)Re: Any way to binmode
by crazyinsomniac (Prior) on Aug 02, 2001 at 19:34 UTC
    stupid golf tricks, look at perlop, and perlopentut, what you want to binmode is ARGV, which is what gets open when you use the diamond operator with nothing in between.

    update I apologize for being a little rude, but like i say below, you can always <>, binmode, seek, do you thing ;)(who'd have thunk it, in the end, i did know a little something)

    UPDATE: well i was almost there, this is kind of stupid, but it'll work as you want it to;

    while(<>) { close ARGV; open ARGV, $ARGV or die $!.' '.$ARGV; binmode ARGV; binmode ARGV; local $/; print $_=<>; close BINMODE; }

     
    ___crazyinsomniac_______________________________________
    Disclaimer: Don't blame. It came from inside the void

    perl -e "$q=$_;map({chr unpack qq;H*;,$_}split(q;;,q*H*));print;$q/$q;"

      I don't think that'll work, as you're free to change @ARGV right up to the first <>. This implies that the ARGV filehandle is not yet open until the first read (which was the point that Thelonius was trying to make).
      what you want to binmode is ARGV
      No, that doesn't work, because the binmode needs to be done before the first read.
        *sigh*, here is an example:
        FF: binmode ARGV; while(<>) { print } continue { if(eof) { close ARGV; goto FF; } }

        upate: i apologize, but you can always:

        FF: binmode ARGV; while(<>) { binmode ARGV; local $/; $a=<>; } continue { if(eof) { close ARGV; goto FF; } }

        UPDATE: well i was almost there, this is kind of stupid, but it'll work as you want it to;

        while(<>) { close ARGV; open ARGV, $ARGV or die $!.' '.$ARGV; binmode ARGV; binmode ARGV; local $/; print $_=<>; close BINMODE; }

         
        ___crazyinsomniac_______________________________________
        Disclaimer: Don't blame. It came from inside the void

        perl -e "$q=$_;map({chr unpack qq;H*;,$_}split(q;;,q*H*));print;$q/$q;"

Re: Any way to binmode
by rob_au (Abbot) on Aug 02, 2001 at 19:20 UTC
    I'm not exactly sure what problems you are encountering, but this is what my usage of binmode would encompass:
     
    unless (open (FH, "filename.ext")) { print STDERR "Cannot open file - ", $!, "\n"; exit 1; }; binmode (FH); while (<FH>) { ... your code follows ... };

     
    You'll note that I have used just the file handle for as binmode argument and not the input channel. As always though, also look at perlfunc:binmode.
     

     
    Ooohhh, Rob no beer function well without!
      I meant <> with the implicit ARGV which opens the series of files given as arguments to your program:
      unshift(@ARGV, '-') unless @ARGV; while ($ARGV = shift) { open(FRED, $ARGV); binmode(FRED); while (<FRED>) { ... # code for each line }

      But what I want to do is:

      use open IN => ':raw'; while (<>) { # code for each line }
      Just lazy, I guess.
Re: Any way to binmode
by John M. Dlugosz (Monsignor) on Aug 03, 2001 at 03:01 UTC
    I think so. Here's an idea: Look up the idiom to get $. to reset with each file in the ARGV list. Put your binmode there. (maybe a dumy read of zero bytes to make it advance & open the next file, then re-binmode it). —John