Old habits die hard

heusserm on 2003-09-12T12:04:10

"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"; } else { #Whatever } #...Snip 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.

Thoughts?


Re: "Thoughts?"

Juerd on 2003-09-12T14:27:30

Yeah....

my style