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

Hi
,
How can i convert a perl program (.pl) that i have written into a perl module (.pm)?

thanks.

Replies are listed 'Best First'.
Re: building modules
by Bilbo (Pilgrim) on Jun 09, 2003 at 14:31 UTC
Re: building modules
by hardburn (Abbot) on Jun 09, 2003 at 14:25 UTC

    It depends. At a bare minimum, you need a package declaration, and probably use the Exporter.

    ----
    I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
    -- Schemer

    Note: All code is untested, unless otherwise stated

      Thanks for the response.
      I have 2 questions now regarding this.

      1. Can u please point me to a link/doc where i can go through this?
      2. Suppose i have a perl module, can i just add some more routines to it just as in a perl file (also export those routines if needed)?

      thanks.

        perlmod is probably the doc you want.

        For your second question, you just need to add the subroutine and then add it to the list of symbols to export.

        ----
        I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
        -- Schemer

        Note: All code is untested, unless otherwise stated

Re: building modules
by monkey_boy (Priest) on Jun 09, 2003 at 15:17 UTC
    At the bare, bare minimum, call it .pm & put 1; at the end. Then you can put this in your scripts:
    use lib "/path_to_my_module/"; use my_module;
    this will give you access to all the subs.
Re: building modules
by gellyfish (Monsignor) on Jun 09, 2003 at 15:17 UTC
    Aside from the other excellent advice, you might want to take a quick look at the program pl2pm that comes with Perl. Admittedly this only takes your perl library file, gives it a package declaration and adds the Exporter stuff and then saves it as a .pm file with the first letter capitalised - but then again if you have only a simple library that might be all that you need to do :-) Of course this isn't a substitute to reading the documentation that you have been pointed at.
    /J\