in reply to Re: How to Speed up MySQL w/ Perl
in thread How to Speed up MySQL w/ Perl

Hey Abigail,

Please enlighten me on the last bit of your code which I reproduced below:

if ($@) { $dbh -> rollback; die $@; } else { $dbh -> commit; } # Can we leave out the 'else' and just say: if ($@) { $dbh -> rollback; die $@; } $dbh -> commit;
Is there any difference?

Replies are listed 'Best First'.
Re: How to Speed up MySQL w/ Perl
by Abigail-II (Bishop) on May 26, 2004 at 11:59 UTC
    Sure, you can leave off the else, but that makes your code less flexible. Suppose you want to replace the die with a warn, then you shouldn't forget introduce the else. Again, if there's just a commit, not much of a problem, if there are a whole bunch of statements, you have to figure out which ones belong logically in the else, and which ones should be run regardless whether the eval block failed. If you start off the else to begin with - or rather, if you use that as a habit, you won't have such problems. Having the else block is more of an engineering issue than a semantic one.

    Abigail

      Great, thanks!

      Now I get a better idea as I sometimes leave out the 'else' but have no idea whether it's a good practice.