Re: Need dependency-free way of capturing verbose function output in a variable

[email protected] (Zefram)
Newsgroups perl.cpan.workers
Message-ID <[email protected]>
James E Keenan wrote:
>    my $oldfh = select(STDOUT);

What do you think this is doing?  What select() alters isn't what
you said you wanted to affect.  Even if you do want to change it,
you'd expect to change it back when you're done, which you don't do.
You're not making any use of $oldfh.

>    &$coderef;
>    close STDOUT;
>    return $stdout;

You're returning with STDOUT closed.  You probably want to restore it to
its former state, which you'd achieve by duplicating the original STDOUT
before closing it and then duplicating it back to STDOUT at the end.

In case the &$coderef produces no output, you should initialise $stdout
to the empty string, not leave it undef.

You should consider the behaviour on exceptions.  Whatever state
restoration you want to perform should be somehow tied to the stack frame,
so that it happens automatically on unwinding, rather than relying on
your function remaining in control.

$| doesn't gain you anything with output being captured in a scalar.

-zefram
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.