in reply to binmode for @ARGV

I checked back to see if anyone had yet looked up how to do this. Since noone had, I hit binmode which pointed me to perldoc open which suggests:

use open IN=>":raw", OUT=>":raw";
but it doesn't work because the calls to open() that are done by <> don't occur in the lexical context of the use open and so are not affected by it? That sucks.

This problem (binmode with <>) has been known for years and I recall solutions being discussed years ago so I was quite disappointed to not find the solution. Perhaps open.pm was the solution and then it was "fixed" to be lexically scoped which also made it useless for its original purpose? (probably not)

Even

*CORE::GLOBAL::open= sub { my( $fh, $arg, @args )= @_; my $ret= CORE::open( $fh, $arg, @args ); binmode( $fh ); return $ret; };
doesn't work because <> calls some C code directly rather than acting like call to open from Perl code. It calls Perl_do_open() which calls Perl_do_open9() which does check the flags set by open.pm. But it only checks the flags if it was called via an OP_OPEN opcode.

So changing

if (PL_op && PL_op->op_type == OP_OPEN) { /* set up disciplines */ U8 flags = PL_op->op_private; in_raw = (flags & OPpOPEN_IN_RAW);
to start with just
if (PL_op) {
might solve the problem?

I guess I'll download modern Perl source and send such a patch to p5p and see how they take it.

                - tye

Replies are listed 'Best First'.
Re: Re: binmode for @ARGV (open.pm)
by BrowserUk (Patriarch) on Jan 30, 2003 at 22:01 UTC

    Thankyou for looking into it Tye. Some of that is still over my head (what's new:), but it's interesting to see that this is one of those cases where simplifying the code may improve it's flexibility. Good luck with the patch. I have encountered several situations in which this 'fix' would have simplified my code when writing filters for binary files.

    If I ever work out how to get MinGW to build the sources, I guess I could add the patch to my own copy. First things first though:)


    Examine what is said, not who speaks.

    The 7th Rule of perl club is -- pearl clubs are easily damaged. Use a diamond club instead.

Re: Re: binmode for @ARGV (open.pm)
by bsb (Priest) on May 24, 2004 at 07:56 UTC
    Does the PERLIO environment variables (described in perlrun) help in this case?

    In Perl 5.6 and some books the :raw layer (previously sometimes also referred to as a "discipline") is documented as the inverse of the :crlf layer. That is no longer the case - other layers which would alter binary nature of the stream are also disabled. If you want UNIX line endings on a platform that normally does CRLF translation, but still want UTF-8 or encoding defaults the appropriate thing to do is to add :perlio to PERLIO environment variable.

    Running "set PERLIO=:perlio" seemed to work under XP

    (Reponse well after original post)