Ask a Question related to PERL Miscellaneous, Design and Development.
-
Dave Saville #1
pipe open question
I am having trouble getting to grips with piped opens - notably in
catching a failure. I have looked through my perl books and done a
google search but am not getting very far.
open FOO, "| bar" or die $!;
print "something";
if bar does not exist then the script stops with the die message
"something" is not printed. But if we have
open FOO, "|bar a_string_with_shell_metas" or die $!;
print "something";
then a fork gets involved to spawn a shell and the expected errors
occur - and the script says "something" and keeps on going.
I found an article that implied you could use a test like:
my $pid = open FOO, "|bar a_string_with_shell_metas";
if ( defined $pid)............
But that does not seem to work as I assume the pid is that of the
shell, that worked, rather than the failed bar.
So how can I *always* find out if the open failed, wether or not there
are metas in the string? Given that I see two types of failure - bar
does not exist, it does but failed to start for some other reason.
TIA
Regards
Dave Saville
NB switch saville for nospam in address
Dave Saville Guest
-
name pipe problem, quick question
I'm trying to re-write a start up script for a game server (hl ds) and setup named-pipes (cmd-1, for input, and res-1, for output, in /home187/b/),... -
Pipe animation question
Hello: I need to show particles (bubbles, dirt, etc) contuinouosly moving through sections of a pipe via Flash MX 2004. The pipe is shown in two... -
open question: www.ihearithurts.com
OK, so Ture wants similar code for the page transitions www.ihearithurts.com. I did them, but I still haven't gotten a response regarding why s/he... -
Can't use "open my" with pipe
This works: open SOURCE 'gunzip -c test.rdf.gz|' or die $!; my $source = \*SOURCE; This doesn't ("No such file or directory"): open my... -
fflush to pipe hangs when child at other end closes the pipe
My AIX 4.3.3 application calls popen(name, "w") to create a pipe to a the named child executable. All is well as long as the child stays active. ... -
D Borland #2
Re: pipe open question
You could place it in and eval block like so
eval {
...
your code in here;
...
}
then just check $@ at the end of block to see if i encountered an error
during it's block.
Remeber with perl, there's always more than one way to do it. So i''m not
saying this is the best way, but it's one way of doing it
D Borland
d (place a dot here) borland (an at-sign here) ntlword (and another dot
here) com
"Dave Saville" <dave.nospam@ntlworld.com> wrote in message
news:qnirfnivyyragyjbeyqpbz.hldaya2.pminews@text.n ews.ntlworld.com...> I am having trouble getting to grips with piped opens - notably in
> catching a failure. I have looked through my perl books and done a
> google search but am not getting very far.
>
> open FOO, "| bar" or die $!;
> print "something";
>
> if bar does not exist then the script stops with the die message
> "something" is not printed. But if we have
>
> open FOO, "|bar a_string_with_shell_metas" or die $!;
> print "something";
>
> then a fork gets involved to spawn a shell and the expected errors
> occur - and the script says "something" and keeps on going.
>
> I found an article that implied you could use a test like:
>
> my $pid = open FOO, "|bar a_string_with_shell_metas";
> if ( defined $pid)............
>
> But that does not seem to work as I assume the pid is that of the
> shell, that worked, rather than the failed bar.
>
> So how can I *always* find out if the open failed, wether or not there
> are metas in the string? Given that I see two types of failure - bar
> does not exist, it does but failed to start for some other reason.
>
> TIA
>
>
> Regards
>
> Dave Saville
>
> NB switch saville for nospam in address
>
>
---
This e-mail has been virus scanned and is certified virus Free.
Checked by AVG anti-virus system ([url]http://www.grisoft.com[/url]).
Version: 6.0.518 / Virus Database: 316 - Release Date: 9/11/03
D Borland Guest
-
Anno Siegel #3
Re: pipe open question
Dave Saville <dave.nospam@ntlworld.com> wrote in comp.lang.perl.misc:
Quite so.> I am having trouble getting to grips with piped opens - notably in
> catching a failure. I have looked through my perl books and done a
> google search but am not getting very far.
>
> open FOO, "| bar" or die $!;
> print "something";
>
> if bar does not exist then the script stops with the die message
> "something" is not printed. But if we have
>
> open FOO, "|bar a_string_with_shell_metas" or die $!;
> print "something";
>
> then a fork gets involved to spawn a shell and the expected errors
> occur - and the script says "something" and keeps on going.
>
> I found an article that implied you could use a test like:
>
> my $pid = open FOO, "|bar a_string_with_shell_metas";
> if ( defined $pid)............
>
> But that does not seem to work as I assume the pid is that of the
> shell, that worked, rather than the failed bar.
With a sufficiently recent Perl you can avoid the shell invocation> So how can I *always* find out if the open failed, wether or not there
> are metas in the string? Given that I see two types of failure - bar
> does not exist, it does but failed to start for some other reason.
with this form of open:
open FOO, '|-', 'bar', '*' or die "$!";
instead of
open FOO, 'bar *' or die "$!";
The first form will not use a shell, but exec the command "bar" with
a literal asterisk as an argument. So, if "bar" isn't found, open()
will fail. Note that open() still doesn't indicate an error that
happens while "bar" is running, it returns too early to do that. You
get those errors from close().
If you *want* the shell to run, the failure to find "bar" is such a run-time
error (of the shell) that cannot be picked up by open(). Close the pipe
explicitly and check the return value of close(). If it comes back false,
there was an error in executing the pipe and the return code is in $?.
So with a piped "open" involving a shell, two error checks are needed:
open my FOO, '| bar *' or die "$!";
# try working with FOO.
close FOO or die "command error $?";
In the part commented "try working with FOO", you will often already
notice if something's amiss. You must be prepared to read unexpected
things from FOO (or, more likely, be unexpectedly unable to read
anything).
In one case, even close() doesn't indicate a failure to find "bar", which
is when the shell runs "bar" in the background, as in "open FOO, 'bar &'".
Here the shell always (well usually) succeeds, because all it does is a
fork. That the child doesn't find "bar" doesn't concern it.
Anno
Anno Siegel Guest
-
Dave Saville #4
Re: pipe open question
On 17 Sep 2003 12:56:50 GMT, Anno Siegel wrote:
<snip>
Now this is interesting - here is some code:>So with a piped "open" involving a shell, two error checks are needed:
>
>open my FOO, '| bar *' or die "$!";
># try working with FOO.
>close FOO or die "command error $?";
use warnings;
use strict;
open SENDMAIL, "|-", "/usr/lib/sendmail1 -oi -t" or die "can't fork
$!\n";
print SENDMAIL <<EOF;
From: me
To: you
Subject: None
EOF
print SENDMAIL "a message\n";
close SENDMAIL;
if ( $? )
{
print "sendmail failure $?\n";
die "sendmail failure $?\n";
}
print "Note sent\n";
exit;
Under OS/2 perl 5.8.0 I get:
Error reading "/usr/lib/sendmail1": No such file or directory at try
line 4.
D:/BIN/sh.exe: /usr/lib/sendmail1: not found
sendmail failure 32512
sendmail failure 32512
On Solaris perl 5.8.0 I get:
Can't exec "/usr/lib/sendmail1": No such file or directory at ./try
line 4.
can't fork No such file or directory
So on one OS the first check catches the fact that /usr/lib/sendmail1
does not exist and on the other it is the check on close.
Ain't portability fun? :-)
Regards
Dave Saville
NB switch saville for nospam in address
Dave Saville Guest



Reply With Quote

