Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Can't clone STD handle with :via layer

by zdm (Sexton)
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 ./1.pl line 10

Can anybody help with this?

Comment on Can't clone STD handle with :via layer
Select or Download Code
Re: Can't clone STD handle with :via layer
by frozenwithjoy (Curate) 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:

    =20=

    (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
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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1081498]
Approved by mtmcc
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (18)
As of 2014-12-18 15:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (58 votes), past polls