Re: Control Structures
by gellyfish (Monsignor) on Apr 29, 2005 at 11:06 UTC
|
I would suggest changing the structure of the program slightly - factor out the code from the else condition and then rather than die you can use an additional if ... else in the first condition. If the condition is true continue to execute the existing code else go the error handling subroutine.
/J\
| [reply] [Watch: Dir/Any] [d/l] [select] |
|
system (burning command);
if (my $cdburn = $?;) {
carry on with code
}
else {
errors
}
Walking the road to enlightenment... I found a penguin and a camel on the way.....
Fancy a yourname@perl.me.uk? Just ask!!!
| [reply] [Watch: Dir/Any] [d/l] |
Re: Control Structures
by zentara (Archbishop) on Apr 29, 2005 at 11:13 UTC
|
#!/usr/bin/perl
$SIG{__DIE__} = sub {
open my $h, ">>", "fatal.log";
print $h scalar localtime, ": @_";
close $h;
die @_;
};
die "ouch $!\n";
I'm not really a human, but I play one on earth.
flash japh
| [reply] [Watch: Dir/Any] [d/l] |
|
| [reply] [Watch: Dir/Any] |
|
I think I'll take the simple approach and restructure my program like gellyfish recommends, but thanks though.
The above looks a little tricky, as I am struggling with a simple if/else ;-)
Walking the road to enlightenment... I found a penguin and a camel on the way.....
Fancy a yourname@perl.me.uk? Just ask!!!
| [reply] [Watch: Dir/Any] [d/l] |
Re: Control Structures
by tcf03 (Deacon) on Apr 29, 2005 at 12:04 UTC
|
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
| [reply] [Watch: Dir/Any] [d/l] |
|
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!!!
| [reply] [Watch: Dir/Any] [d/l] [select] |
|
| [reply] [Watch: Dir/Any] [d/l] |
|
|