http://www.perlmonks.org?node_id=451574

bradcathey has asked for the wisdom of the Perl Monks concerning the following question:

Fellow Monasterians,

I need a little help to understand the importance of using a module in it's official form or not. Thanks to tachyon's tutorial I've successfully coded my first working module.

package MyModule2; #line 1 use strict; use Exporter; our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS); $VERSION = 1.00; @ISA = qw(Exporter); @EXPORT = (); @EXPORT_OK = qw(add_it); %EXPORT_TAGS = ( All => [qw(&add_it)]); # line 11 sub add_it { my $amount = shift; my $total = 12+ $amount; return ($total); } 1;

Which is called by:

#!/usr/bin/perl -w print "Content-type: text/html\n\n"; #use lib "/home/jdsakroc/public_html/admin/cgi-bin"; use strict; use warnings; use CGI::Carp qw(fatalsToBrowser); use MyModule2 qw(:All); my $amount = 12; print add_it($amount),"\n";

But, the following works as well (minus the first 11 lines of orig.)

sub add_it { my $amount = shift; my $total = 12+ $amount; return ($total); } 1;

Which is called by:

use MyModule; my $amount = 12; print add_it($amount),"\n";

Question is: What is the point of creating the module with all the header stuff, when I can just save the function in a ______.pm file and use it like that? How clueless is this? Thanks!

Update: Thanks all, I'm getting it now ;-) In one word: "namespace." I can see how similiarly named functions would collide if the namespace was the same.


—Brad
"The important work of moving the world forward does not wait to be done by perfect men." George Eliot