Beefy Boxes and Bandwidth Generously Provided by pair Networks Joe
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Perl library or new module?

by LaidBackWebSage (Acolyte)
on Jun 02, 2004 at 14:14 UTC ( #359448=perlquestion: print w/ replies, xml ) Need Help??
LaidBackWebSage has asked for the wisdom of the Perl Monks concerning the following question:

I need some clarification...

I have been tasked with job of re-writing the code for a web site. The entire site runs on Apache/mod_perl, and is a mish-mash of code, what I would describe as "stream-of-consciousness" code. :-)

My goal is to reduce everything to a bunch of subroutines and default variables and constants that get called as needed.

My conundrum is: how do I do this?

The boss wants a module, but that does not seem correct to me. Most of the code we are using is already depending on modules, so I would need to include calls to these other modules wthin my module, and that just doesn't seem right.

So I looked at putting all my subroutines into 2 Perl libraries: one for database-related code and another for all other code.

But I am not able to "see" the subroutines or variables that I declare in the Perl Library files.

The test script, as well as the Perl libraries, are using the "use strict" pragma, as well as the taint flag (-T) in the shebang.

I suspect that I will have success if I utilize "Package::Foo" syntax, but I want to avoid that if possible.

Fellow coders, what say you?

Many thanks for your input!

Comment on Perl library or new module?
Re: Perl library or new module?
by Fletch (Chancellor) on Jun 02, 2004 at 14:25 UTC

    perldoc Exporter. And consider finding a copy of Refactoring for a structured approach to rewriting.

      Just to add to Fletch's reply, check out Schwern's Refactoring article on perl.com.

      I would add to Schwern's list after Pull Logical Chunks Out into Functions. Once that's done, I would look at the functions and determine whether or not they can re-factored into modules or libraries. I would favor modules over libraries most of the time. The only decision I would face is whether to OO or not - perlmodstyle.

      -derby
Re: Perl library or new module?
by Ninthwave (Chaplain) on Jun 02, 2004 at 15:25 UTC

    Get a safari account on O'Reilly's and check out Perl Objects, References & Modules. I would suggest buying the book outright but the safari account lets you see if this is needed in your library before hand.

    Quoting from the above:
    Chapter 12 Using Modules
    "A module is a building block for your program: a set of related subroutines and variables packaged so it can be reused."
    That defines what you are doing.

    I would also suggest these resources:

    "No matter where you go, there you are." BB
Re: Perl library or new module?
by perrin (Chancellor) on Jun 02, 2004 at 15:45 UTC
    A library, as in a file of perl code with no package statement that just magically adds subs and variables to your current namespace when you use it, is generally considered to be an out-of-date approach that is supported mostly for the sake of legacy perl4 code. All new libraries should be written as modules. This is much safer in a number of ways.

    Modules use other modules all the time. It is a normal and expected way to do things. It may turn out that your site is best divided into several modules with varying purposes. Most significant sites are best broken up into more than one.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (9)
As of 2014-04-19 10:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (480 votes), past polls