[pgh-pm] Why you want a subroutine instead of a package variable....
Daniel J. Wright
Dan at DWright.Org
Thu Apr 13 15:07:34 PDT 2006
One of the issues presented in the talk last night is why you'd want to have
my $DEBUG
sub DEBUG { $DEBUG = shift if @_; return $DEBUG }
instead of just
our $DEBUG
One excellent reason that I don't think was covered is that one of those
will catch typo mistakes and the other will not:
$Class::Observable::Debug = 1;
Class::Observable::Debug(1);
In the first case, Perl will do what you tell it to do, it sets $Debug to
1. It doesn't matter that in the rest of your code you only cares about
$DEBUG. In the second case, if you or one of your users accidentally use
the Debug subroutine, Perl will yell at you and tell you that it can't
find the Debug subroutine.
I can personally vouch for many hours of yelling at my computer because I
*knew* I had set that variable before realizing that I was doing something
stupid like this.
Accessor subs helps find errors like these quicker and saves debugging time.
-Dan
More information about the pgh-pm
mailing list