Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Re: Easy Way to remember usage of PERL modules

by tobyink (Abbot)
on Mar 01, 2013 at 09:52 UTC ( #1021208=note: print w/replies, xml ) Need Help??

in reply to Easy Way to remember usage of PERL modules

There over 27,000 distributions on CPAN. I guarantee that nobody can remember how to use them all! There are probably a few dozen modules that I know inside and out and never (or almost never) need to look at their documentation. For everything else, there's perldoc!

That said, many modules fall into one of three distinct categories:

  1. Pragmata: modules that effect the Perl language itself. Examples are: strict, warnings, indirect, Modern::Perl, etc.

    These usually have lexical scope, so only apply to the code block they're used in:

    { use warnings; # warnings are enabled here } # warnings are not enabled here
  2. Exporters: modules that give you functions. Examples: Carp, LWP::Simple, etc

    These generally allow you to pass a list of function names that you want to import. For example use Carp "croak".

    These are generally scoped by package.

    package Foo; { use Carp "croak"; # croak works here { package Bar; # croak does not work here } # croak works here again } # croak still works here

    And if you don't import croak you can generally still call it using the fully-qualified name, Carp::croak() provided somebody has loaded Carp.

  3. Classes: for object-oriented programming. Examples include: Path::Class::File, IO::Socket, etc.

    Unlike Pragmata and Exporters, there's generally no need to load classes at compile-time using a use statement; they can be loaded at run-time using require instead if desired.

    Classes are not lexically or package scoped like Pragmata and Exporters. Once the Person class has been loaded I can call Person::->new anywhere in my program and it will create a Person object.

    { package Foo; use Person; } { package Bar; my $bob = Person::->new; }

It's possible to create modules which are none of the above, or all of the above (!) but most fall broadly into one of those three categories.

Once you're familiar with those, then it's quite easy to remember that, say, Carp is an exporter and gives you the functions carp, croak, confess, and optionally cluck; Path::Class::File is a class which has methods like openr, slurp and spew; etc.

package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1021208]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (8)
As of 2017-06-25 13:12 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (567 votes). Check out past polls.