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

Hi, I have a method that takes a file handle as a paramter, the method simply prints out something to that file handle, something like:
sub subroutine { my $fh = shift; print $fh "Hello, filehandle!\n"; }
Usually I open a file/socket and pass it to this method. But sometimes I just want to get the whole result as a string. Is there something like java's java.io.StringWriter, where I can use it as a file handle? (Of couse, I can also just rewrite the method to produce a string, but...)

Replies are listed 'Best First'.
Re: Can a string be a file hanle?
by CombatSquirrel (Hermit) on Sep 26, 2004 at 22:46 UTC
    Yes it can, have a look at perldoc -f open:
    sub subroutine { my $fh = shift; print $fh "Hello, filehandle!\n"; } my $dest; open my $fh, '>', \$dest; subroutine($fh); close $fh; print "\$dest is '$dest'";
    Cheers,
    CombatSquirrel.

    Entropy is the tendency of everything going to hell.

      That is a cool trick. I looked in opentut and open and didn't see any indication that such a thing was possible. Where did you find out you could do this?

      May the Force be with you
        If you look about a third of the way down on the "open" man page that you linked to in your own reply, you'll find the part that describes how 'File handles can be opened to "in memory" files held in Perl scalars...'

        Stringstreams were introduced somewhere near perl 5.8. Check if you have a new enough perl. If you didn't see this in perlfunc or perlopentut, you might have a too old version.

Re: Can a string be a file hanle?
by Arunbear (Prior) on Sep 26, 2004 at 22:34 UTC
    Zaxo's answer to one of your previous questions contains the answer to this question too.
      Darn it, as my kid would say. I guess I didn't really understand that one. Thanks.
Re: Can a string be a file hanle?
by JediWizard (Deacon) on Sep 26, 2004 at 22:47 UTC

    So as I understand it, you want to get the string printed by the subroutine into a variable in the calling namespace. If this is what you want try something like this:

    use IO::Pipe; use strict; my $pipe = IO::Pipe->new(); if(my $pid = fork()){ waitpid($pid, 0); $pipe->reader(); my $str = <$pipe>; print "\$str == '$str'\n"; }else{ $pipe->writer(); &print_to_pipe($pipe); } sub print_to_pipe { my $fh = shift; print $fh "Hello World\n"; }

    See IO::Pipe.

    May the Force be with you
Re: Can a string be a file hanle?
by Happy-the-monk (Canon) on Sep 26, 2004 at 22:32 UTC

    I just want to get the whole result as a string.

    Trying to find out what you mean... Do you want to return the printed string when you call   print? I am a bit puzzled.
    The code you write will do what the node's title suggest, print to the filehandle that was passed to the subroutine.

    Cheers, Sören

Re: Can a string be a file hanle?
by and11 (Initiate) on Sep 27, 2004 at 20:32 UTC
    Probably, you mean something that module IO::Scalar can do ? my $data; $SH = new IO::Scalar \$data; subroutine($SH);