Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

java/c has main - perl has ?

by pmc2 (Acolyte)
on Mar 31, 2003 at 23:38 UTC ( #247101=perlquestion: print w/replies, xml ) Need Help??
pmc2 has asked for the wisdom of the Perl Monks concerning the following question:

I have a perl module that I want to execute directly from the command line like this:
./ clone_db arg1 arg2 arg3
I also want to use it like a standard perl module.
use DBUtil; clone_db($arg1,$arg2,$arg2);
The problem is if there is code to handle a commandline interface in the module it will be executed when the module is pulled in as a standard perl module. In java or c I'd put it in a "main" function. Where do I put it in perl?

Replies are listed 'Best First'.
Re: java/c has main - perl has ?
by tachyon (Chancellor) on Mar 31, 2003 at 23:57 UTC

    You can do it like this using -M to load the module and -e to execute whatever is inside the ' ' . In this case we call clone with the args:

    perl -MDBUtil -e 'clone(arg1 arg2 arg3)' or perl -MDBUtil -e 'clone(@ARGV)' arg1 arg2 arg3

    Another option is to add something like this to your module:

    #!/usr/bin/perl package DBUtil; # run main if called with command line args # will not execute when called as a module (no cmd line args) main() if @ARGV; sub main { # blah }

    More typically you would write a 3 line script that uses your module like this:

    #!/usr/bin/perl use DBUtil; clone(@ARGV)

    Then you just call the script. This is analagous to the command line version and is probably the more conventional way to do it.




Re: java/c has main - perl has ?
by hv (Parson) on Apr 01, 2003 at 00:37 UTC

    When a module is used or required, the user (or requirer) is set up as a caller, accessible using caller. You can take advantage of that like so:

    unless (defined caller) { # run as command line script ... } 1;

Re: java/c has main - perl has ?
by BrowserUk (Pope) on Apr 01, 2003 at 01:34 UTC

    I do this all the time, mostly as a way of incorporating unit test code into my modules, but I've seen no good reasons why it shouldn't be done for other purposes. I do it like this.

    package My::Module; ... return 1 if caller; #! perl -slw package main; use My::Module; my $thing = My::Module->new(); # Do My::Thing:)

    Use the module in the normal way in other programs, or invoke it using perl -x libpath/My/ to run the embedded program. The -x is only needed if you want any shebang line switches to be processed, omit it and put your switches on the command line if you prefer.

    Examine what is said, not who speaks.
    1) When a distinguished but elderly scientist states that something is possible, he is almost certainly right. When he states that something is impossible, he is very probably wrong.
    2) The only way of discovering the limits of the possible is to venture a little way past them into the impossible
    3) Any sufficiently advanced technology is indistinguishable from magic.
    Arthur C. Clarke.
Re: java/c has main - perl has ?
by cees (Curate) on Apr 01, 2003 at 00:01 UTC

    You could go the route that uses:

    perl -MCPAN -e shell

    Then just write a 'shell' sub that does what you want and export it by default.

    If that is no good, you could possibly look at $0 to see if you are being invoked as ./ or being required by another program or module. I have never tried that but it might work...

Re: java/c has main - perl has ?
by VSarkiss (Monsignor) on Apr 01, 2003 at 03:04 UTC

    For a good example of this, look at the diagnostics module, which may also be called as the standalone program splain. (It 'splains what the error messages mean, get it?)

    There's a bunch of stuff in there, but pay attention to how the variable $standalone is set and used. On top of which, the code's funny in several places....

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://247101]
Approved by blokhead
[erix]: job hunting is an annoying situation to be in. Of course, once settled somewhere you're going to want out again...
[erix]: but that doesn't make the searching less anoying
[Tanktalus]: I've been looking for over 12 months :(

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (11)
As of 2017-05-23 19:07 GMT
Find Nodes?
    Voting Booth?