Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Dual personality: Module and script

by tachyon (Chancellor)
on Aug 03, 2004 at 15:07 UTC ( [id://379682]=note: print w/replies, xml ) Need Help??


in reply to Dual personality: Module and script

As pointed out you really don't want to do this, but here is how anyway* ;-) This only works if you use the module not if you require if as when you use a module the modules import() method is called at the start of proceedings. This is not true for require when import is not called.

package MyModule; use strict; use Exporter; use vars qw($VERSION @ISA @EXPORT $MOD_CALL ); $VERSION = 1.00; @ISA = qw(Exporter); @EXPORT = qw( func1 ); sub func1 { return reverse @_ } sub import { $MOD_CALL++; } CHECK{ print $MOD_CALL ? "Called as a module\n" : "Called as a script\ +n" } 1;

On the rare occasions I have made a module a stand alone executable (usually for all the wrong laziness reasons) I have just used the presence of values in @ARGV to initiate script like behaviour. Generally you want to pass some data to a script anyway......

if ( @ARGV ) { print "I'm a script!\n"; # do stuff exit 0; } # only get to here if you we are a module

* Recall the story about getting enough rope to hang yourself?

cheers

tachyon

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2024-03-29 02:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found