Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

String Buffer

by kayos (Sexton)
on Apr 25, 2000 at 21:12 UTC ( #9037=sourcecode: print w/replies, xml ) Need Help??
Category: strings
Author/Contact Info T.R. Fullhart,

Sometimes I encounter a script or program that wants to print directly to STDOUT (like Parse::ePerl), but I want it in a scalar variable. In those cases, I use this StringBuffer module to make a filehandle that is tied to a scalar.


use StringBuffer; my $stdout = tie(*STDOUT,'StringBuffer'); print STDOUT 'this will magically get put in $stdout'; undef($stdout); untie(*STDOUT);
# $Id:,v 1.1 2000/04/03 22:53:04 kayos Exp $
package StringBuffer;

    my ($class, $c) = @_;
    return bless(\$c,$class);

sub PRINT {
    my ($self) = shift;
    ${$self} .= join('', @_);

sub PRINTF {
    my ($self) = shift;
    my ($fmt) = shift;
    ${$self} .= sprintf($fmt, @_);

#   sometimes Perl wants it...
sub DESTROY { };

Replies are listed 'Best First'.
Re: String Buffer
by bruno (Friar) on Aug 24, 2008 at 13:58 UTC
    Thanks a lot, I can make real use of this!
      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?

What's my password?
Create A New User
Node Status?
node history
Node Type: sourcecode [id://9037]
[choroba]: Pamela Anderson?
[choroba]: erix: I'm proud to be from a nation that also does one thing well
[LanX]: ah yes! And he called her Kit and she could talk while a light was going from her left to right eye and back...
[erix]: choroba: pretty impressive! :)
LanX imagines which nations would be dominating if the winter olympics were invented in East Asia
[LanX]: Corion: and that's the clue how Germany can dominate Handball tournaments, just continuing the domestic league and forbidding the best players of the world to attend :)
LanX (which would be a good scheme for England and football too)
[LanX]: If "Canada will play the Czech Republic for bronze on Saturday", that means Germany will play Syria in disguise...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (10)
As of 2018-02-23 16:38 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (303 votes). Check out past polls.