Re: More about module
by moodster (Hermit) on May 06, 2002 at 08:40 UTC
|
- It depends on you needs. If there is a subroutine you are using in several scripts you should put it in a module.
- Probably. Loading a module will usually require a little more overhead than including all the code in your script.
- I may be missing something here, but I have never used, nor seen anyone else use, an include file in perl (one that wasn't a module, that is). I imagine you could do neat tricks with eval to include files in a script, though.
- Reusability. We are lazy and prefer to only type our code once, then use it whenever we like. Using the module system is the standard way of writing software components in perl and it provides (among other things) support for locating modules in shared (/usr/lib/perl) or private (~/my/perl) directories, namespace protection and version numbers for your modules.
Cheers,
--Moodster
| [reply] [d/l] |
|
|
3 I may be missing something here, but I have never used, nor seen anyone else use, an include file in perl (one that wasn't a module, that is). I imagine you could do neat tricks with eval to include files in a script, though.
You are missing something:
require "c:/some_script.pl";
print some_func_from_some_script();
use Foo::Bar; is like BEGIN { require Foo::Bar } ie use is compile time, require is run time. You are just requiring in a file (the .pm is implicit as is the path which is assumed to be in @INC). See the docs.
cheers
tachyon
s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print
| [reply] [d/l] |
|
|
Every day, you learn something new. :)
But to be honest, I don't see why this should be better than putting your code in a proper module. It saves you the bother of messing around with Exporter, yes, but Exporter is your friend. Exporter wants to cuddle you and keep you safe from harm.
Cheers,
--Moodster
| [reply] |
|
|
Re: More about module
by Joost (Canon) on May 06, 2002 at 10:30 UTC
|
Just my 2 eurocents:
1. I commonly use modules (so *.pm files) for anything that loads perl code (except sometimes configuration files created with Data::Dumper and the like)
2. Modules are probably a little slower (but i'd be surprised if it's significant).
3. If you load modules using the use MODULE statement, the file is loaded eval'ed at compile time, so syntax errors in the loaded files abort the script before running. Also, if you include an import routine in your module, it will be called whenever a use MODULE statement is found. (but the module itself will only be loaded and eval'ed once: when the first use is found.)
4. The main reason to declare functions and variables in different packages, is to make sure you don't override some other (global) vars or subroutines, and so you can create objects from them (object classes all need their own package name). In the end though, the *.pm extention is not really needed for all this, it just makes the whole process a little easier and less error-prone, because you can use *.pm files . (see 3.)
See also:
perldoc -f use
perldoc -f require
perldoc perlobj
Joost.
| [reply] [d/l] [select] |
Re: More about module
by perrin (Chancellor) on May 06, 2002 at 16:30 UTC
|
In addition to the standard benefits of separating namespaces (very important for any long-term project), using modules makes it easier to move CGI scripts to mod_perl. If this is for a CGI script, definitely use a module with a separate namespace.
Also, there is no speed difference. A module is just an include file that declares a package name. | [reply] |
Re: More about module
by BUU (Prior) on May 06, 2002 at 12:40 UTC
|
Modules (technically its packages) are good because they prevent you from having a sub in your include file and a sub in your 'main' file with the same name. And if your include file is using globals of any sort, replace them with a function to return that value, or go all the way to OO and store all your data within your object.. | [reply] |
Re: More about module
by Anonymous Monk on May 07, 2002 at 05:45 UTC
|
Here is a rule I try to follow: If you think you will use a sub in more than two scripts, turn it into a module. Chances are you will probably use it more that you expect.
I don't use perl for web work so my point of view may be a little different but this is what I do.
I create a module CompanyX and make sub modules as I need them for all the interfaces I need at CompanyX:
Example:
CompanyX::Lotus_Notes
CompanyX::Oracle
CompanyX::file_converter
Now I have a module with all of companyX's interfaces. if the company changes its interface or switches db all I have to do is change either the db link in the script or change the module and all the scripts will be updated.
One other thing no-one mentioned. By using modules all of your simular code is in one location and you can call them from anywere, with include files you have to know the path to the include file. This is not a problem if you are storing all of your files in one location, but you become location dependent.
One other thing, if your code becomes usefull module form will make it easier to share.
Just my long winded 4cents
| [reply] |