Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Modules as executable scripts?

by aufflick (Deacon)
on Jun 15, 2007 at 05:58 UTC ( [id://621402]=note: print w/replies, xml ) Need Help??


in reply to Modules as executable scripts?

Neat - I have a feeling I'll be doing this one day :)

One suggestion for those who feel queazy about 'magic' like this is that you can easily kick off a sub in a package with something like:

perl -MYour::Package -e run

where run is the name of a sub that requires no args. Which approach is neater is in the eye of the beholder.

I really like the comment from friedo that discussed using a similar approach to make a package that can be a standalone test script and also a base package for other tests.

Replies are listed 'Best First'.
Re^2: Modules as executable scripts?
by friedo (Prior) on Jun 15, 2007 at 06:25 UTC
    perl -MYour::Package -e run

    Note that this only works if Your::Package exports run by default, since code executed via -e runs in the main package.

      good point - completely correct.

      taking a look at the -M syntax in perlrun, a cute thing you could do is to implement (or override) import() in your package, and respond to triggers passed in:

      package DoIt; sub import { &run if( $_[0] eq 'run' ); }

      the following commandline:

      perl -MDoIt=run,arg1,arg2

      will call the DoIt::run sub with @_ set to qw(run arg1 arg2).

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://621402]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (9)
As of 2024-03-28 18:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found