For Programmers: Free Programming Magazines  


Home > Archive > PERL Modules > July 2004 > How to handle input and output of a program executed by System function in perl









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author How to handle input and output of a program executed by System function in perl
zw

2004-07-18, 8:55 pm

Could anybody tells me how to handle input and output of a program
executed by System function in perl, please? I used a system function
defined in a perl script to call a program such as a Java or C program
to run and then I want to capture the return result from this Java or
C program to a local variable defined in this perl script.
Jürgen Exner

2004-07-18, 8:55 pm

Xref: kermit comp.lang.perl.modules:45520

zw wrote:
> Could anybody tells me how to handle input and output of a program
> executed by System function in perl, please? I used a system function
> defined in a perl script to call a program such as a Java or C program
> to run and then I want to capture the return result from this Java or
> C program to a local variable defined in this perl script.


Exaclty which part of the third paragraph of the man page for system() is
unclear (see "perldoc -f system")?
Please let us know so it can be improved.

Oh, why do you believe this has anything to do Perl modules?

jue


zw

2004-07-19, 8:55 pm

"Jürgen Exner" <jurgenex@hotmail.com> wrote in message news:<JmBKc.4704$Iz3.2470@nwrddc01.gnilink.net>...
> zw wrote:
>
> Exaclty which part of the third paragraph of the man page for system() is
> unclear (see "perldoc -f system")?
> Please let us know so it can be improved.
>
> Oh, why do you believe this has anything to do Perl modules?
>
> jue


Hi,
I think Perl is good at dealing with system functions like other
scripting languages do. But I am not sure how does it handle inputs
and outputs from
external programs? for example,
system "java Foo arg1 arg2";
Can I assign the result returned by Foo.java to a variable like
$a = system "java Foo arg1 arg2";
In fact, if the Foo.java prgram caculates arg1 + arg2, then $ a
should be the sum of them.
I am not sure can system function do so and if not, which function I
should use to deal with this problem?
Thanks in advance.
Scott W Gifford

2004-07-19, 8:55 pm

[ Followup-To set to comp.lang.perl.misc, which is more appropriate ]

zw@cs.man.ac.uk (zw) writes:

[...]

> I think Perl is good at dealing with system functions like other
> scripting languages do. But I am not sure how does it handle inputs
> and outputs from
> external programs? for example,
> system "java Foo arg1 arg2";
> Can I assign the result returned by Foo.java to a variable like
> $a = system "java Foo arg1 arg2";


You can use the backticks operator for this:

$a = `java Foo arg1 arg2`;

You can also write to an external program by opening a filehandle
piped to that program:

open(F,"|java Foo arg1 arg2")
or die "open failed: $!";
print F "Some input data\n";
close(F)
or die "close failed: $!";

Or read from an external program by opening a filehandle piped from
that program:

open(F,"java Foo arg1 arg2|")
or die "open failed: $!";
my $line = <F>;
close(F)
or die "close failed: $!";

If you want to read and write, you can use IPC::Open2 or IPC::Open3.

Good luck!

---ScottG.
Jürgen Exner

2004-07-20, 3:55 am

zw wrote:
> "Jürgen Exner" <jurgenex@hotmail.com> wrote in message
> news:<JmBKc.4704$Iz3.2470@nwrddc01.gnilink.net>...
[...][color=darkred]
[color=darkred]
> I think Perl is good at dealing with system functions like other
> scripting languages do. But I am not sure how does it handle inputs
> and outputs from
> external programs? for example,
> system "java Foo arg1 arg2";
> Can I assign the result returned by Foo.java to a variable like
> $a = system "java Foo arg1 arg2";


Did you read the perldoc page for system()? Did you? Read the third
paragraph again!
Which part is still unclear?

Note: you may want think again if you want to capture the return value or
the output of the program foo. Those are two very different things.

> In fact, if the Foo.java prgram caculates arg1 + arg2, then $ a
> should be the sum of them.
> I am not sure can system function do so and if not, which function I
> should use to deal with this problem?


Both parts of this question are very clearly answered in the manual page for
system(). Why don't you just read it?

jue


510046470588-0001@t-online.de

2004-07-20, 3:56 pm

zw@cs.man.ac.uk (zw) writes:
>
> Hi,
> I think Perl is good at dealing with system functions like other
> scripting languages do. But I am not sure how does it handle inputs
> and outputs from
> external programs? for example,
> system "java Foo arg1 arg2";
> Can I assign the result returned by Foo.java to a variable like
> $a = system "java Foo arg1 arg2";


the simplest way is
$a = `java Foo arg1 arg2`;

another example is
$s = `cat filename`;
for retrieving the content of an external file, when cat is the
unix command.

in array context, this approach splits the output string into lines.


a safer way would use the functions fork, exec, and pipe for starting
the external programm and indirecting its output into a file handle.

if the program reads or writes (but not both) continually from Stdin/out,
the open function may be used with a pipe.

bidirectional communication, as needed when e.g. writing a perltk interface
to a command line interpreter like octave, is more difficult,
as it has deadlocks coming if not handled carefully.
IPC::Open2 may help, or IO::Pty ;

Klaus Schilling

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com