in reply to stderr, local block and redirection

Try my $output = ""; instead. And you may actually want to print something to STDERR inside the block.

#!/usr/bin/perl use strict; use warnings; sub test_stderr { my $output = ""; { open local(*STDERR), '>', \$output; print STDERR "CATCH ME IF YOU CAN\n"; } print "\$output is '$output'\n" if $output; } test_stderr; print STDERR "I'm not caught\n";

Update: Gah, got a halfway version in my clipboard. Tweaked slightly.

Replies are listed 'Best First'.
Re^2: stderr, local block and redirection
by jfroebe (Parson) on Sep 13, 2006 at 17:07 UTC

    Woohoo! that worked! :) Why would setting $output to a non undef value make it work?

    Jason L. Froebe

    Team Sybase member

    No one has seen what you have seen, and until that happens, we're all going to think that you're nuts. - Jack O'Neil, Stargate SG-1

      Although it's not explicitly explained that I can find, my guess would be that under the hood it needs an actual not-undef SvPV* into which the data is written. Otherwise it's going to be trying to append things to the single global undefined value &PV_sv_undef which is what throws the error.

        I think the buffer for read and for sysread must be similarly initialized.
        my $read = read($fh, my $buf='', $MAX_READ);

        That makes perfect sense :) Interesting that it only occurs after the first occurance of the code. Into the bowels of the Perl engine I dare not go without asbestos lined bloomers. ;-)

        Jason L. Froebe

        Team Sybase member

        No one has seen what you have seen, and until that happens, we're all going to think that you're nuts. - Jack O'Neil, Stargate SG-1