in reply to Re: Problems with open
in thread Problems with open

Thanks a lot! So it looks like I need a work around for this. I am using Perl 5.005_02. Any ideas?

-Shannon

Replies are listed 'Best First'.
Re: Problems with open
by Abigail-II (Bishop) on Jul 24, 2002 at 15:20 UTC
    Well, you could upgrade.... ;-).

    If you know the command after the pipe is going to be simple (just letters, digits and whitespace), you could test whether the first word exists somewhere in the current path and is executable. Or peek in the source of a modern perl and see how's it dealt with and mimic that.

    But note that even recent perls only handle cases that don't involve the shell.

    Abigail

      Testing whether it exists in the current path sounds like a great idea. I could do this in several cases. The problem occurs when it does exist and what is passed to it (via print) happens to be incorrect (i.e. if you were expecting parameters and they were in the wrong format).

      Could you give me a little direction of the checking if the first word exists somewhere in the current path. I can think of a difficult way of doing it, is there any simple or built-in method for checking if a word is in the PATH?

      I really appreciate your help. Thanks!

      -Shannon
        grep {-x "$_/$program"} split /:/ => $ENV {PATH};
        And if $program starts with a slash, you can just check -x $program.

        The other problem is more general. If the program exists, the fork and the exec will work. The other program *will* get started. The only "problem" is that it will terminate sooner than you expect. Hence, your pipe will be "broken". In that case (printing to a pipe that was closed on the other hand), your program will get a SIGPIPE. You could write something like:

        eval { local $SIG {PIPE} = sub {die "Pipe!"}; print FH "Whatever\n"; .... close FH or die; }; if ($@ && $@ =~ /^Pipe!/) { ... Do something ... }
        Note that you might reach the end of the eval before the SIGPIPE is delivered though.

        Abigail

        You could use Shell:
        use Shell; my $sh = Shell->new; my @commands = qw (ls garbage rm); for (@commands) { if ($sh->can($_)) { print "command $_ is available\n"; } else { print "command $_ is NOT available\n"; } }
        Update: sorry, sorry, this is wrong, I haven't really looked at the output of the script.

        ---- amphiplex