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

In my line of work, it seems that more often that not I write a script that contains functionality that would be useful to have as part of a module as well. While I could certainly put all of my useful code in a module and then write a short wrapper that would call a routine in the module to kick off processing, I think that it would be slick if I could make it all happen in the module file. For example, I have a module called checkem.pm that starts off as follows:
#!/bin/sh eval "exec /usr/software/bin/perl5.8.0 -I`pwd` -Mcheckem -e main $*" if $running_under_some_shell; package main; sub main { print "Hello from main\n"; <useful code here> }
This is all well and good, but it requires me to be in the directory where checkem.pm lives or to hardcode the path to checkem.pm in the -I argument. Neither of these is horribly appealing to me. It would be really nice if I could use the same code in each module. I was trying to do something more like:
#!/bin/sh DIR=`dirname $0` BASE=`basename $0 | sed -e 's/\.pm\$//'` eval "exec /usr/software/bin/perl5.8.0 -I$DIR -M$BASE -e main $*" if $running_under_some_shell; package main; ...
but of course Perl doesn't like the DIR= and BASE=lines. Adding if $running_under_some_shell to the DIR= and BASE= lines doesn't help either and -w didn't provide any useful insight. Any suggestions would be appreciated. It would also be nice if I didn't have to name things module.pm too ;-)

Replies are listed 'Best First'.
Re: Running a module?
by abell (Chaplain) on Sep 19, 2003 at 14:16 UTC

    Whether you have been called as a script or included as a module can be checked by taking a look at caller.

    A small example:

    #!/usr/bin/perl package mymodule; use Data::Dumper; my @caller = caller; print Dumper ( @caller ); print "Execute this\n" unless caller; 1;
    When called as perl mymodule.pm this outputs
    
    Execute this
    
    while when called as perl -Mmymodule -e '' it outputs
    
    $VAR1 = 'main';
    $VAR2 = '-e';
    $VAR3 = 0;
    

    Cheers

    Antonio

    The stupider the astronaut, the easier it is to win the trip to Vega - A. Tucket
Re: Running a module?
by Abigail-II (Bishop) on Sep 19, 2003 at 14:21 UTC
    #!/bin/sh eval "exec /opt/perl/bin/perl -I`dirname $0` -M`basename $0 .pm` -e ma +in $*" if $running_under_some_shell; package main; sub main { print "Hello from main\n"; } 1;

    That seems to work for me.

    Abigail

      Duh, must have too much water on the brain. Now if I could just get perl to load a module that doesn't end in .pm life would be perfect.
        BEGIN{ require "Foo.baz"; Foo->import(); }
        #!/bin/sh eval "exec /opt/perl/bin/perl -I`dirname $0` -e 'require q!base!; main + ()' $*" if $running_under_some_shell; package main; sub main { print "Hello from main\n"; } 1;

        Abigail

Re: Running a module?
by dtr (Scribe) on Sep 19, 2003 at 14:23 UTC

    You can use the FindBin module to get the path to the executable you are currently running.

    Try:-

    BEGIN { use FindBin qw( $BIN ); use lib qw( $BIN ); }
    Or, if that doesn't work for you:-
    #! /user/bin/perl -I . BEGIN { use FindBin qw( $BIN ); chdir $BIN; }
    might also work for you.