Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

What are Modules

by ddrumguy (Acolyte)
on Dec 07, 2001 at 20:02 UTC ( #130213=perlquestion: print w/replies, xml ) Need Help??

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

I am a complete (well sort of) newbie to perl. I had an intro to Perl class in the last couple of weeks and am working on my first projects at work - stripping reports down to put them in a database. Anyway other than a smidge of CL and Korn shell scripting - Perl is my first attempt at doing some consistent programming.... SO - that said I have a question. What is a module? How do i use it. What is CPAN? how does it tie into a module. I am scanning as much info as i can and i remember seeing these two (modules/cpan) recently - is their someplace here that can teach me all about modules and using them? THanks o bretheren bob

Replies are listed 'Best First'.
Re: What are Modules
by dragonchild (Archbishop) on Dec 07, 2001 at 20:11 UTC
    A module is a collection of functions that is used in multiple programs. You would use a module in different ways, depending on the way it's implemented.
    1. A class for an OO system
    2. A repository of functions, with Exporter
    CPAN is the Comprehensive Perl Archive Network. Among many other purposes, it serves as a repository for useful modules. Some of the ones you'll hear a lot about are CGI, Class::*, and Devel::*.

    You would download a module from CPAN and install it, using the instructions that came with the module.

    As for more info, I suggest purchasing Programming Perl, 3rd edition. There's a number of chapters there about modules and a whole chapter on CPAN.

    ------
    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

Re: What are Modules
by mirod (Canon) on Dec 07, 2001 at 20:12 UTC

    I think the first thing you need to learn is how to use the documentation that comes with Perl: try the following:

    perldoc

    Which leads you to:

    perldoc perldoc

    Which should lead you to:

    perldoc -q module

    and to:

    perldoc perlmod

    Note that man perl would also lead you to this information and that on Windows Active Perl also comes with the doc.

    Of course you can also use the Search on this site, but this should get you started!

Re: What are Modules
by George_Sherston (Vicar) on Dec 07, 2001 at 21:08 UTC
    I'd just like to add one more answer to the question "what are modules?", namely

    "worth taking the time to learn about"

    My first six months working with Perl I took the attitude that by the time I'd figured out each new module I could have done the work myself. It was a painful learning process. I now have the zeal of the convert, and my first instinct when I come up with a new problem is to browse CPAN. My advice, FWIW, is, if in doubt assume there's a module that will do it for you.

    In particular, if you're working with databases, I'd encourage you to check out DBI.pm, which makes a lot of things a different order of magnitude easier, and even makes some problems go away altogether (here's just one example of what I mean).

    Remember it's a J curve... you go a LONG way up for a very little dip at the start. I've tried both ways, and I know which I like better!

    George Sherston
      George,

      I agree with you, but I also think it is important for a newbie not to rely too much on modules because even though it will take more time to write your own code, I feel you learn Perl faster this way. This was the case when I started out. People were suggesting modules left and right and I said to myself "How am I going to learn this stuff if all I'm doing is using other people's code?"

      Now that I have a better grasp on Perl, I find myself using CPAN much more. I would suggest to a newbie to try and write the code themselves first and then check out CPAN to see if a module can do something they want better/faster (which is usually the case).

      Hope I don't get flamed for this, but this is the approach that I took.

      -Dru
        No flames, so long as you know you're wrong ;-) just kidding. Seriously though it's dependent on the end user. If they are the curious type and will actually attempt to understand the modules they use then that's good. I just rewrote a script from years ago that I used to select 5 random lines from a file that was atrocious, no scoping, vast assumptions (one of which was causing it to get locked in an infinite loop! Very Bad since it was run by cron), etc. etc. So I got Algorithm::Numerical::Shuffle and the script is now much more stable and trivial. And I really think that it would have been best for me to have used it in the first place long long ago. I would have been more confident in the script (or justifiably so), and spent time on making it work well instead of just making it work. On the other hand I wouldn't have been able to (it would have unnecessary) revisit it and see how very far I'd come ;-).

        --
        perl -p -e "s/(?:\w);([st])/'\$1/mg"

        It's a balance, really, IMHO. One needs to have an idea how the modules work otherwise one can't use them intelligently. But at the same time one has to let them do the work otherwise there's no point. But my guess is that most beginners err on the side of *not* using modules, and will always find ways to practice their skills by re-inventing a whole load of wheels. I too learnt a fair amount by rolling my own, but I think I would have learnt other things quicker, and been more productive, if I'd made the jump to modules sooner.

        Also, a fab way to learn good programming style is to look at how the modules do it. So instead of learning by rolling one's own, one can learn by getting to grips with the real thing.

        George Sherston
Re: What are Modules
by xunker (Beadle) on Dec 07, 2001 at 23:40 UTC

    Let's take things ong at a time, eh? I think you're going to want explainations in basic terms, and that's what I'm good at -- writing things in painfully terse form :)

    * What is a module? - A module is basically a collection of subrouties that is not a program by itself, but is used by other programs. It's all about code recuse and astetics. The calling program accesses and uses the subrouting inside the module.

    * How do i use it - You just said it: use module_name; or require module_name; Use and Require are similar but have different nuances, so 'use' would be your best bet. Put 'em up near the begining of the program.

    * What is CPAN? - CPAN: A networked storage system of Perl Modules. It combines the Storage of the modules, managment and instalation into come seamless package. When people say 'use CPAN', they usually mean do (as root usually) "perl -MCPAN -e shell", which gets you into the CPAN shell where you can install perl modules with a few keystrokes. You want to install NET::IRC? Just type 'install NET::IRC'. It's very slick.

    Module != CPAN. You can write you own modules in your own code, and I recommend that you do. CPAN is just a repostory of Generic and useful modules other people have written.

    * how does it tie into a module - If you write a module that is usful enough that others would like it, you can bundle it up and put it on the CPAN network for other people to use.

    * is their someplace here that can teach me all about modules and using them? - Here would be a great place to start. Using modules is pretty easy, you've probably already seen it/done it and not even known it. Making modules is harder, but worth learning if you want to write high-quality code.

Re: What are Modules
by scain (Curate) on Dec 07, 2001 at 20:31 UTC
    Once you read a little more about CPAN and want to install modules, try the command perl -MCPAN -e shell which can make installing modules a breeze (on unix systems, I think there is something different to do with windows, but I don't mess with those boxes).

    Scott

Re (tilly) 1: What are Modules
by tilly (Archbishop) on Dec 08, 2001 at 23:47 UTC
    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...

Re: What are Modules
by ddrumguy (Acolyte) on Dec 07, 2001 at 20:35 UTC
    Thanks. no perl man pages here at work. will need to do a bit of research at home. From a newbie perspective it may be helpful to add modules and a brief explanation of them (as one of the responses stated) in the newbie sections of this site. Heading to the CPAN url now to learn more ... thanks! bob
      If your saying that perl is installed, but you don't have any man pages, i.e. if 'perldoc perl' or 'man perl' doesn't work, then something is broken. Or maybe the perl you run linked to another directory not in your path. (does 'ls -l $(which perl)' indicate, e.g. that you're running /usr/local/bin/perl but its just a link to /opt/perl/bin/perl, and '/opt/perl/bin' is not in your PATH, and '/opt/perl/man' is not in your MANPATH).

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (6)
As of 2020-08-10 12:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Which rocket would you take to Mars?










    Results (57 votes). Check out past polls.

    Notices?