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

Is there a way to create a single perl document listing all common procedures, which can then be referenced only by name within other scripts?
  • Comment on External script to hold common procedure calls

Replies are listed 'Best First'.
Re: External script to hold common procedure calls
by gaal (Parson) on Jul 03, 2004 at 20:56 UTC
    I'm not sure if this is what you meant, but if you want to import "everything" into your own namespace by just one "use" statement, you can put this in Everything.pm:

    package main; # mixin use IO::File qw(whatever); use Net::..... use SomethingElse; use EtCetera; 1;

    This will put everything in the main namespace, which may be good enough for you. If, however, you want to call this from a different namespace and want everything imported there, you need some more magic than a (naive) mixin:

    package Everything; sub import { my $pkg = caller; eval qq{ package $pkg; use This; use That; use And::So::On; 1; } or die $@; } 1;

    That said, this is probably a bad idea if taken to the extreme. You'll have "namespace pollution", that is, the modules you use will fight for each other's subroutine names.

    (Anyone know how to do this without the eval?)

Re: External script to hold common procedure calls
by davidj (Priest) on Jul 03, 2004 at 20:51 UTC
    What do you mean by "common procedures?". Are you refering to procedures native to perl that are used regularly? Or procedures that YOU define that are common to programs that you are writing? (similar to C/C++ libraries)

    If you want the latter (procedures that YOU define that are common to programs that you write), then the answer is yes. Create a module containing the procedures and use it in you program in the same way you would use any module.

    Check Tutorials for some help on how to create them.

    hope this helps,
    davidj
Re: External script to hold common procedure calls
by davido (Cardinal) on Jul 04, 2004 at 04:47 UTC

    You might be desiring what Perl calls packages and modules.

    Start by creating a file called Mysubs.pm. In this file, put the following:

    package Mysubs; use strict; use warnings; use base qw( Exporter ); our @EXPORT = qw( fee fi fo fum ); sub fee { print "You called Mysubs::fee()\n"; } sub fi { print "You called Mysubs::fi()\n"; } sub fo { print "You called Mysubs::fo()\n"; } sub fum { print "You called Mysubs::fum()\n"; } 1;

    Save that file in your script's working directory for simplicity for now. ...later you'll want to do something else with it and modify @INC to deal with that, but for now just save it in the same path as the main script.

    Next create another script as follows:

    #!/usr/bin/perl use strict; use warnings; use Mysubs; fee(); fi(); fo(); fum();

    Presto, it works. There are much more elegant constructs you can use as you get more familiar with modules and packages; exporting only those subs you need, object oriented design, and so on. But this should get you started. Our Tutorials section has a lot of good information to keep you busy for awhile. And merlyn's Aplaca book, "Learning Perl Objects, References & Modules", published by O'Reilly & Assoc. is a first class introduction and detailed course in Perl's modules and object oriented world.


    Dave