How to read STDOUT from a Perl function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Alfred von Campe

    How to read STDOUT from a Perl function

    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
  • Jeff Bars

    #2
    Re: How to read STDOUT from a Perl function

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

    my $var = function();

    or maybe I misread...

    -JB

    "Alfred von Campe" <alfred@110.net > wrote in message
    news:4d599f95.0 307300723.4c352 f22@posting.goo gle.com...[color=blue]
    > 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[/color]


    ---
    Posted via news://freenews.netfront.net
    Complaints to news@netfront.n et

    Comment

    • Gerard Oberle

      #3
      Re: How to read STDOUT from a Perl function

      Would something like the following work?

      open FOO_IN, "perl -e \'require \"file.pl\"; &subroutine_cal l();\'"|"
      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;'

      Comment

      • Alfred von Campe

        #4
        Re: How to read STDOUT from a Perl function

        > Would something like the following work?[color=blue]
        >
        > open FOO_IN, "perl -e \'require \"file.pl\"; &subroutine_cal l();\'"|"
        > or die "blah blah blah";
        >
        > while (<FOO_IN>) {...}
        > close FOO_IN;[/color]

        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

        Comment

        Working...