Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: open STDOUT to a subroutine reference?

by tobyink (Abbot)
on Nov 06, 2012 at 16:19 UTC ( #1002517=note: print w/replies, xml ) Need Help??


in reply to open STDOUT to a subroutine reference?

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'

Replies are listed 'Best First'.
Re^2: open STDOUT to a subroutine reference?
by jandrew (Chaplain) on Nov 06, 2012 at 16:47 UTC

    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: note [id://1002517]
help
Chatterbox?
[Lady_Aleena]: Well, not much of my code. A few little things people here like. The big things though, the more important to me code, people here seem to loathe.
[atcroft]: Lady_Aleena: My early code was pretty horrible... but somewhere early on I learned not to take criticism of my code personally... which was probably a good thing, considering the kind of code I wrote at the time. :\
[Lady_Aleena]: I am hungry, so I'm going to go make something to eat. I hope you figure it out atcroft. Have a nice night!
[atcroft]: I have one script that I have started re-writing several times over the years, and have yet to get it working in a cleaner/better form. (It is some smelly code-I did not even know about hashes at the time I wrote it.)
[atcroft]: Enjoy, Lady_Aleena!

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (7)
As of 2017-05-27 05:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?