Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: String Buffer

by bruno (Friar)
on Aug 24, 2008 at 13:58 UTC ( #706563=note: print w/replies, xml ) Need Help??


in reply to String Buffer

Thanks a lot, I can make real use of this!

Replies are listed 'Best First'.
Re^2: String Buffer
by FunkyMonk (Canon) on Aug 24, 2008 at 15:10 UTC
    Or, without using tie:
    open my $oldout, ">&STDOUT" or die "Can't dup STDOUT: $!"; my $stdout; close STDOUT; open STDOUT, ">", \$stdout or die $!; # This is where the magic happen +s print "foo\n"; close STDOUT; open STDOUT, ">&", $oldout or die $!; print "Done fooling around\n"; print $stdout; # foo

    This is mostly copy & pasted from open, so you might want to look there for more.


    Unless I state otherwise, all my code runs with strict and warnings

      In fact, that uses a perlio layer silently.

      I've been wishing a perlio layer that allows you to simply open a stream that behaives like any tied handle eg.

      use StringBuffer; use PerlIO::Tied; # Note: never close one of the three standard handles. It's almost alw +ays a bad idea. open OLDSTDOUT, ">&=STDOUT"; open STDOUT, ">:tied(StringBuffer)"; # I presume extra arguments to ti +e get passed as extra argument to open print STDOUT "this magically gets put to \$stdout\n"; open STDOUT, ">&=OLDSTDOUT"; print STDOUT "this gets put to the original standard output again\n";

      This gets even more useful if you want to use a tied io handle from the command line eg. perl -MPerlIO::Tied -we 'some code' ':tied(SomeModule, arguments)'.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://706563]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2016-10-01 22:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How many different varieties (color, size, etc) of socks do you have in your sock drawer?






    Results (9 votes). Check out past polls.