Well we have to be careful about terminology here, so why do we start out making a few definitions.

A module _could_ be defined as a perl code file with a .pm extension that contains one or more package declarations

And a package _could_ be defined as a concept where code, declarations, subroutines, methods and documentation relating to a specific implementation are collected

Note that if we use these definitions, a package can span several modules (files with .pm extensions)

So lets consider just single .pm file that completely contains one package (declared with the package keyword)

So for package Foo::Bar, I need look for a file Foo/Bar.pm, which starts (in general) with the declaration

package Foo::Bar;

With that in place, you question about use and require, use is (generally) the same as

BEGIN {require Foo::Bar; Foo::Bar->import();}

In general, you'll make use of require (no pun) when dynamically pulling in modules i.e. you dont know until runtime which module is required (again no pun) until after compilation.

As for @_, I use this, but I am using Params::Validate a lot now and passing hashes so I have named argument lists e.g.

my $baz = Foo::Bar->new(easy => 'to', see => 'whats', happening => 'here');

which means no more remembering the positional significance of arguments - its almost self documenting

For data 'from your script' to the module, try not to call out to a scripts data from a module - it reduces the reusability of that module, as every script must be written with that data in place if it wants to use the services of the module.

So dont do this

package Dark::Madness; my $inner_demon = $main::higher_demon; ... 1;

Try to pass data back and forth through the interface - e.g.

$foobar->set_widget(param1 => value1, param2 => value2, ); $foobar->use_widget(); my $widget_dreams = $foobar->get_dreams(style => 'psychotic');

There are lots of consideration on the use of @EXPORT, @EXPORT_OK and %EXPORT_TAGS you should read up on, but basically try to use these as little as possible - they can be useful, but some people get upset when a symbol they have named gets clobbered by a symbol of the same name that is rudely exported by a modules @EXPORT list.

+++++++++++++++++
#!/usr/bin/perl
use warnings;use strict;use brain;


In reply to Re: Creating modules by leriksen
in thread Creating modules by sulfericacid

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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, details, 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, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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.