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

Is there any sensible way to binmode *ARGV? For one-liners and other uses of the diamond operator. merlyn?


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."

Replies are listed 'Best First'.
Re: binmode and one-liners
by Anonymous Monk on Jun 30, 2007 at 11:00 UTC
    perl -Mopen=IN,:bytes

      Thankyou Anonymonk. That's perfect. I've never even noticed the open pragma before.

      I'm assuming that *ARGV is always opened using a 2-arg open rather than a 3-arg that wouldn't be affected.


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
        Yes, that magic hasn't changed
Re: binmode and one-liners
by shmem (Chancellor) on Jun 30, 2007 at 09:11 UTC
    Hmm. What about
    # kids, don't do that at home... perl -pi -e "BEGIN{binmode *ARGV}; s/f\0o\0o/b\0a\0r/g" *.dll

    - did you try that? Don't have windows, so I can't check its effect :-/

    update - but that should do, since from perlop

    The loop
    while (<>) { ... # code for each line }

    is equivalent to the following Perl-like pseudo code:

    unshift(@ARGV, '-') unless @ARGV; while ($ARGV = shift) { open(ARGV, $ARGV); while (<ARGV>) { ... # code for each line } }

    so setting binmode to the IO slot of the *ARGV typeglob is just fine.

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
      I'm thinking that this will try to binmode ARGV before the file handle is even open. So it won't work.

      You'd have to be able to call binmode between the implicit file open, and the reading of the first line. And you just can't interject code there. So I don't think it is possible.

        update: scratch that, I misread your post.

        Well no, BEGIN blocks are executed first?

        qwurx [shmem] ~ > echo foo > foo; echo bar >> foo; qwurx [shmem] ~ > perl -ple 'BEGIN { close ARGV } print' foo foo bar

        Had the filehandle ARGV ever been open before or in the BEGIN block, it is re-opened after the BEGIN block. But I would guess ARGV isn't open in the BEGIN block. Makes more sense to me, anyways. But... perhaps

        perl -ple 'BEGIN{close ARGV; binmode ARGV} s/foo/bar'

        will do.

        --shmem

        _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                      /\_¯/(q    /
        ----------------------------  \__(m.====·.(_("always off the crowd"))."·
        ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}

        You'd have to be able to call binmode between the implicit file open, and the reading of the first line. And you just can't interject code there.
        Yes you can. eof with no parens and no parameters starts the ARGV magic but doesn't read any lines.

        ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

      The problem is, as identified by bart, that binmode affects the open file associate with a filehandle. Besides that a BEGIN block will happen before the file is opened, the diamond operator can also open multiple files, each of which would need to be binmoded.

      I keep thinking about some sort of tieing of *ARGV in a BEGIN block?

      If that could be made to work and stuck in some suitably short named module, the you could do something like:

      perl -margvb -ne" ... * files

      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
Re: binmode and one-liners
by kevinxie (Initiate) on Jan 02, 2023 at 20:25 UTC