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

Wise ones, is there a way to open with a custom PerlIO layer by default? The Layer I'm using is File::BOM. Of course, I could change all my opens to File::BOM::open_boms, but I don't want to change the (legacy) code, I want to change the environment it runs in.

I understand how to open(FILE,"<:via(Layer)", $path), and what it means. But I want open(FILE,$path) to "default" to :via(Layer).

Lacking wisdom, I tried combinations of

use open ':via(Layer)' use Layer use open 'via(Layer)'
and even
env PERLIO='via(Layer)'

The usual result is

Unknown PerlIO layer class ':via(Layer)' at ...
and my surprise thereat shows that there is something about PerlIO::via that I do not understand.

Replies are listed 'Best First'.
Re: open via
by repellent (Priest) on Oct 30, 2011 at 21:50 UTC
    Something like useopen IN => ':via(File::BOM)' will do.
    use strict; use warnings; use Encode; use File::BOM qw(:all); use open IN => ':via(File::BOM)'; my $enc = "UCS-2"; my $str = "\x{ABCD} a b c d"; my $buf = $enc2bom{$enc} . encode($enc, $str); open(my $FH, "<", \$buf) or die $!; my $read = <$FH>; print($read eq $str ? "match" : "no match", "\n"); __END__ match