[Charlotte.PM] Cargo Cult Programming - @ISA array and $VERSION strings

William McKee william at knowmad.com
Mon Nov 22 11:01:43 CST 2004


At the meeting last week, we took a look at the modulemaker script
(which is part of the ExtUtils::ModuleMaker module[1]). It was this
script which produced automated code that I used in a recent project
without fully evaluating what it was doing (thus the references to cargo
cult programming in my message from last week).

The first problem I encountered was with inheritance. After copying
existing code into the framework, I was getting errors with inherited
methods not being found. As it turns out, the framework was overriding
my 'use base' pragma by directly setting @ISA in the BEGIN block.

At the meeting, we looked at how easy modulemaker is to use and the
framework code it creates for your new module. We then proceeded to add
a new test to the framework created by modulemaker to check whether use
base was being ignored or not.

I'm using Test::More[2] these days which offers many convenience tests
including can_ok and isa_ok. I wasn't sure whether isa_ok would properly
determine inheritance but knew that can_ok would tell me whether the
methods were imported. As expected, this test failed when run with the
default code. By changing the '@ISA = qw(Exporter)' line to 'push @ISA,
qw(Exporter)', the test passed.

Over the weekend, I also tried the isa_ok test which also works because
it checks the @ISA array which is unique for each package (in Perl,
inheritance is determined by the @ISA array; to check the value look at
@MyModule::ISA for packge MyModule or @main::ISA for the default
package). See Chapter 6 of Damian's book for more details.

The other problem I had was more of a minor nit than a serious issue
such as not getting properly subclassed. It had to do with the version
string not being quoted so that perl was dropping the ending zeros.
General rule of thumb is to put version strings into single quotes as
follows:

  $VERSION = '1.00';


Hope everyone has a good Thanksgiving!


Cheers,
William

[1]
http://search.cpan.org/author/RGEOFFREY/ExtUtils-ModuleMaker-0.32/lib/ExtUtils/ModuleMaker.pm
[2]
http://search.cpan.org/author/MSCHWERN/Test-Simple-0.50/lib/Test/More.pm

-- 
Knowmad Services Inc.
http://www.knowmad.com


More information about the charlotte mailing list