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

Hello Wise and Generous Monks:

I have a very simple beginner's question.

How do I split up a PERL program? My program is approaching 800 lines and I would like to place the subroutines and functions I have written in to separate files.

many thanks

kd

Replies are listed 'Best First'.
Re: splitting a source file
by toolic (Bishop) on Oct 24, 2007 at 18:28 UTC
    I think you want to create your own Perl modules. Have you gone through this tutorial: Simple Module Tutorial?
Re: splitting a source file
by KurtSchwind (Chaplain) on Oct 24, 2007 at 19:11 UTC
    No offense to the 'write a module' folks, but we are talking about a beginner here with a program that's under 1k lines. What's wrong with some simplicity? kd, put your subs into as many files as you want and include them require
    #!/bin/perl require "bunch_o_subs.pl"; ...
    If you want to get a bit more advanced, you can go with Modules and there are certainly many advantages to them, but if you are just looking to clear the clutter up a bit and segregate the subs out, there is nothing wrong with using require.
      What's wrong with some simplicity?

      Provided all you ever want to do is stuff some subroutines someplace and use them later, not much... but you give up namespaces, compile-time bindings, custom import, path management, and a few other features. You have to go through hoops to create your own classes. You don't learn how to use other modules..

        Just because someone uses a require doesn't mean that they forfeit the user of other modules. I used modules for years before writing my own.

        And for small apps, Namespaces isn't really a large factor.

        It's easy to over-engineer applications. Many times, someone just wants to write a bit of code that does the trick on a single machine where they are the user, the developer and the maintainer. Under these circumstances, I don't see why someone who is relatively new to perl should jump into creating modules and worrying about namespaces.

        Having said that, I will concede that there is never a better alternative to good coding standards and procedures. But if you really want to get there, it can happen a bit more organically. As soon as he runs into a namespace issue he'll be looking into modules. Let it wait until then. Right now, he needs to clean up the clutter a bit.
        It's his first time, let him start with require. It'll do for now.

        Later, in a next project, or in a rewrite, he can add use, packages and all the rest... But use is just an extension of require. Let him start simple.

Re: splitting a source file
by andreas1234567 (Vicar) on Oct 24, 2007 at 18:46 UTC
    perlmodstyle attempts to describe the Perl Community's "best practice" for writing Perl modules.
    --
    Andreas
Re: splitting a source file
by Spidy (Chaplain) on Oct 24, 2007 at 18:50 UTC
    You're probably going to want to try breaking your code up into modules - check out perlmod for some information on modules.
Re: splitting a source file
by Oden (Novice) on Oct 24, 2007 at 20:40 UTC
    You can do something like that:
    ############## # Main program ############## #!/usr/bin/perl -- package Main; require "mysubs.pm"; # This is my subs Mysubs->foo(); Mysubs->test("this is a test"); ########### # mysubs.pm ########### #!/usr/bin/perl -w -- use strict; package Mysubs; sub foo { if ($_[0] eq "Mysubs") { shift @_; } ## if you call your sub from a +nother file. } sub test { if ($_[0] eq "Mysubs") { shift @_; } ## if you call your sub from an +other file. print $_[0]; }
Re: splitting a source file
by tuxz0r (Pilgrim) on Oct 24, 2007 at 19:14 UTC
    I don't think kd is talking about creating a "module", but just simply separting his subroutines from his main code block. All you need to do is put the subroutines in a separate file, say 'subs.pl', and just do
    require "subs.pl";
    Once you figure out separting routines from primary code, then you can move on to writing your own modules, packages and so forth, kd. Hope this helps.

    Update: And, almut and chromatic caught my mistake on using 'use', which was incorrect. I was mistaken and it should be the following to get compile time inclusion:

    BEGIN { do "subs.pl"; }
    Thanks for the catch, guys.
      use "subs.pl";

      Did you try it?

      #!/usr/bin/perl use strict; use warnings; use "subs.pl"; foo(); bar(); syntax error at use_subs.pl line 6, near "use "subs.pl"" Execution of use_subs.pl aborted due to compilation errors. #!/usr/bin/perl use strict; use warnings; use subs.pl; foo(); bar(); syntax error at use_subs.pl line 6, near "use subs." Execution of use_subs.pl aborted due to compilation errors.
        I'm having problems running your example on my Vista desktop. Is there anything special I need or is there a bug in your example?
      use "subs.pl";

      ... or,

      require "subs.pl";

      Actually, it must be require "subs.pl"; or do "subs.pl";  —  use "subs.pl"; is a syntax error...   </nitpick>

      (To get the compile-time effect, you can wrap the require in a BEGIN { ... } block.)