Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re (tilly) 1: What are Modules

by tilly (Archbishop)
on Dec 08, 2001 at 23:47 UTC ( [id://130448]=note: print w/replies, xml ) Need Help??


in reply to What are Modules

As a beginner, what is probably going to be most helpful is a template to cargo-cult from. When you have read all of the other resources, you will understand what all the pieces here do. But this is a "quick start".

Now I am going to suppose that you have a place somewhere to put your library of Perl stuff you are developing. For the sake of argument that will be "/your/library/path". Now let's create the Foo::Bar module. Create the file "/your/library/path/Foo/Bar.pm". Into it put this:

package Foo::Bar; use Exporter; @ISA = 'Exporter'; # This is a list of functions you are willing to export. @EXPORT_OK = qw( hello ); use strict; # of course # Functions go here. Let's be traditional sub hello { print "Hello, World\n"; } 1;
Now in each script where you might want to be able to "hello", put in the following two lines:
use lib "/your/library/path"; use Foo::Bar qw(hello);
And after that you can call the function "hello" freely.

Now why would you do this? Well the classic syndrome that I see in shell programmers who pick up Perl is that they convert a mess of shell scripts into Perl scripts, and have the Perl scripts call each other like you would in shell. This leaves you with all of the limitations of shell programming, like difficulty handling errors, problems passing around structured data, etc.

What works better in my experience is to take your scripts and write each one as above as a module. Then you can replace the scripts with trivial ones that load up the right module, and call one function in it. But now when your scripts want to call on each other's functionality they can just load up the the right modules and call functions rather than execute external scripts. This gives you all of the flexibility, better performance, and opens up the door later for you to improve how you do your error handling or get fancier in how you store and pass data...

Now be warned as you go off that what I have given is but one common form of module. There are others. And you can easily get fancier (eg by using pod). But this should be enough to actually go and try writing some...

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (6)
As of 2024-03-28 11:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found