|Perl Monk, Perl Meditation|
Re (tilly) 1: What are Modulesby tilly (Archbishop)
|on Dec 08, 2001 at 23:47 UTC||Need Help??|
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:
Now in each script where you might want to be able to "hello", put in the following two lines:
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...