Home: General Discussions: Tutorial/Article Suggestions for The Learning Cent:
Modules and References


Sep 6, 2000, 2:16 PM

Views: 45333
Modules and References

How about some topics on creating modules?

I spent serious time learning to write them and the documentation required is sparse at best. Even the good Perl books all seem to stop a day late and a dollar short when it comes to the nuances of modules and the tricks to make them more powerful. How about articles on Modules - General, Inheritance (Perl Style), Exporting Methods and tags, Importing Methods and tags, etc.

Also, I have struggled to find out what the heck the 'SUPER::' thing is that some of the core modules use. How about touching on those types of things?

Also, boy, do I remember the trouble I had when something like this happened to me...

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


package Some;

sub someMethod {
return \%someHash;
<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


use Some;

$someScriptVar = Some::someMethod();
print "Result: $someScriptVar\n";

Then when I ran someScript.pl, I got...
Result: HASH(0x0FF34800)

I've since learned some of the neat things
about references, but there are probably a
lot of new people out there struggle with the
concepts. Yeah ... read perlreftut POD ...
sure. I think I'll start with the Septugiant (? spelled wrong I think) to learn Greek. Smile

[This message has been edited by fashimpaur (edited 09-06-2000).]

[This message has been edited by fashimpaur (edited 09-06-2000).]

Enthusiast / Moderator

Sep 6, 2000, 2:56 PM

Views: 45333
Re: Modules and References

I'll answer as much as I can here. I don't have time to write a full article on it, since I have other things in the works.

First, as far as exporting functions and variables, have a GOOD READ through the Exporter.pm docs. They explain how to use the Exporter module, and the @EXPORT, @EXPORT_OK, and %EXPORT_TAGS variables.

Inheritance is far easier in Perl than, say, C++. None of this private/protected/public nonsense:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

package BaseClass;
sub new; # you do have to actually DEFINE these functions...
sub this;
sub that;

package SubClass;
@ISA = qw( BaseClass );
sub new;
sub this;

package main;
$a = SubClass->new;
# SubClass does not provide a 'that' method
# but SubClass inherits from BaseClass, which
# does have a 'that' method
# the equivalent call is similar to
# BaseClass::that($a);

A class can inherit from many other classes. The order of base-class searching is depth-first. That is, first check $ISA[0]'s methods. If not there, if that class has an @ISA tree, go through THAT, and so on. If no match is found, go to $ISA[1].

Hmm. That's all for now. You might see an article from me in a couple of months.

Jeff "japhy" Pinyan -- accomplished author, consultant, hacker, and teacher