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
    Alfred von Campe, Jul 30, 2003
    #1
    1. Advertising

  2. Alfred von Campe

    Jeff Bars Guest

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

    my $var = function();

    or maybe I misread...

    -JB

    "Alfred von Campe" <> wrote in message
    news:...
    > 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



    ---
    Posted via news://freenews.netfront.net
    Complaints to
    Jeff Bars, Jul 30, 2003
    #2
    1. Advertising

  3. Would something like the following work?

    open FOO_IN, "perl -e \'require \"file.pl\"; &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
    #3
  4. > Would something like the following work?
    >
    > open FOO_IN, "perl -e \'require \"file.pl\"; &subroutine_call();\'"|"
    > or die "blah blah blah";
    >
    > while (<FOO_IN>) {...}
    > close FOO_IN;


    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
    Alfred von Campe, Jul 31, 2003
    #4
  5. Alfred von Campe

    cr4ft

    Joined:
    Jan 5, 2009
    Messages:
    1
    Can't you just assign the STDOUT to variable?

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

    Best,

    Joni
    cr4ft, Jan 5, 2009
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?ISO-8859-2?Q?Jacek_Pop=B3awski?=

    read stdout/stderr without blocking

    =?ISO-8859-2?Q?Jacek_Pop=B3awski?=, Sep 12, 2005, in forum: Python
    Replies:
    1
    Views:
    3,987
    =?ISO-8859-2?Q?Jacek_Pop=B3awski?=
    Sep 13, 2005
  2. Elad
    Replies:
    0
    Views:
    404
  3. Andreas S
    Replies:
    3
    Views:
    247
    Eric Hodel
    Dec 9, 2006
  4. Sean Berry

    Read stdout into perl

    Sean Berry, Jul 14, 2005, in forum: Perl Misc
    Replies:
    9
    Views:
    176
    T Beck
    Jul 14, 2005
  5. Replies:
    2
    Views:
    320
    A. Sinan Unur
    Dec 7, 2005
Loading...

Share This Page