in reply to Nested redirect for STDERR to a string buffer within a perl program

How about using 'local'?

#!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11164860 use warnings; sub divertSTDERR { my ($sub) = @_; my $buffer; local *STDERR; open STDERR, '>', \$buffer or die "open failed $!"; $sub->(); chomp $buffer; return $buffer; } my $buf = divertSTDERR( \&handleInner ); warn "output from handleInner:\n>>$buf<<\n"; warn "\n##################################################\n\n"; $buf = divertSTDERR( \&handleOuter ); warn "output from handleOuter:\n>>$buf<<\n"; sub handleInner { warn "inside handleInner\n"; } sub handleOuter { warn "before inner divert\n"; my $buf = divertSTDERR( \&handleInner ); warn "after inner divert\n"; warn "handleOuter returns >>$buf<< from handleInner\n"; }

Outputs:

output from handleInner: >>inside handleInner<< ################################################## output from handleOuter: >>before inner divert after inner divert handleOuter returns >>inside handleInner<< from handleInner<<

Is this doing what you want?

Replies are listed 'Best First'.
Re^2: Nested redirect for STDERR to a string buffer within a perl program
by Anonymous Monk on May 01, 2025 at 15:20 UTC
    Than you tybalt89,
    How about using 'local'? .. Is this doing what you want?

    Your code works, too.

    So, now I have to choose which one to use. :-)

      IIRC Capture::Tiny uses a real temp file, so the file handle is redirected even if you fork or exec. If that's something you want to capture, then you need that method. The 'local' to a scalar will only apply to perl code within the same process, but it will also be more efficient because an OS file handle isn't involved.