in reply to Opening not-existing command - Error Handling

Of course my goal would it be that the open command dies the program because the command "dira" does not exist.

Your program did exactly that. You told it to die with $!, which is the German text you see. You can make it die with another text if you do not like the German one.

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

    I tried the following:

    #!/usr/bin/perl use strict; use warnings; open(my $CMD, "dira |") || die "another text"; # ... close($CMD);

    But the program still produces the same error message:

    Der Befehl "dira" ist entweder falsch geschrieben oder konnte nicht gefunden werden.

      Not for me:

      #!/usr/bin/perl use strict; use warnings; open(my $CMD, "dira |") || die "something else\n"; while( <$CMD> ) { print "$_"; } close($CMD);

      Output:

      Can't exec "dira": No such file or directory at ./test.pl line 6. something else
      This form of open starts a child process. The return result is the child process ID. Thus, your open(...) || die ... will show the error if the child can't be created. You need to check $? to check for errors from the child. See this for an example of checking for errors in this situation.

        Thanks. You are right. Examining $? helps to know if the child process executed correctly or not.

        Here my sample code:

        eval { open my $pipe, "perl --version |"; my @res = <$pipe>; close $pipe; }; print "Exit Status: " . ($?>>8);

        If I use the not existing command "perl2 --version" then I get the following error message and the exit status 1.

        Der Befehl "perl2" ist entweder falsch geschrieben oder konnte nicht gefunden werden. Exit Status: 1

        If I use an existing command like "perl --version" then I get the exit status 0 and no error message:

        Exit Status: 0

        Problem of course is that I then know the error not directly after the open, but only when the child process exited, that means after the close.