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

Re: Override printing to STDOUT/ERR

by Corion (Pope)
on Apr 13, 2018 at 13:10 UTC ( #1212804=note: print w/replies, xml ) Need Help??

in reply to Override printing to STDOUT/ERR

I would follow Jenda's advice in Re: !Overriding Builtin print and use tie to redirect all kinds of writes to a filehandle to a subroutine of my choosing.

Replies are listed 'Best First'.
Re^2: Override printing to STDOUT/ERR
by bliako (Prior) on Apr 13, 2018 at 13:56 UTC

    thanks for the recommendation. Following your suggestion I stumbled upon  Tie::STDOUT (by David Cantrell) which does the trick:

    #!/usr/bin/env perl use strict; use warnings; use Tie::STDOUT print => sub { my @params = @_; my @calhis = (); my $i = 2; # to avoid printing stacktrace for Tie::STDOUT # build a kind of stacktrace: while( my ($package, $filename, $line, $subroutine, $hasargs, $wantarray, $evaltext, $is_require, $hints, $bitmask, $hinthash) = caller($i++) ){ push(@calhis, "$filename, line $line : $package".'::'."$subrou +tine"); } # print the stacktrace with whatever needs to be printed print join("->", @calhis), " : ", @_; } ; print "from main\n"; sub1("main"); sub2("main"); sub sub1 { my $via = $_[0]; print "from sub1 via $via\n"; } sub sub2 { sub1("sub2"); }

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1212804]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (2)
As of 2020-10-25 06:51 GMT
Find Nodes?
    Voting Booth?
    My favourite web site is:

    Results (249 votes). Check out past polls.