Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re^2: binmode and one-liners

by bart (Canon)
on Jun 30, 2007 at 09:15 UTC ( [id://624181]=note: print w/replies, xml ) Need Help??


in reply to Re: binmode and one-liners
in thread binmode and one-liners

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.

Replies are listed 'Best First'.
Re^3: binmode and one-liners
by shmem (Chancellor) on Jun 30, 2007 at 09:36 UTC

    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}
Re^3: binmode and one-liners
by diotalevi (Canon) on Jul 01, 2007 at 15:27 UTC

    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.

    ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

      After my previous reply, I found the time and facilities and ran a quick test and found two mistakes in your suggestion. First, it is eof() (with parens) not eof without parens that triggers ARGV magic.

      Second, as I suspected, eof() does read ahead:

      % perl -del existingFile.txt DB<1> x eof() 0 '' DB<2> x sysseek(*ARGV,0,1) 0 4096 DB<3>

      So, if on a platform where binmode mattered, binmode() would not have been in effect for the first 4096 (resulting) bytes of the file. (But note that you don't need such a platform to perform this test.) Part of the problem here is that the impact of binmode is handled by the underlying I/O layers, not by Perl, so the bytes pre-read have already passed through the layer where binmode would be applied (but wasn't).

      If there is some way to get eof without parens to trigger an ARGV-magic implicit open, then I couldn't figure it out and so didn't test it. So please describe it in more detail.

      - tye        

      I'll believe that when I see test code to demonstrate it. My recollection is that eof reads ahead to see whether you are at EOF rather than just returning whether Perl already knew that you were at EOF.

      I can see eof allowing code to run just before an implicit close / open, but I'd be surprised to find eof not causing an implicit read() after it caused an implicit open().

      Sorry, I'm not in a position to test it myself at the moment.

      - tye        

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://624181]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2024-04-19 16:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found