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

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

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.


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

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

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


Site Timeline