Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Tie *STDOUT, recursion error

by Superfox il Volpone (Sexton)
on Apr 11, 2015 at 17:09 UTC ( [id://1123142]=perlquestion: print w/replies, xml ) Need Help??

Superfox il Volpone has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I was thinking to tie the *STDOUT handle to mask the values to be printed in the stardard output. Though, I hit a deep recursion error, probably because I lose the original reference to *STDOUT and invokes my new handle when trying to print:
package hyundai{ sub TIEHANDLE{ my $class = shift; my $oldhandle = shift; return bless ({"handle" => $oldhandle}, $class); } my $mask = sub{ (my $text = shift()) =~ s/a/b/g; # replaces a with b return $text; }; sub PRINT{ my $self = shift; die("Not a ref") if !ref($self); my $text = join('',@_); print STDERR ("STDOUT: ", \*STDOUT, "\n"); # prints GLOB(0x880a20) + print STDERR ("STDERR: ", \*STDERR, "\n"); # prints GLOB(0x880a80) print STDERR ("handle: ", \$self->{"handle"}, "\n"); # prints GLOB +(0x8806c0) $self->{"handle"}->print( &$mask($text) ); # infinite recursion ! } }; print("Hello world 1 aaaaaaa\n"); my $oldhandle = *STDOUT; undef(*STDOUT); tie *STDOUT, "hyundai", $oldhandle; print("Hello world 2 aaaaaaa\n");
I was following this (old) post related to the same issue, though it does not seem that a working solution was proposed. Any idea how to proceed ?

s.fox

Replies are listed 'Best First'.
Re: Tie *STDOUT, recursion error
by choroba (Cardinal) on Apr 11, 2015 at 19:31 UTC
    Interestingly, the following works:
    print { $self->{handle} } $mask->($text);
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      hi choroba, shouldn't the two syntaxes be equivalent?! also, why is {$self->{"handle"}} and not *{$self->{"handle"}}. My understanding was that { } is used to deference a reference, such as ${ $ref_scalar }, so a sigil was required. But I am wrong as the above code example proves :/

      s.fox
Re: Tie *STDOUT, recursion error
by Athanasius (Archbishop) on Apr 12, 2015 at 06:20 UTC

    Hello Superfox il Volpone,

    I get no infinite recursion on my system (Windows 8.1, 64-bit):

    16:17 >perl 1215_SoPW.pl Hello world 1 aaaaaaa STDOUT: GLOB(0x1c94bc8) STDERR: GLOB(0x1c94c28) handle: GLOB(0x55c2d8) Hello world 2 bbbbbbb 16:17 >perl -v This is perl 5, version 20, subversion 2 (v5.20.2) built for MSWin32-x +64-multi-thread ...

    (Strawberry Perl, portable.)

    Hope that is of interest,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Re: Tie *STDOUT, recursion error
by Anonymous Monk on Apr 11, 2015 at 19:37 UTC
      thanks for the reply. I was looking to the source code of Tie::STDOUT. He basically solves it with something like:
      open(my $stdout, ">&", STDOUT); ... # in the overloaded method PRINT $stdout->print( @_ ); # this refers to the original STDOUT.
      I guess this opens a different underlying file handle, while my $oldhandle = STDOUT is an assignment/alias to the same file handle used by STDOUT.

      s.fox
Re: Tie *STDOUT, recursion error
by Superfox il Volpone (Sexton) on Apr 12, 2015 at 09:52 UTC
    hey Athanasius,
    it is interesting indeed. In my case I am under perl v5.16.3 on CentOS 7. I am starting to believe this is a non portable behaviour of perl.

    s.fox

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1123142]
Approved by philipbailey
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (3)
As of 2024-09-12 02:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    The PerlMonks site front end has:





    Results (15 votes). Check out past polls.

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.