-
use vars, dammit!2005-03-18 11:00 in /tech/perl/HallOfShame
Or
ourif you prefer. Whatever you do, don’t refer to variables in the current package using the full, explicit package name. Concretely, don’t say,@My::Package::ISA = qw( My::SuperClass );
Instead, write
our @ISA = qw( My::SuperClass );
or
use vars qw( @ISA ); @ISA = qw( My::SuperClass );
The problem with the first is that it is typo-prone. If you typo the package name,
perl -cwwon’t complain (in fact, perl will happily do exactly what you said), but you’ll get errors like ‘Can’t locate object method “foo” via package “My::Package”’. This is particularly likely to happen when you create a new subclass and copy the old subclass to use as a template. The alternatives are typo-proof, and easier to read to boot.(BTW, this is a specific case of the general principle Don’t Repeat Yourself. In this case, you already typed the package name once in the
packagedirective. Doing it again anywhere else in the file is just asking for trouble sometime later.)
Comments
prakash wrote:
Kevin wrote:
Leave a comment
Please use plain text only. No HTML tags are allowed.
Comments are closed for this story.
Trackbacks are closed for this story.