Re: How to handle input and output of a program executed by System function in perl
510046470588-0001_at_t-online.de
Date: 07/20/04
- Next message: Reinhard Pagitsch: "Re: lwp post question"
- Previous message: Jürgen Exner: "Re: How to handle input and output of a program executed by System function in perl"
- In reply to: zw: "Re: How to handle input and output of a program executed by System function in perl"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 20 Jul 2004 08:50:48 +0200
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
- Next message: Reinhard Pagitsch: "Re: lwp post question"
- Previous message: Jürgen Exner: "Re: How to handle input and output of a program executed by System function in perl"
- In reply to: zw: "Re: How to handle input and output of a program executed by System function in perl"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|