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

While I am using the Enormous Power of PERL for my prestigious clients, I want to learn how to easily use PERL modules from cpan, like calling the modules,passing args to modules, etc. Please can anyone guide me on this topic?

  • Comment on Easy Way to remember usage of PERL modules

Replies are listed 'Best First'.
Re: Easy Way to remember usage of PERL modules
by tobyink (Canon) on Mar 01, 2013 at 09:52 UTC

    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
Re: Easy Way to remember usage of PERL modules
by Corion (Patriarch) on Mar 01, 2013 at 09:01 UTC

    Most of the Perl modules come with documentation on their usage. That documentation is available locally through the perldoc command:

    perldoc File::Spec

    That documentation is also available online through search.cpan.org: File::Spec.

    I find the "SYNOPSIS" section usually the most informative as it usually shows the intended use of a module.

Re: Easy Way to remember usage of PERL modules
by toolic (Bishop) on Mar 01, 2013 at 13:14 UTC
    In addition to the POD, many CPAN modules also provide test code, commonly in the t directory, which shows example usage. Furthermore, some modules provide ready-to-use example scripts; bin and examples directories are common places for those. Look at the MANIFEST link for a module on CPAN.

    Of course, you can search the internet for even more examples, especially on forums such as PerlMonks.

Re: Easy Way to remember usage of PERL modules
by nagalenoj (Friar) on Mar 01, 2013 at 09:03 UTC

    Every cpan module comes with pod documentation, which you can read. It'll have usage, descriptions, examples, everything...
    Also, you can always use some search engines to know the usage.

Re: Easy Way to remember usage of PERL modules
by pvaldes (Chaplain) on Mar 01, 2013 at 21:13 UTC
    I am using the Enormous Power of PERL for my prestigious clients... how can I call a module?

    liar XDD ;-)

    ...show us some code :-)