How to read STDOUT from a Perl function

Discussion in 'Perl' started by Alfred von Campe, Jul 30, 2003.

  1. I would think this is simple to do, but I just can't figure it out this
    morning. Assume I have a function in Perl which writes some output to
    STDOUT. I want to call this function from a Perl script and capture its
    output in a variable.

    If this was an executable or even another Perl script, I could use the
    open() function. But since this is a Perl function, I can't use open().
    Or can I? There's got be be a way, but my brain is just not cooperating
    this morning.

    Alfred von Campe, Jul 30, 2003
    1. Advertisements

  2. Alfred von Campe

    Jeff Bars Guest

    Assuming the function returns a value (and it should):

    my $var = function();

    or maybe I misread...

    Jeff Bars, Jul 30, 2003
    1. Advertisements

  3. Would something like the following work?

    open FOO_IN, "perl -e \'require \"\"; &subroutine_call();\'"|"
    or die "blah blah blah";

    while (<FOO_IN>) {...}
    close FOO_IN;

    - Jerry Oberle
    perl -e 'printf "mailto%c%s%c%s%cchase%ccom%c", 58, "Gerard", 46,
    "Oberle", 64, 46, 10;'
    Gerard Oberle, Jul 31, 2003
  4. Would something like the following work?
    Probably (I haven't tested it), but this solution is even uglier than
    what I had originally done :) . It looks like I did not miss something
    obvious; this functiononality does not exit in Perl. Basically, what I
    want to do is to temporarily assign STDOUT to a Perl variable, so that
    everything written to stdout is put in that variable.

    Alfred von Campe, Jul 31, 2003
  5. Alfred von Campe


    Jan 5, 2009
    Likes Received:
    Can't you just assign the STDOUT to variable?

    Something like:
    $variable = `echo cat123`;
    print $variable;


    cr4ft, Jan 5, 2009
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.