neilwatson has asked for the wisdom of the Perl Monks concerning the following question:

I've read that WIFEXITED from POSIX is supposed to reverse the logic of the system command so that commands that return above zero will be evaled as false. But, my test shows nothing happening. What have I missed?

#!/usr/bin/perl use strict; use warnings; use feature 'say'; use Data::Dumper; use POSIX qw/ WIFEXITED /; use Test::More tests => 2; WIFEXITED( system '/bin/false' ) or die $!; ok( WIFEXITED( system '/bin/false' ), "Should fail because exits 1" ); ok( WIFEXITED( system '/bin/true' ), "Should pass because exits 0" ); done_testing(); Run it: 1..2 ok 1 - Should fail because exits 1 ok 2 - Should pass because exits 0
What does test one pass?

UPDATE: This works:

#!/usr/bin/perl use strict; use warnings; use feature 'say'; use Data::Dumper; use POSIX qw/ WIFEXITED /; use Test::More tests => 2; ok( WIFEXITED( ( system '/bin/false' ) >> 8 ) , "This should fail because exits 1" ); ok( WIFEXITED( ( system '/bin/true' ) >> 8 ) , "This should pass because exits 0" ); done_testing(); Run it: 1..2 not ok 1 - This should fail because exits 1 # Failed test 'This should fail because exits 1' # at ./perl line 10. ok 2 - This should pass because exits 0 # Looks like you failed 1 test of 2.

Neil Watson
watson-wilson.ca

Replies are listed 'Best First'.
Re: WIFEXITED when system returns none zero
by pme (Monsignor) on Aug 11, 2015 at 17:26 UTC
    Hi Neil,

    In according to system: "The return value is the exit status of the program as returned by the wait call. To get the actual exit value, shift right by eight."

    #!/usr/bin/perl use strict; use warnings; use POSIX qw/ WIFEXITED /; WIFEXITED( (system '/bin/true') >> 8 ) or die "true $!"; WIFEXITED( (system '/bin/false') >> 8 ) or die "false: $!";
    UPDATE: You may find autodie interesting.
    #!/usr/bin/perl use strict; use warnings; use autodie qw/:all/; system '/bin/true'; system '/bin/false';

      Actually the use of Test:: was intentional. It's part of a test plan. So 'ok' and not 'die'. Good catch on the shift by 8 though.

      Neil Watson
      watson-wilson.ca