Slash Boxes
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

heusserm (4461)

  (email not shown publicly)
AOL IM: MatthewHeusser (Add Buddy, Send Message)

Matt Heusser is JAPH and an XP-Er []. (The Methodology, not the Operating System.) Right now, he's doing a lot of Test Driven Development in Perl.

Journal of heusserm (4461)

Friday September 12, 2003
07:04 AM

Old habits die hard

[ #14668 ]

"Hi. My name is Matt, and I spent a great deal of time coding in C++"

I've found that my C++ idioms end up in my perl code. Here's a code sample:

#begin sample

sub DoStuff
        my $blnOk = 1;
        my $strMessage = "Success";
        my $dbh = Get_DBH_Function();
        if (!$dbh)
                $blnOk = 0;
                $strMessage = "Couldn't get DBH";
        return($blnOk, $strMessage);
#End Snippit

I've got if ($blnOk) fields to get the DBH, to get the STH, to prepare, to execute ... blah.

How about something more like this?

#Begin Snippit #2
use Carp;
sub DoStuff2
        eval {
                my $dbh = Get_DBH_Function();
                if (!$dbh) {
                        croak "Failed to get dbh in DoStuff";
                #More code ...

        if ($@ ne "") {
                        return(0, $@);
        } else {
                        return(1, "Success");
#End Snippit #2

Of course, the 3rd way to do it is to Croak without the eval, and trust the calling routine to eval it - The BSD way. But I'm not convinced.

The morals of the story:

1) I'm thinking of dumping my hungarian notation
2) I'm thinking of changing my curly-brace style so my code is more compact
3) I'm thinking of adding some structured exception handling instead of writing structured code. (IE, Most of my current code has one entry point and one exit for each function, and returns status state and messages.) Since I won't have try/catch until perl 6, I'll make do with eval.


The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
More | Login | Reply
Loading... please wait.