coding style Re: SPUG: Clearing a variable in a package

Fred Morris m3047 at inwa.net
Tue Aug 17 12:20:24 CDT 2004


At 9:55 AM 8/17/04, Dan Ebert wrote:
>I want the variable to be reset with every call ... is there a way of
>doing this without putting a $errstr = ''; at the begining of each
>method/subroutine in the package?

As a matter of style, and not necessarily limited to Perl, although it
works well with Perl's OO-paradigm, what I often do when I feel the need
for this sort of property is to code internal methods called good() and
bad() and call those in the return statement:

  return good();

or

  return bad( 'I smell smoke' );


I haven't met a debugger yet which won't let me set a break on bad()! It's
also self-documenting and vaguely useful to be able to grep | wc -l for the
instances of good() or bad().

Good will typically return true (or 1) after clearing errstr, and bad will
return false (or 0) after stashing the message.

A common extension is to allow an optional parameter which is the return
value..  or define variants goodref( ref ) which returns ref and badref(
message ) which returns undef.

I often override bad() to close connections or rollback changes.

--

Fred Morris
fredm3047 at inwa.net (I-ACK)




More information about the spug-list mailing list