Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > How to read STDOUT from a Perl function

Reply
Thread Tools

How to read STDOUT from a Perl function

 
 
Alfred von Campe
Guest
Posts: n/a
 
      07-30-2003
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
 
Reply With Quote
 
 
 
 
Jeff Bars
Guest
Posts: n/a
 
      07-30-2003
Assuming the function returns a value (and it should):

my $var = function();

or maybe I misread...

-JB

"Alfred von Campe" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> 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 http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
 
Gerard Oberle
Guest
Posts: n/a
 
      07-31-2003
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;'
 
Reply With Quote
 
Alfred von Campe
Guest
Posts: n/a
 
      07-31-2003
> 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
 
Reply With Quote
 
cr4ft cr4ft is offline
Junior Member
Join Date: Jan 2009
Posts: 1
 
      01-05-2009
Quote:
Originally Posted by Alfred von Campe
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
Can't you just assign the STDOUT to variable?

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

Best,

Joni
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
beginner Q: Kernel#puts, STDOUT, $stdout relation Andreas S Ruby 3 12-09-2006 12:39 AM
Problems redirecting STDOUT (NOT sys.stdout) to a pipe. Elad Python 0 03-19-2006 01:30 PM
copy stdout fails with permission denied when stdout is redirected brian.mabry.edwards@gmail.com Perl Misc 2 12-07-2005 10:49 PM
read stdout/stderr without blocking =?ISO-8859-2?Q?Jacek_Pop=B3awski?= Python 1 09-13-2005 07:25 AM
Read stdout into perl Sean Berry Perl Misc 9 07-14-2005 05:01 PM



Advertisments