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

Hello,
I'm sure this is kinda basic, but I'm not finding an answer in my searches here, so I figured I'd ask. I'm trying to get data out of an external module, and having problems with the filehandle part of it. I'm using a module that has a method like:
$object->cat(\*FILEHANDLE, $target)
which will write the contents of $target to FILEHANDLE (and does treat it as a filehandle). Problem: I can't figure out how to get a filehandle other than STDOUT to work (I don't want to dump this to STDOUT, I want to read the data out of the filehandle & parse it).

Thanks for the help.

Replies are listed 'Best First'.
Re: Reading from a filehandle written to by a subroutine?
by BrowserUk (Patriarch) on Mar 17, 2005 at 17:14 UTC

    If your on 5.8.x, then you should be able to use a "memory file". See perlfunc:open/perlopentut.

    open my $memFH, '+>', \my $buffer; $object->cat( $memFH, $target ); while( <$fh> ) { ## do something with it } # Or $buffer =~ s[...][...]mg; #etc

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco.
    Rule 1 has a caveat! -- Who broke the cabal?
      Thanks for the pointers. A bit of background, since I wasn't terribly clear:

      I'm trying to get info out of a Subversion repository, using the Subversion perl bindings. One of those modules has a method (cat) where you specify the a target (including a revision number), and it writes that version of the file to a filehandle you give it. So, there's no file for me to open, but I need to be able to read from the resulting filehandle to get back the file from the repository.

      The Memory file seems like a promising tack to take, I'll try that.

      Thanks.

Re: Reading from a filehandle written to by a subroutine?
by holli (Abbot) on Mar 17, 2005 at 17:06 UTC
    You could try FileHandle:
    use FileHandle; ... $fh = new FileHandle; $fh->open("> file") or die $!; $object->cat($fh, $target);


    holli, /regexed monk/
Re: Reading from a filehandle written to by a subroutine?
by Mugatu (Monk) on Mar 17, 2005 at 17:05 UTC
    You can use a tied filehandle. See perltie for details.
Re: Reading from a filehandle written to by a subroutine?
by VSarkiss (Monsignor) on Mar 17, 2005 at 17:15 UTC

    I'm having a little trouble understanding this.

    So presumably doing the simple thing:

    open FOO, '>', $filename or die "Can't open $filename: $!\n"; $object->cat(\*FOO, $target);
    "doesn't work". Can you elaborate a little on what doesn't go as you expect?

    Also, you're saying it's OK with STDOUT, but you

    want to read the data out of the filehandle
    You can't read from STDOUT (not generally, anyway), so maybe the root of the problem is that you're trying to read from a file handle when it's opened for writing or vice versa?

    Adding a little more detail will help to get you a more accurate solution.

Re: Reading from a filehandle written to by a subroutine?
by sh1tn (Priest) on Mar 17, 2005 at 17:26 UTC
    use strict; use warnings; open FH, ">", "test$$.txt" or die $!; _cat(*FH, "This is a test\n"); sub _cat { my $fh = shift; my $data = shift; print $fh $data or die $! }
    Update:
    use strict; use warnings; my $data; #write to filehandle open WH, ">", "test$$.txt" or die $!; _write(*WH, "This is a test\n"); close WH; #read from filehandle into $data open RH, "test$$.txt" or die $!; _read(*RH, \$data); close RH; print $data; sub _write { my $fh = shift; my $data = shift; print $fh $data or die $! } sub _read { my $fh = shift; my $data = shift; $$data .= $_ while <$fh> }