Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Subroutines loaded in from other files

by Valerie170 (Initiate)
on Nov 28, 2002 at 15:53 UTC ( #216327=perlquestion: print w/replies, xml ) Need Help??

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

Hi all,
I currently have a PERL script which is basically a group of sub-routines. All of these sub routines are then called by a single "main" sub-routine. However, I wish to split these sub-routines so that they are each contained within their own file and then loaded to the main script. Can anyone give me a simple explanation of the steps necessary in order to do this?

Thanx in advance,

update (broquaint): added formatting + title change (was Subroutines loaded in from ohter files)

Replies are listed 'Best First'.
Re: Subroutines loaded in from ohter files
by tune (Curate) on Nov 28, 2002 at 15:58 UTC
    The easiest way is to use the "require" directive. Others will recommend using packages, but you can still go with the less painful solution of "require". A file should like this (eg.
    #!/usr/bin/perl sub routine1 { # this routine's code here } sub routine2 { # another routine's code } 1;
    Note the "1;" at the bottom. In the main file you need the following line somewhere at the beginning:
    require "";


Re: Subroutines loaded in from ohter files
by Abigail-II (Bishop) on Nov 28, 2002 at 16:01 UTC
    man perlmod, especially the section named Perl Modules.


Re: Subroutines loaded in from ohter files
by helgi (Hermit) on Nov 28, 2002 at 16:12 UTC
    While I would recommend bundling more than one related subroutine in each file for easier bookkeeping, here is a simple way:

    Make a file named in '/usr/home/you/modules'. This one should contain something like:

    package Module; require Exporter; @ISA = Exporter; @EXPORT = qw(dostuff); sub dostuff { my ($dostuff) = @_; return $dostuff } 1; __END__
    One of those for each subroutine. Obviously you must substitute your own names and code. That 1; at the end is required!

    You then call them by adding:

    use lib '/usr/home/you/modules'; use Module qw(dostuff);

    to your main program.

    I disagree with using a 'main' subroutine. It is un-Perlish and serves no purpose.

    Helgi Briem
    helgi AT decode DOT is

      I'm not using a main function either, but consider doing so in the future since it may help trap scoping errors and eliminates global variables which is always a Good Thing(tm).

      Just my 2 cents, -gjb-

Re: Subroutines loaded in from ohter files
by hawtin (Prior) on Nov 28, 2002 at 16:16 UTC

    Why do you want to split the subroutines into files? If you want to "encapsulate" the elements (to ensure that, for example, global variables don't "leak" between routines) you can do that within a single file by putting things in blocks:

    use strict; use vars ('$really_global_var'); { # First block, the main routine my($var_in_main); main(); exit(0); sub main { SubOne::sub1(); ... } } { # Second block for the first set of things # Can also put it in its own package for further # clarity package SubOne; my($sub1_var); sub sub1 { $::really_global_var = 42; $sub1_var = 23; } }

    If you require a whole load of files then it makes it harder to ship. Of course if you have a number of scripts that each use subroutines then putting everything in one file won't work.

Re: Subroutines loaded in from other files
by Cody Pendant (Prior) on Nov 28, 2002 at 22:34 UTC
    I don't have an answer, but I do have a question -- is this regarded as a sensible way to code Perl? (or even PERL?). Is it the fashionable way?

    I've seen this approach a few times, and it makes for increased modularity I guess, but I don't love it.

    What are the monks thoughts on this approach.

    ($_='jjjuuusssttt annootthheer pppeeerrrlll haaaccckkeer')=~y/a-z//s;print;

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://216327]
Approved by adrianh
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (3)
As of 2022-05-26 02:30 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (92 votes). Check out past polls.