in reply to Control Structures

I use something similar to the following in most of my cgi scripts when making system calls.
use CGI::Carp qw(fatalsToBrowser); # rest of script my $CMD="some command"; system("$CMD"); ( $? == 0 ) ? print "job success\n" : print "job failure\n";
Ted
--
"Men have become the tools of their tools."
  --Henry David Thoreau

Replies are listed 'Best First'.
Re^2: Control Structures
by ghenry (Vicar) on Apr 29, 2005 at 13:10 UTC

    I love the look of this, because I really wanted to make the code look cleaner and I have been looking for a chance to use the Ternary Conditional Operator.

    Could I swap out the print statements for subroutine calls and move the success and failure things to something like &failure and &success?

    Something like:

    my $CMD="some command"; system("$CMD"); ( $? == 0 ) ? &success($time, $hostname) : &failure($time, $hostname) +;

    and then shift these scalars inside the subroutines?

    Thanks.

    Walking the road to enlightenment... I found a penguin and a camel on the way.....
    Fancy a yourname@perl.me.uk? Just ask!!!

      I'll answer my own question ;-)

      I tried it and it worked!

      For your info:

      Walking the road to enlightenment... I found a penguin and a camel on the way.....
      Fancy a yourname@perl.me.uk? Just ask!!!
        if (-s "$imagefile" < 700_000_000 and -e "$imagefile") { print '<script language="JavaScript"> var x=window.confirm("Is there a CD/DVD in the drive?" +) if (!x) window.location="http://hostname/cgi-bin/swish.cgi" </script>'; print "\n\nISO not too big and exists. \nBurning the CDR +W.\n\n" + ."\nPlease be patient, this will take a while...\n\n"; system "sudo cdrecord -v -eject dev=$device $imagefile"; + + } ( $? == 0 ) ? &success($date, $time, $hostname, $logdir, $to, $from) : &failure($date, $time, $hostname, $logdir, $to, $from);

        Hmm. That looks like mixed success to me. What happens if the test at the top of the if statement fails? Then $? will be 0 (unless it was fortuitously still set from a previous failed child process), even though cdrecord was never attempted.

        the lowliest monk