http://qs321.pair.com?node_id=1212804


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 (Monsignor) 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"); }