One way would be to change all the small programs into modules, and wrap all their "main" code in a subroutine in that module:

#!/usr/bin/perl -w # prog1.pl print "Hello";

would become

#!/usr/bin/perl -w # prog1.pl package Prog1; sub main { print "Hello @_"; }; main(@ARGV) if ! caller;
if ($0 eq __FILE__) {
main(@ARGV);
};

That way, you can use and run all parts from the master program and still run all parts separately as programs. This makes the migration easier:

# main program: use strict; use Prog1; my @results = Prog1::main("steph_bow"); # equivalent to 'perl -w prog1 +.pm steph_bow' ...

Of course, you will want to move some parameters from being passed as files to be passed as arguments within Perl, but those parts can go into the "main program" parts of each module.

In the long run, you want to move away from the name main and give a more sensible name, like process_names or whatever, but it's hard to give a concrete example for a generic case :)

Update: My code to detect whether a file is loaded as a module or a program failed for invocations that didn't use the bare filename. Fixed thanks to ysth!


In reply to Re: alternative way to subs by Corion
in thread alternative way to subs by steph_bow

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.