As far as I can tell, $0 will give you something to work
with and from there you can figure out the rest. Based on my
limited testing:
- If the program was executed based on your $PATH, $0
will include the full pathname.
- If the program was executed explicitly, either by being
in the directory, or including a relative path, $0 is this
relative path
I think Perl is doing some fancy stuff to help you out there.
After all, things often show up in the process table as
having a "$0" of "perl foo" or "perl /path/to/foo",
which correlates to the above two scenarios.
In the event $0 does not include the full path for you,
you can always "guess" which one was called by going through
your $ENV{PATH} and testing each one:
my $my_path = $0;
foreach my $path (split(/:/, $ENV{PATH}))
{
$my_path = "$path/$0" if (-f "$path/$0");
}
Either way, I think the functionality you are looking for
is something like:
my ($called_name) = $0 =~ m#/([^/]+)$#;
{
no strict;
&{"mode_$called_name"}() if (defined &{"mode_$called_name"});
}
sub mode_foo
{
# If program is called as "foo"
}
sub mode_moo
{
# If program is called as "moo"
}
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.