in reply to alternative way to subs
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;
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!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: alternative way to subs
by ysth (Canon) on Apr 30, 2008 at 07:43 UTC |