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 ;-)

In reply to Running a module? by bschmer

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.