how to make a runnable module (or modular script) ?

how to make a runnable module (or modular script) ?

am 24.09.2007 10:45:01 von bugbear

Dear all;

In a large project, I have a requirment to use
a selection of scripts.

Each script is standalone (at the moment),
and the scripts ARE used in isolation.

Hoewever, a very frequent caller of the scripts
is a "dispatcher", which uses ITS parameters
to decide which script to call.

The dispatcher then uses "system" to call
the selected script.

Some investigation has revealed
that a large proportion of time
is going on perl startup (since the
scripts are quite quick).

Is there a "standard" way that I could create
scripts/modules/files/things that
are runnable both straight off the command
line AND as "subscripts" without
incurring a second perl startup?

I would also (strongly) like to keep
each piece of code a separate (single) file;
I would rather NOT have to make a A.pm (module) and A.pl
(script that calls the module) pair for each piece of code.

Suggestions welcomed.

BugBear

Re: how to make a runnable module (or modular script) ?

am 24.09.2007 11:33:58 von Christian Winter

bugbear wrote:
> In a large project, I have a requirment to use
> a selection of scripts.
>
> Each script is standalone (at the moment),
> and the scripts ARE used in isolation.
>
> Hoewever, a very frequent caller of the scripts
> is a "dispatcher", which uses ITS parameters
> to decide which script to call.
>
> The dispatcher then uses "system" to call
> the selected script.
>
> Some investigation has revealed
> that a large proportion of time
> is going on perl startup (since the
> scripts are quite quick).
>
> Is there a "standard" way that I could create
> scripts/modules/files/things that
> are runnable both straight off the command
> line AND as "subscripts" without
> incurring a second perl startup?
>
> I would also (strongly) like to keep
> each piece of code a separate (single) file;
> I would rather NOT have to make a A.pm (module) and A.pl
> (script that calls the module) pair for each piece of code.
>
> Suggestions welcomed.

perldoc -f do

This way your scripts get executed eval()-like by the current
Perl interpreter instance.

If your subscripts expect command line arguments, you could use
a global array that replaces the default @ARGV in the subscripts
if present, like

------------------- dispatcher --------------------------
#!/usr/bin/perl

use strict;
use warnings;

our @SUBSCRIPT_ARGS;

if( CASE_THAT_LEADS_TO_SUBSCRIPT_ONE ) {
push @SUBSCRIPT_ARGS, 'argument1';
do 'subscript1.pl' or die $!;
} elsif( ... )
...

---------------------- subscript1 ------------------------
#!/usr/bin/perl

use strict;
use warnings;

our @SUBSCRIPT_ARGS;

@ARGV = @SUBSCRIPT_ARGS if( @SUBSCRIPT_ARGS );

print "subscript1 called with argument $ARGV[0].$/";
----------------------------------------------------------

-Chris

Re: how to make a runnable module (or modular script) ?

am 24.09.2007 18:03:28 von Ben Morrow

Quoth Christian Winter :
> bugbear wrote:
> >
> > Is there a "standard" way that I could create
> > scripts/modules/files/things that
> > are runnable both straight off the command
> > line AND as "subscripts" without
> > incurring a second perl startup?
> >
> > I would also (strongly) like to keep
> > each piece of code a separate (single) file;
> > I would rather NOT have to make a A.pm (module) and A.pl
> > (script that calls the module) pair for each piece of code.
>
> perldoc -f do
>
> This way your scripts get executed eval()-like by the current
> Perl interpreter instance.
>
> If your subscripts expect command line arguments, you could use
> a global array that replaces the default @ARGV in the subscripts
> if present, like
>
> ------------------- dispatcher --------------------------
> #!/usr/bin/perl
>
> use strict;
> use warnings;
>
> our @SUBSCRIPT_ARGS;
>
> if( CASE_THAT_LEADS_TO_SUBSCRIPT_ONE ) {
> push @SUBSCRIPT_ARGS, 'argument1';
> do 'subscript1.pl' or die $!;
> } elsif( ... )

Or simply

