LPM: Strict variable checking in classes?

Steve Lane sml at zfx.com
Thu Mar 29 08:47:02 CST 2001


> Matt Cashner <eek at eekeek.org> wrote:
>     if($self->{$method}) {

should that be

      if (exists $self->{$method}) {

?  surely you don't want to disallow attributes
that have false values?

also,

>         return undef;

is always a bad thing to do.  if the method is
called in list context, a list with one element,
undef(), will be returned, which is true in boolean
context.  a simple "return;" is safer.

-- Steve

Rich Bowen wrote:
> 
> Resend - nonmember submission
> 
> -------- Original Message --------
> 
> From: Matt Cashner <eek at eekeek.org>
> To: Perl Geeks <lexington-pm-list at happyfunball.pm.org>
> Subject: Re: LPM: Strict variable checking in classes?
> 
> On Wed, 28 Mar 2001, David Hempy wrote:
> 
> > However, the laid-back approach of hash lookups will happily let me print a
> > useless $this->{coutn} after incrementing $this->{count} a few thousand
> > times.  Tracking down typos like that can be a real pain.  (And a
> > shuddering C programmer now beams...)
> 
> use vars qw($AUTOLOAD);
> sub AUTOLOAD {
>         my $self = shift;
>     my ($method, $return);
>     ($method = $AUTOLOAD) = ~s/.*:://;
>     if($self->{$method}) {
>         if(@_) {
>             $self->{$method} = shift;
>         }
>         return $self->{$method};
>     } else {
>         warn "there is no $method attribute of this object, bingo.";
>         return undef;
>     }
> }
> 
> provided i typed that in correctly, that should work for you. if you
> need an explanation of anything just ask :)
> 
> and blessed has one syllable in my vernacular :)
> 
> --------
> Matt Cashner
> Web Applications Developer
> The Creative Group (http://www.cre8tivegroup.com)
> eek at eekeek.org | Codito, ergo sum

--
Steve Lane <sml at zfx.com>



More information about the Lexington-pm mailing list