in reply to Re: Opening not-existing command - Error Handling
in thread Opening not-existing command - Error Handling

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.

Replies are listed 'Best First'.
Re^3: Opening not-existing command - Error Handling
by AR (Friar) on Aug 16, 2011 at 15:29 UTC

    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
Re^3: Opening not-existing command - Error Handling
by Anonymous Monk on Aug 17, 2011 at 04:13 UTC
    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.