if (...) {
local @ARGV = ...;
do 'subscript1.pl';
}

> ---------------------- subscript1 ------------------------
> #!/usr/bin/perl
>
> use strict;
> use warnings;
>
> our @SUBSCRIPT_ARGS;
>
> @ARGV = @SUBSCRIPT_ARGS if( @SUBSCRIPT_ARGS );

....then there's no need for this.

Ben

Re: how to make a runnable module (or modular script) ?

am 24.09.2007 18:22:34 von xhoster

Ben Morrow wrote:
> Quoth Christian Winter :
> > bugbear wrote:
> > >
> > > Is there a "standard" way that I could create
> > > scripts/modules/files/things that
> > > are runnable both straight off the command
> > > line AND as "subscripts" without
> > > incurring a second perl startup?
> > >
> > > I would also (strongly) like to keep
> > > each piece of code a separate (single) file;
> > > I would rather NOT have to make a A.pm (module) and A.pl
> > > (script that calls the module) pair for each piece of code.
> >
> > perldoc -f do
> >
> > This way your scripts get executed eval()-like by the current
> > Perl interpreter instance.
> >
> > If your subscripts expect command line arguments, you could use
> > a global array that replaces the default @ARGV in the subscripts
> > if present, like
> >
> > ------------------- dispatcher --------------------------
> > #!/usr/bin/perl
> >
> > use strict;
> > use warnings;
> >
> > our @SUBSCRIPT_ARGS;
> >
> > if( CASE_THAT_LEADS_TO_SUBSCRIPT_ONE ) {
> > push @SUBSCRIPT_ARGS, 'argument1';
> > do 'subscript1.pl' or die $!;
> > } elsif( ... )
>
> Or simply
>
> if (...) {
> local @ARGV = ...;
> do 'subscript1.pl';
> }


Are there any gotcha's that one should look out for in
doing this?

Thanks,

Xho

--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.

Re: how to make a runnable module (or modular script) ?

am 24.09.2007 18:24:14 von brian d foy

In article <13feu8dmesiid5d@corp.supernews.com>, bugbear
wrote:


> Is there a "standard" way that I could create
> scripts/modules/files/things that
> are runnable both straight off the command
> line AND as "subscripts" without
> incurring a second perl startup?

Turn your scripts into "modulinos", or modules that represent the
script. I talk about this in Mastering Perl's chapter on "Modules as
Programs":

http://www252.pair.com/comdog/mastering_perl/Chapters/18.mod ulinos.html

Re: how to make a runnable module (or modular script) ?

am 25.09.2007 15:40:22 von bugbear

bugbear wrote:
>
> Suggestions welcomed.

And supplied!

Thanks to all, for two, not one, but TWO
good solutions.

BugBear

Re: how to make a runnable module (or modular script) ?

am 25.09.2007 19:18:33 von brian d foy

In article <13fi3u7ef6u3dcd@corp.supernews.com>, bugbear
wrote:


> Thanks to all, for two, not one, but TWO
> good solutions.

Well, two solutions, and one of them not good. The do( '' ) solution
has some gotchas, as mentioned in its documentation in perlfunc. The
error checking for do( '' ) involves both $! and $@, and now your
solution lives in two files instead of one.

Re: how to make a runnable module (or modular script) ?

am 26.09.2007 11:49:51 von bugbear

brian d foy wrote:
> In article <13fi3u7ef6u3dcd@corp.supernews.com>, bugbear
> wrote:
>
>
>> Thanks to all, for two, not one, but TWO
>> good solutions.
>
> Well, two solutions, and one of them not good. The do( '' ) solution
> has some gotchas, as mentioned in its documentation in perlfunc. The
> error checking for do( '' ) involves both $! and $@, and now your
> solution lives in two files instead of one.

Of course it does - the "dispatcher" is one file,
and each "handler" is a normal runnable perl file.

The do solution has the merit that it can
be wrapped over a pre-existing script,
either legacy, or written by someone unaware
of the dispatcher.

Your solution is tidier, but more intrusive.

BugBear