in reply to Opening not-existing command - Error Handling

Using SVN::Notify, here's a very simple precheck and snippet:
#!/usr/bin/perl use strict; use warnings; use SVN::Notify; my $command = 'xmllint --version'; open(my $CMD, "$command |") or die $!; my $exe = SVN::Notify->find_exe($command); while (<$CMD>) { print $_; } close $CMD;
Update: Try this:
#!/usr/bin/perl use strict; use warnings; use SVN::Notify; my $command = 'dir'; my $exe = SVN::Notify->find_exe($command); system($exe);

Replies are listed 'Best First'.
Re^2: Opening not-existing command - Error Handling
by Dirk80 (Pilgrim) on Aug 16, 2011 at 11:50 UTC

    Thank you. Sounds good. I tried it. But my problem is now that I always get the message "Cannot locate command" for a existing command and for a not-existing command.

      Remember that "dir" is not an executable. It's a shell built-in. So try "cmd /c dir" as your command (and thus your executable is "cmd"). Though, really, dir is a bad example because it's so trivial to do inside perl. Other built-ins may be more difficult/impossible to emulate in perl, so the question is still more or less valid as a general question, just not so much for this particular example.

Re^2: Opening not-existing command - Error Handling
by Dirk80 (Pilgrim) on Aug 17, 2011 at 12:50 UTC

    Thanks. Now it works fine after your update. Here a sample code snippet:

    #!/usr/bin/perl use strict; use warnings; use SVN::Notify; my $command = 'perl'; my $exe = SVN::Notify->find_exe($command); if( defined $exe ) { print "Command \"$command\" exists!"; } else { print "Command \"$command\" does NOT exist!"; }

    Only problem is that it is not able to handle parameters. If I use the command "perl --version" then I receive the answer that this command does not exist. If I use only "perl" then I get the answer that it exists.

    Fazit: Thank you a lot. This is a very good method to find out whether a command exists or not. But it is necessary to cut away the parameters of the command before giving it to the SVN::Notify->find_exe function.