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

I am not new to Perl or CGI but this is the first time I've written a script over 1,000 lines in length. This is a little long and I have one subroutine that takes up almost half of all the space.

How can I save this as another script and have the original read it as if it were the same script? I want just the sub routine in another file and have all the variables called in it work as it does now.

Any pointers?

Replies are listed 'Best First'.
Re: Running another script within CGI
by Happy-the-monk (Canon) on Oct 25, 2004 at 22:45 UTC

    Any pointers?

    Simple Module Tutorial

    But most simplistic starter for your specific task:

    1. Put only your subroutine in a file next to the script called "Foo.pm".
    2. Add a new last line to that file saying just "1;"
    3. Add a new first line to that file saying "package Foo;"
    4. Add a first line to your original script saying "use Foo;" *
    5. Call the subroutine in your original script as "Foo::name_of_that_subroutine"

    * ok, not first in this case, but right after the #!/some/path/to/perl -w - line.

    Cheers, Sören

Re: Running another script within CGI
by pg (Canon) on Oct 25, 2004 at 22:43 UTC

    OO is what you want.

    Change the way you look at it, instead of thinking to put the longest sub in a separate file, you have to take a look at what you are doing, abstract classes out of them, and put properties and methods that are tight-coupled in classes.

Re: Running another script within CGI
by hostyle (Scribe) on Oct 26, 2004 at 10:30 UTC