in reply to Finding out how you were called...

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: 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" }

Replies are listed 'Best First'.
Re: Re: Finding out how you were called...
by suaveant (Parson) on Aug 07, 2001 at 04:35 UTC
    Ahhh! $0 does... that's my bad for assuming argv[0] in c and $0 in perl contained the same values... is that just a perl thing, because it is a script called in the interpreter? How does perl find that data, and what is the way in C (If anyone knows... I realize this is perlmonks, not cmonks :) Thanks, tho... I want to do this in perl and C, but if it only works in perl, oh well :)

                    - Ant
                    - Some of my best work - Fish Dinner

      Don't feel bad. An entire module has sprung up based on this same misconception (and it was authored by some of the top Perl guys around). $0 is the name of the script, which Perl has to be able to open so Perl knows the correct path to it and doesn't hide it from you. argv[0] in the C code that implements Perl is "...perl" (not the script name) and may not have a full path and so $^X may not have a full path. See FindBin is broken (RE: How do I get the full path to the script executing?) for more.

              - tye (but my friends call me "Tye")
      It would seem that Perl processes its own argv[0] to make it all nice and easy for you to do what you want. This is not something that happens all by itself in C programs. C does, to a certain degree, leave you up to your own devices. So, evaluating your path for a possible match is something to consider doing, perhaps with strtok on a copy of your $PATH.