in reply to Re: Control Structures
in thread Control Structures

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!!!

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

    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

        I've added an else clause after the if, which kicks it to &failure

        Gavin.

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