Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Can't clone STD handle with :via layer

by zdm (Beadle)
on Apr 08, 2014 at 12:43 UTC ( #1081498=perlquestion: print w/replies, xml ) Need Help??
zdm has asked for the wisdom of the Perl Monks concerning the following question:

#!/usr/bin/env perl use strict; use warnings; use PerlIO::via::QuotedPrint; # or any other PerlIO::via:: package fro +m CPAN binmode STDOUT, q{:via(QuotedPrint)}; open my $fh, q{>&}, q{STDOUT}; 1; __END__

This code runs with error:

No package specified at ./ line 10

Can anybody help with this?

Replies are listed 'Best First'.
Re: Can't clone STD handle with :via layer
by tobyink (Abbot) on Apr 08, 2014 at 14:07 UTC

    That is odd. I don't understand why this happens. I have a couple of workarounds which work for me, but may or may not be suitable for whatever you're doing:

    • Temporarily binmode STDOUT, ':raw' before cloning it.

    • Don't clone STDOUT at all; just take a reference to it with my $fh = *STDOUT{IO}. (In this case fileno($fh) and fileno(\*STDOUT) will report the same number.)

    Update: I asked about this in #p5p and Zefram agrees that it seems like a bug, or at least the error message could be better. It's now on RT.

    use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name
Re: Can't clone STD handle with :via layer
by frozenwithjoy (Priest) on Apr 08, 2014 at 13:53 UTC

    Hi there. I have two suggestions:

    (1) Why are you trying to open a filehandle to STDOUT? Why not just print?

    For example, this:

    #!/usr/bin/env perl use strict; use warnings; use PerlIO::via::QuotedPrint; binmode STDOUT, ':via(QuotedPrint)'; print " ";

    Prints this:


    (2) If you actually need to use STDOUT as a filehandle, an alternative that gives the same output as above is this:

    binmode STDOUT, ':via(QuotedPrint)'; my $fh = \*STDOUT; print $fh " ";

      As i correctly understand '>&' - is standard technique to clone handle.

      I use Capture::Tiny, which clone all STD handles on each call. Probably other CPAN code may use handle cloning too. So i wish to understand, why cloning with :via layer not working.

      Maybe something missing in PerlIO::via::QuotedPrint - which prevents cloning normally?

        It's nothing specific to PerlIO::via::QuotedPrint; it seems to happen with all PerlIO::via::* modules, but not with built-in layers like :unix, :utf8, or :encoding(iso-8859-1).

        Update: indeed, the error message is generated within PerlIO::via. Here's the relevant code.

        use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1081498]
Approved by mtmcc
Front-paged by Corion
[choroba]: which lessons?
[prospect]: Learning Perl There where 17 videos.
[prospect]: And it was great because I'm using the book as well, to get some more examples or extra information

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2017-09-25 10:25 GMT
Find Nodes?
    Voting Booth?
    During the recent solar eclipse, I:

    Results (279 votes). Check out past polls.