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

Hi, I have some programme, of about 150 lines, and I want to use this programme in another programme of some 200 lines. Can I define previous programme as subroutine and then use in later one... How to do it, can any one tell me ??? Thanks.

Replies are listed 'Best First'.
Re: Defining a module .....
by ColtsFoot (Chaplain) on May 12, 2001 at 10:52 UTC
    Try something along the lines of this:-
    Create a file say called Utils.pm
    Package Utils; @EXPORT = qw(sub1 sub2); sub sub1 { Code for sub1 here return; } sub sub2 { Code for sub2 here return; }
    Then when you want to use the module do the following:-
    #!/usr/bin/perl -w use strict; BEGIN {push @INC, qq(/path/to/module/Utils);} use Utils;
    You can then reference the subroutines as follows:-
    &Utils::sub1(); &Utils::sub2();
    Hope this makes sence.
      Using @EXPORT and @EXPORT_OK (etc.) are only needed and useful if you're also actually exporting symbols, often with Exporter.pm.

      Also, use lib "path" is generally preferred over manually push()ing to @INC.

      japhy -- Perl and Regex Hacker
Re: Defining a module .....
by Chady (Priest) on May 12, 2001 at 10:49 UTC

    you could read about modules. and if you just want your subroutines.. you can structure like this:

    # file1.pl #this file contains your subroutines... # end your file with a return true.. or a true value.. 1; # this shoud do. ############ # File2.pl # in the beginning of your file BEGIN { require './file1.pl'; } # this will get what you want...
    Just be sure to check the link for creating modules.


    He who asks will be a fool for five minutes, but he who doesn't ask will remain a fool for life.

    Chady | http://chady.net/