in reply to Override printing to STDOUT/ERR

Maybe Capture::Tiny serves your purpose enough:

$ perl -MCapture::Tiny=capture -wE'my($out,$err,$exit)=capture{say"Hel +lo";warn"Die\n";};say"OUT:$out";say"ERR:$err";say"EXIT:$exit";' OUT:Hello ERR:Die EXIT:1

Enjoy, Have FUN! H.Merijn

Replies are listed 'Best First'.
Re^2: Override printing to STDOUT/ERR
by perlancar (Hermit) on Dec 20, 2018 at 15:34 UTC
    While Capture::Tiny is simpler, the OP wanted to separate output according to the package/file the print() originates. So in this case, a tie-based solution like Tie::STDOUT is more appropriate as it lets you call your custom code on every print() or printf() or syswrite(), and then you can use caller() to extract the source location of each print() and act accordingly.