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

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

Replies are listed 'Best First'.
Re: Problems with open
by Abigail-II (Bishop) on Jul 24, 2002 at 16:12 UTC
    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

      Good solutions and good help. Thanks very much!

      -Shannon
Re: Re: Re: Problems with open
by amphiplex (Monk) on Jul 24, 2002 at 16:05 UTC
    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
      This is better. Still I won't be able to check to see if the command executed properly, but you solved the problem I asked for and I appreciate that.

      Thanks!

      -Shannon