in reply to Opening not-existing command - Error Handling
So my question is, how can I check when I open a process, if the command really exists?Just try to run the command and see if that fails. Don't fail fatally (die()) if you do not want to.
Alternatively, you could wrap your stuff into an eval { ... } block and then examine $@ (e.g. Problem executing command in the example above). You could also try one of the many CPAN modules that supports handling of exceptions.use strict; use warnings; my $run_cmd = "dira"; if ( open(my $CMD, "$run_cmd |") ) { while( <$CMD> ) { print "$_"; } close($CMD); } else { die "Problem executing command $run_cmd - $!"; # ... or something non-fatal }
Update:
Another possibility would be to loop through all pathes in the environment variable PATH and then checking if the command exists.There is this ancient thread and some CPAN modules like Dir::Which or File::Which that can be used to perform apriori checks - but see cdarke's comments below.
Update2:
The program above was tested and worked fine under Linux. Since I am not a Windows-expert,
I can only do wild guesses on what the problem could be:
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Opening not-existing command - Error Handling
by Dirk80 (Pilgrim) on Aug 16, 2011 at 12:10 UTC | |
by Anonymous Monk on Aug 17, 2011 at 13:11 UTC |