Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > sending stdin to a shell command in perl

Reply
Thread Tools

sending stdin to a shell command in perl

 
 
Andrew
Guest
Posts: n/a
 
      11-24-2003
Hi,
in my simple perl script I have to send string to stdin of an
excutable binary which I run from perl. So I am doing the following:

e.g.

my ($result) = `echo "this will be sent to command" | ./command 2>&1`;
# process result...

is there more elegant way to send to command's stdin without using
echo?

Thanks
Andrei
 
Reply With Quote
 
 
 
 
Tad McClellan
Guest
Posts: n/a
 
      11-24-2003
Andrew <(E-Mail Removed)> wrote:

> in my simple perl script I have to send string to stdin of an
> excutable binary which I run from perl.



perldoc perlopentut

perldoc -f open


--
Tad McClellan SGML consulting
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
 
 
 
Andrew
Guest
Posts: n/a
 
      11-24-2003
(E-Mail Removed) (Tad McClellan) wrote in message news:<(E-Mail Removed)>.. .
> Andrew <(E-Mail Removed)> wrote:
>
> > in my simple perl script I have to send string to stdin of an
> > excutable binary which I run from perl.

>
>
> perldoc perlopentut
>
> perldoc -f open


Hm, I need to execute command from perl pass something to its stdin
and read from stdout. Can you give an example?
Thanks
Andrew
 
Reply With Quote
 
Ben Morrow
Guest
Posts: n/a
 
      11-24-2003

(E-Mail Removed) (Andrew) wrote:
> Hm, I need to execute command from perl pass something to its stdin
> and read from stdout. Can you give an example?


perldoc -q both

Ben

--
I've seen things you people wouldn't believe: attack ships on fire off the
shoulder of Orion; I've watched C-beams glitter in the darkness near the
Tannhauser Gate. All these moments will be lost, in time, like tears in rain.
Time to die. |-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-| (E-Mail Removed)
 
Reply With Quote
 
Darin McBride
Guest
Posts: n/a
 
      11-24-2003
Andrew wrote:

> (E-Mail Removed) (Tad McClellan) wrote in message
> news:<(E-Mail Removed)>.. .
>> Andrew <(E-Mail Removed)> wrote:
>>
>> > in my simple perl script I have to send string to stdin of an
>> > excutable binary which I run from perl.

>>
>>
>> perldoc perlopentut
>>
>> perldoc -f open

>
> Hm, I need to execute command from perl pass something to its stdin
> and read from stdout. Can you give an example?


That becomes a bit more difficult than I think Tad thought.
Admittedly, I missed it, too.

You may want to look at:

perldoc IPC::Open2


 
Reply With Quote
 
Matthew Braid
Guest
Posts: n/a
 
      11-24-2003
Darin McBride wrote:
> Andrew wrote:
>
>
>>(E-Mail Removed) (Tad McClellan) wrote in message
>>news:<(E-Mail Removed)> ...
>>
>>>Andrew <(E-Mail Removed)> wrote:
>>>
>>>
>>>>in my simple perl script I have to send string to stdin of an
>>>>excutable binary which I run from perl.
>>>
>>>
>>> perldoc perlopentut
>>>
>>> perldoc -f open

>>
>>Hm, I need to execute command from perl pass something to its stdin
>>and read from stdout. Can you give an example?

>
>
> That becomes a bit more difficult than I think Tad thought.
> Admittedly, I missed it, too.
>
> You may want to look at:
>
> perldoc IPC::Open2
>
>


If IPC::Open2 doesn't work for you (or is a bit unstable - I've had
problems a couple of times if I don't control the code of the process
I'm calling) you can do something like this:

1) create a temp file (File::Temp is good - make sure the file is not
automatically removed on exit)
2) fork with open('|-') (look this one up in the docs given above - very
handy)
3) In the child section, reopen STDOUT (and STDERR if you want) to the
temp filehandle, then exec the program you want to run.
4) In the parent program, print to the child's file handle (that you got
with the open call) the input you want to give it, then close the handle.
5) In the parent, seek to the start of the temp file and read in the output
6) Don't forget to clean up the temp file!

