Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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

In reply to To module or not? by bradcathey

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2024-04-25 14:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found