Beefy Boxes and Bandwidth Generously Provided by pair Networks chromatic writing perl on a camel
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Comment on

( #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":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others exploiting the Monastery: (8)
    As of 2014-04-19 02:55 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      April first is:







      Results (475 votes), past polls