Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Using a subroutine as a module

by ysth (Canon)
on Nov 03, 2003 at 00:29 UTC ( #303972=note: print w/ replies, xml ) Need Help??


in reply to Using a subroutine as a module

The other answers so far have addressed your actual problem. I'd like to add a comment on your "validate.pm".

You start it with a shebang line, which is an odd thing to do since it is not actually executed (nor will any flags such as -w be parsed from the line by perl).

Usually (but only optionally) a module will declare its own name space and only export to the use'ing code subs that are requested:

in MyModule.pm:

package MyModule; use Exporter (); our @ISA = 'Exporter'; our @EXPORT_OK = qw/foo bar/; sub foo { "do some foo stuff" } sub bar { "do some bar stuff" } 1; # not actually needed since @EXPORT_OK= evaluates as true
and in the calling code:
use MyModule 'foo'; &foo();
You might take a look at perlmod.pod for more info on creating modules.

One last comment: one-level all-lowercase names like "validate" are usually for pragmas and are reserved for use by perl5-porters.


Comment on Re: Using a subroutine as a module
Select or Download Code
Re: Re: Using a subroutine as a module
by bobn (Chaplain) on Nov 03, 2003 at 02:09 UTC

    Actually, better not to export at all:

    in MyModule.pm:

    package MyModule; sub new { bless {}, shift } sub foo { "do some foo stuff" } 1;
    and in the calling code:
    use MyModule; my $o = new MyModule; $o->foo();
    $o is an object. It can have data, but in this case, it is just a way to tell perl where to find the subroutine foo(). This means you could have several different modules, each with a routine named 'foo;, and by using the object each module returns (customarily) from the call to new(), you can tell them apart. Whereas if you have the misfortune to use 2 modules that both export foo into your namespace, who knows what happens?

    --Bob Niederman, http://bob-n.com

    All code given here is UNTESTED unless otherwise stated.

      I would rather use class methods than create a dummy object with no other purpose than to qualify the package to call.

      File::Spec is an excellent example of when that works well.

      In the more typical case, however, you would be just as well off importing nothing and always fully qualifying your calls (e.g. MyModule::foo() ). Importing some things is a programmer convenience to save typing and improve readability. If the sub names reflect what they do, conflicts should be rare (and can always be resolved by fully qualifying).

      Egads, far too many uses of OO are superfluous and/or ill advised already and now you want OO for simple library calls. Just use fully qualified package names:

      #### MyMod.pm package MyMod; sub foo { "whatever" } 1; ### calling script #!/usr/bin/perl -w use strict; use MyMod; print MyMod::foo();

        Sorry, I disagree. Modularization of code is desirable. IMHO, using a dummy object to acheive this is more valid than fully qualifying the calls for the following reasons:

        • Typically, less typing, as filenames tend to be longer han variable names (in my world, at least)

        • greater flexibility - if you decide to relocate something in the filesystem, you change only your 'use' and 'new' statments, not everyplace the calls are made.

        • "future-proofing" - as the user gains sophisitication, he or she can more easily modify the modules, or create new ones that inherit from the existing. Exporting or fully qualifying can defeat that.

        • "future-proofing" - on the day when the user suddenly wants to set instance data for his routines, he or she is ready.

        Besides, fully qualified calls just look ugly to me, though I suppose it's safer than trusting multiple modules not to stomp on each other or your own functions. Using object methods are also fully qualified.

        --Bob Niederman, http://bob-n.com

        All code given here is UNTESTED unless otherwise stated.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://303972]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (15)
As of 2014-07-29 13:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (217 votes), past polls