dcorbin has asked for the wisdom of the Perl Monks concerning the following question:

I've developed a module that works great. However, I'd like to be able to execute this module directly, and have it run code that is NOT executed when it is loaded in a Use or require statement. Is this possible? How? many thanks.
  • Comment on It's a Module. It's a program. Can it be?

Replies are listed 'Best First'.
Re: It's a Module. It's a program. Can it be?
by bbfu (Curate) on Apr 01, 2001 at 06:24 UTC

    Yes, you can.

    package MyPackage; $is_require = (caller(0))[7]; if($is_require) { print "I'm a module!\n"; } else { print "I'm a program!\n"; }

    caller rocks.

    bbfu
    Seasons don't fear The Reaper.
    Nor do the wind, the sun, and the rain.
    We can be like they are.

Re (tilly) 1: It's a Module. It's a program. Can it be?
by tilly (Archbishop) on Apr 01, 2001 at 07:11 UTC
    if (__FILE__ eq $0) { print __FILE__ . " is being run\n"; } else { print __FILE__ . " is being loaded\n"; }
Re: It's a Module. It's a program. Can it be?
by Anonymous Monk on Apr 01, 2001 at 04:49 UTC
    Why can't you write a normal program which use's the module and runs this special code?
Re: It's a Module. It's a program. Can it be?
by extremely (Priest) on Apr 02, 2001 at 00:45 UTC
    You might want to look at how CPAN is set up and just call a master "method" when you want it to run as a program. Ala: perl -MYourModule -e YourMethod

    --
    $you = new YOU;
    honk() if $you->love(perl)