There's a lot of error checking you need to do (eg success of the open,
success of the exec, handling SIGPIPE if the child dies while you're
writing to it etc etc) but it will work without blocking problems (well,
so far it has all the time for me

This method doesn't really allow for a system where the parent and child
need to chat back and forth, but if that's the case then usually the
program you're calling will be written to expect interaction and
IPC::Open2 should be OK.

MB

 
Reply With Quote
 
Eric J. Roode
Guest
Posts: n/a
 
      11-25-2003
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Matthew Braid <(E-Mail Removed)> wrote in
news:bpu2q7$4jk$(E-Mail Removed):

> 1) create a temp file (File::Temp is good - make sure the file is not
> automatically removed on exit)
> 2) fork with open('|-') (look this one up in the docs given above -
> very handy)
> 3) In the child section, reopen STDOUT (and STDERR if you want) to the
> temp filehandle, then exec the program you want to run.
> 4) In the parent program, print to the child's file handle (that you
> got with the open call) the input you want to give it, then close the
> handle. 5) In the parent, seek to the start of the temp file and read
> in the output 6) Don't forget to clean up the temp file!
>
> There's a lot of error checking you need to do (eg success of the
> open, success of the exec, handling SIGPIPE if the child dies while
> you're writing to it etc etc) but it will work without blocking
> problems (well, so far it has all the time for me
>
> This method doesn't really allow for a system where the parent and
> child need to chat back and forth, but if that's the case then usually
> the program you're calling will be written to expect interaction and
> IPC::Open2 should be OK.


Is all this really better than the OP's original method of echoing a
string to a command pipeline within backtics?

- --
Eric
$_ = reverse sort $ /. r , qw p ekca lre uJ reh
ts p , map $ _. $ " , qw e p h tona e and print

-----BEGIN PGP SIGNATURE-----
Version: PGPfreeware 7.0.3 for non-commercial use <http://www.pgp.com>

iQA/AwUBP8NJB2PeouIeTNHoEQLV8QCaA8d2n/nQJKWIpSBvfYTnRfM5XqwAnAz8
hywvD69iD+MMVH/X6djAgPwP
=fRHB
-----END PGP SIGNATURE-----
 
Reply With Quote
 
Darin McBride
Guest
Posts: n/a
 
      11-25-2003
Eric J. Roode wrote:

> Is all this really better than the OP's original method of echoing a
> string to a command pipeline within backtics?


I suppose it depends on your definition of "better". Since the OP
didn't ask for "better" but "more elegant", I believe that IPC::Open2
is that "more elegant". I'm not as big of a fan of redirecting to a
file and reading that file, so I do not believe it to be more elegant.
It reminds me too much of DOS (which did its "piping" by redirecting to
a file and then redirecting from the file) or certain unix shells that
can't handle some complicated piping. (If that unix shells part sounds
vague, it's because it is - I never know when or where this bug will
hit me.)

If you only need to send a single, short line with no shell
metacharacters, then maybe the OP's method is fine. If you have
extensive dynamic content to send, then a pipe is a much better
solution. And IPC::Open2 is "the" answer for then reading from the
same process. Far from the only way (IO:ipe can work, too, as well
as the OP's method and the piping via file method). But my personal
preference for solving the OP's problem in the general case.

 
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
peek at stdin, flush stdin Johnathan Doe C Programming 5 05-17-2013 04:30 PM
How to pass stdin of a C++ program to the stdin of a process createdwith ShellExecute() Ben C Programming 2 08-29-2009 09:47 PM
STDIN, OUT, ERR and $stdin, out, err - Differences? Terry Cooper Ruby 7 06-09-2009 05:48 AM
can I run unix shell command in the ModelSim shell? clinton__bill@hotmail.com VHDL 2 02-18-2005 10:04 PM
Reading stdin once confuses second stdin read Charlie Zender C Programming 6 06-21-2004 01:39 PM



Advertisments