in reply to How to creater your own Perl modules/libraries?

module or library or package

You are in danger of being bogged down by semantics. The term "library" does not really mean anything specific in perl. The terms package, namespace, and class can also mean the same thing. The term module is often used to be the same thing as well. That is a simplification, as you learn more Perl then you will find that those terms do actually have their own distinct meanings, but right now you can consider them to be more or less the same.

So you want to create a module, read perlmod and perlmodlib. It is actually very easy. For example, put the following in MyModule.pm in the current directory:
package MyModule; use warnings; use strict; # Insert your subroutines here # Don't forget this: 1;
Then just use MyModule; and call the subroutines as (for example) MyModule::mysub();.

Replies are listed 'Best First'.
Re^2: How to creater your own Perl modules/libraries?
by JadeNB (Chaplain) on Nov 21, 2009 at 17:29 UTC

    This was an excellent post, but there's one caveat:

    # Don't forget this: 1;
    Note that this is not necessary. :-)

    UPDATE: If you don't already know that this is a joke, then please ignore it, or your module-writing life will be harder than it needs to be. If you don't want to ignore it, then at least read perlmod and the linked page carefully to see why it's a joke.

      Hi,

      Just want to confirm that the 1; is not necessary?

        I probably should have been more careful. It is necessary that your module return something true, and the easiest way to make sure that it does is to put 1; at the end of the source file *. All that I was saying was that what is actually returned may be anything true, including 1, "I'm a true string", or [ qw/references are always true/ ].

        To put it briefly: don't leave off the 1; at the end without a good reason (although the good reason may be “I thought of something funnier”). If you do leave it off, then do a simple test import of your module just to make sure that it can be done.

        * To be precise, this requires that you haven't done something tricky earlier, like returning earlier in the source file.

Re^2: How to creater your own Perl modules/libraries?
by newbie01.perl (Sexton) on Nov 23, 2009 at 10:34 UTC

    Hi,

    Thanks for your response ... will try it out ...