For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > June 2005 > setting perlio layers (e.g., utf8) on "magic" ARGV file handle









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author setting perlio layers (e.g., utf8) on "magic" ARGV file handle
Dave

2005-06-11, 3:57 am

I was able to find a few references to similar dilemmas by searching
Google Groups, but none of them fully satisfied my question.

When processing one or more files that have been passed to one's Perl
program from the command line, how does one achieve an effect equivalent
to calling "binmode(FH, ":layer")"? Using "binmode(ARGV)" does not seem
to work for this purpose, because binmode() cannot be called before the
file is open.

As a practical matter, I can use the "C" option on the command line to
workaround this limitation in my present situation, but I would like to
know how to manipulate the layers on the files in @ARGV from within my
program. In addition, the "C" option only controls some of Perl's
Unicode behavior, so it will not always be a viable workaround,
depending on what I'm doing.

I realize that I can of course explicitly open the files myself, but I
can't help but to think that there must exist a more elegant solution
than that.

Dave
Dave

2005-06-11, 3:57 am

It seems that the "open" pragma works for this. I hadn't thought of
that because I've never used it.

However, this raises yet *another* question.

From "perldoc perlopen":

The "open" pragma serves as one of the interfaces
to declare default "layers" (also known as
"disciplines") for all I/O. Any two-argument open
(), readpipe() (aka qx//) and similar operators
found within the lexical scope of this pragma
will use the declared defaults. Three-argument
opens are not affected by this pragma since there
you (can) explicitly specify the layers and are
supposed to know what you are doing.

But further down, it reads:

When open() is given an explicit list of layers
they are appended to the list declared using this
pragma.

So which is it? If you e.g., "use open IO => ':foo'", and then open a
file with the three-argument notation, e.g., "open(FH, '>:bar',
'myfile')", is the layer "bar" appended to "foo" or not? It seems that
the perlopen documentation contradicts itself, but perhaps I am
misinterpreting something.

Also, from my initial experimentation, it seems that you can't stack
multiple layers in the way that you can with "open"; e.g., open(FH,
':foo:bar', 'myfile'). I don't recall any mention of that in the
documentation.

Dave
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com