in reply to A question of style.
usemy $query = "SELECT location FROM demstock2 WHERE ds_id=$main"; my $sth = $db->prepare($query); $sth->execute or print "Can't execute <pre>$query</pre>: " . $db->errs +tr . "<br>\n";
Whatever was in $main will be appropriately quoted at to make the SQL still valid and to protect your DB.my $query = "SELECT location FROM demstock2 WHERE ds_id=?"; my $sth = $db->prepare($query); $sth->execute( $main ) or print "Can't execute <pre>$query</pre>: " . +$db->errstr . "<br>\n";
Now, as for other aspects:
Now, in the subs, you don't have to print out the error codes; since these errors are 'fatal', simply return the error message; this avoids having to write the else brance, and thus 'flattens' your code more. With the code above, just make sure that successful completion returns '1'. This should now make your logic look like:my %actions = { 'BOND'=>\&do_bond, 'SHARE'=>\&do_share } if ( exists $actions{ $action } ) { my $err = &{ $actions{ $action } }; if ( $err ne '1' ) { print $err; } } else { print "Action is not defined."; }
sub do_bond { get LOCATION; check LOCATION or return; foreach @acc check PARENT_ID or return; update & insert or return; foreach @built check PARENT_ID or return; update & insert or return; return 1; # if you got here, everything's fine }
Update After reading a later reply, I misunderstood the logic in the for blocks; it doesn't change the suggestions above, as another post points out, you can still 'short circuit' with next.:
In such a case where multiple error statements were collected, I'd not print them all, but would collect them into an array or string, this being returned. An empty array or string would imply success.sub do_bond { get LOCATION; check LOCATION or return; foreach @acc check PARENT_ID or { set error; next; } update & insert or { set error; next; } foreach @built check PARENT_ID or { set error; next; } update & insert or { set error; next; } return 1; # if you got here, everything's fine }
IMO I think it's more important to try to avoid shuffling the main logic of a program into the else part of an if block *unless* the 'then' portion has as much or more logic. When dealing with web services in which you want to only handle cases you specifically code for, this may seem difficult to write, but cautious use of 'next', 'last', and 'break' statements can help maintain that. Also, it's just as easy to swap the else and then blocks for any statements, pushing all error handling to the end of the cdoe.
-----------------------------------------------------
Dr. Michael K. Neylon - mneylon-pm@masemware.com
||
"You've left the lens cap of your mind on again, Pinky" - The Brain
"I can see my house from here!"
It's not what you know, but knowing how to find it if you don't know that's important
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: A question of style.
by davis (Vicar) on Oct 30, 2001 at 21:15 UTC | |
by Masem (Monsignor) on Oct 30, 2001 at 21:48 UTC | |
by tommyw (Hermit) on Oct 30, 2001 at 21:26 UTC |