LPM: RE: Strict variable checking in classes?
Rich Bowen
rbowen at rcbowen.com
Thu Mar 29 08:36:37 CST 2001
Resend: Non member submission
-------- Original Message --------
From: "Braun, Thomas " <Thomas.Braun at asbury.edu>
Subject: RE: Strict variable checking in classes?
Date: Wed, 28 Mar 2001 23:23:57 -0500
One trick would be to create accessor methods, and use those. For
example:
sub count {
return $_[0]->{count};
}
Trying mymodule->coutn will return an error saying something to the
effect
of "can't locate method coutn via package mymodule", and you only have
to
spell it right in the subroutine :-)
You can also do nifty things like the following to set and/or return the
value (in places where this is desirable)
sub count (
my $self = shift;
$self->{count} = shift if (scalar(@_))
return $self->{count};
)
Anyone else have better ideas?
Tom
ps. I'm the giddy C++ programmer, not the shuddering one
> -----Original Message-----
> From: owner-lexington-pm-list at pm.org
> [mailto:owner-lexington-pm-list at pm.org]On Behalf Of David Hempy
> Sent: Wednesday, March 28, 2001 10:58 PM
> To: Perl Geeks
> Subject: LPM: Strict variable checking in classes?
>
>
>
> I'm delving into using some object oriented approaches in a
> perl program
> for the first time. Finding some really neat tricks.
>
> It seems that all instance variables (aka class attributes)
> are key/value
> pairs in an anonymous hash. This makes it really easy to
> tote along from
> function to function all the variables you might possibly need. Also
> really easy to create new instance variables on the
> fly...something that
> should make a C++ programmer absolutely giddy. (or make them
> shudder...)
>
> Anyway, one hitch I've run into is losing the "strict" checking of my
> variable names. If I spend a bunch of work incrementing
> $count and then
> try to print $coutn, perl will fuss at me at compile time.
> ("use strict"
> starts every program I write)
>
> 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...)
>
> Is this just the nature of the beast? Are there other ways to handle
> instance variables in a class? I fully expect to find the answer ten
> seconds after I send this message, so wish me luck.
>
> -dave
>
>
> ps. I've ordered the OOP book that several in the group have
> promoted...hope to have this program done before it gets here.
>
> --
> David Hempy
> Internet Database Administrator
> Kentucky Educational Television - Distance Learning Division
> <hempy at ket.org> -- (859)258-7164 -- (800)333-9764
>
>
More information about the Lexington-pm
mailing list