Ask a Question related to PERL Miscellaneous, Design and Development.

  1. #1

    Default 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

  2. Similar Questions and Discussions

    1. 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/),...
    2. 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...
    3. 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...
    4. 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...
    5. 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. ...
  3. #2

    Default 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

  4. #3

    Default Re: pipe open question

    Dave Saville <dave.nospam@ntlworld.com> wrote in comp.lang.perl.misc:
    > 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.
    Quite so.
    > 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 a sufficiently recent Perl you can avoid the shell invocation
    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

  5. #4

    Default Re: pipe open question

    On 17 Sep 2003 12:56:50 GMT, Anno Siegel wrote:

    <snip>
    >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 $?";
    Now this is interesting - here is some code:

    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

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139