binmode in 1-liner?

L

Larry

I am a big fan of Perl 1-liners and I even use them on Windows. One
thing that bugs me though is that there doesn't seem to be an easy way
to process binary files with 1 liners. It would be nice if there were
a command-line switch to turn on "binmode". As it is, I am forced to
use

perl -0777 -ne "BEGIN { binmode STDIN; binmode STDOUT } ... "

which is a lot of typing for a 1-liner.
 
D

Dr.Ruud

Larry:
I am a big fan of Perl 1-liners and I even use them on Windows. One
thing that bugs me though is that there doesn't seem to be an easy way
to process binary files with 1 liners. It would be nice if there were
a command-line switch to turn on "binmode". As it is, I am forced to
use

perl -0777 -ne "BEGIN { binmode STDIN; binmode STDOUT } ... "

which is a lot of typing for a 1-liner.

Use a wrapper.

Maybe call it 'perlb.pl' (assumes you have added 'pl' to the executable
extensions).
 
M

Mark Clements

Larry said:
I am a big fan of Perl 1-liners and I even use them on Windows. One
thing that bugs me though is that there doesn't seem to be an easy way
to process binary files with 1 liners. It would be nice if there were
a command-line switch to turn on "binmode". As it is, I am forced to
use

perl -0777 -ne "BEGIN { binmode STDIN; binmode STDOUT } ... "

which is a lot of typing for a 1-liner.

You can set the environment variable PERLIO to accomplish this. see

perldoc perlrun

for details.

Not quite the same, of course.

The other alternative would be to create a module that does the
necessary in its BEGIN block. You could then

perl -MMyBinModeModule -le "...........

though probably choosing a shorter name for the module.

Mark
 
A

Anno Siegel

Mark Clements said:
You can set the environment variable PERLIO to accomplish this. see

perldoc perlrun

for details.

Not quite the same, of course.

The other alternative would be to create a module that does the
necessary in its BEGIN block. You could then

If it's a module, the BEGIN {} doesn't make a difference. If it's
loaded via "use", it will be run at compile time (relative to the
calling script) anyway. If it's via "require", BEGIN {} in the module
doesn't help any.

Anno
 
M

Mark Clements

Anno said:
If it's a module, the BEGIN {} doesn't make a difference. If it's
loaded via "use", it will be run at compile time (relative to the
calling script) anyway. If it's via "require", BEGIN {} in the module
doesn't help any.

OK: I should have thought of this. Thanks for the correction.

Mark
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top