Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

open STDOUT to a subroutine reference?

by jandrew (Hermit)
on Nov 06, 2012 at 16:13 UTC ( #1002513=perlquestion: print w/ replies, xml ) Need Help??
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.

Comment on open STDOUT to a subroutine reference?
Select or Download Code
Re: open STDOUT to a subroutine reference?
by tobyink (Abbot) 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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1002513]
Approved by marto
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (15)
As of 2015-07-06 12:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (74 votes), past polls