http://www.perlmonks.org?node_id=1002513

jandrew has asked for the wisdom of the Perl Monks concerning the following question:

Hello all,

Whenever possible I use a subset of a project that I am working on to learn some new element of perl. This winds up being a really great way to improve my perl-Foo but can on occaision lead to a massive time sink with no actual result.

Currently I have hit an impasse on my latest 'I wonder if I can do that' and I would like some feedback if possible. I'm trying to open STDOUT to a subroutine reference and I'm intrigued by the fact that it doesn't die but it also doesn't behave the way I had hoped. I also wonder if it's significant that the variable is still a CODE ref even after I use it in the open statement.

use Modern::Perl; print "Hello World 1\n"; my $sub_ref = sub{ warn join( ' ', @_ ) . " - this is really bad" }; open( STDOUT, ">>", $sub_ref ) or die "open failed: " . $!; print "Hello World 2"; warn ref $sub_ref;

results in the output

Hello World 1 CODE at CodeTest.pl line 8.

I would like to get

Hello World 1 Hello World 2 - this is really bad at CodeTest.pl line 3 CODE at CodeTest.pl line 8.

Any help or even don't go there's with an explanation would be helpful. I am aware of tee and pipe and although tie seemed intriguing I couldn't get it to tie \*STDOUT to a CODE ref at all.

Replies are listed 'Best First'.
Re: open STDOUT to a subroutine reference?
by tobyink (Canon) on Nov 06, 2012 at 16:19 UTC

    IO::Callback to the rescue! (It's a module so cool, I couldn't stand to see it languishing unmaintained on CPAN, so I took over maintenance.)

    use IO::Callback; # Here's the coderef my $code = sub { my $str = shift; print STDERR "This is really bad: $str"; }; # Turn it into a filehandle opened in write mode my $fh = IO::Callback->new('>', $code); # Select that filehandle (makes it the default for print, say, etc). select($fh); # Print something - it gets passed to the coderef! print "Hello World\n";
    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

      tobyink++ yet again you have provided the answer.

      Thank you.