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

I have a script called hello.pl and I have another script called run.pl. The run.pl will execute hello.pl and basically I want to get hello.pl to print that run.pl called it.

Does it make sense?

Basically, there are several scripts that will try to execute hello.pl but if it is coming from run.pl, I want hello.pl to do some additional stuff.

Replies are listed 'Best First'.
Re: How to Find the Script that Called it
by Joost (Canon) on Dec 18, 2008 at 00:26 UTC
    Assuming a POSIX system, you can't.

    All you can do is get the parent process id and if that process is still running find out more. And most of more is probably system dependent.

    Is there any sane reason you can't pass an argument or environment variable and use the (lack off) that to differentiate the behaviour? It would probably help testing too.

      The reason is that there are about 10 scripts that is calling the main script. So, if I try to pass an argument, then it means I have to change the 10 scripts on how it is calling the main script.

      I certainly can do that but it means I have to test the 10 scripts...
        You can set an environment variable in the one parent script that you're looking for. E.g. $ENV{RUNNING_FROM_FOO}=1; in foo.pl, then in bar.pl test if ($ENV{RUNNING_FROM_FOO}) { ... }.

        Update: I see the Joost already suggested an environment variable, but you seem to have missed it (as did I). So it's probably worth saying again anyway :-)

Re: How to Find the Script that Called it
by GrandFather (Saint) on Dec 18, 2008 at 02:33 UTC

    Sounds like you are using back ticks or do to access a script that very likely should be a module. Once it's a module it is trivial to add special case behavior for different contexts.

    Even if you insist on executing the main script rather than calling it, you can pass optional parameters on the command line in the case where you need special processing. Do that just for the unusual case and you only need to alter the special case execution code to add the new parameter.


    Perl's payment curve coincides with its learning curve.
Re: How to Find the Script that Called it
by almut (Canon) on Dec 18, 2008 at 00:55 UTC

    I'd second what Joost said.  As an example of a platform specific approach, you could do something like this on systems with a /proc file system (the example is for Linux, but it should be quite similar elsewhere):

    #!/usr/bin/perl my $ppid = getppid(); # parent PID my $procfile = "/proc/$ppid/cmdline"; open my $fh, "<", $procfile or die "Cannot open $procfile: $!"; my $cmdline = <$fh>; $cmdline =~ tr/\0/ /; close $fh; print "cmdline: $cmdline\n";

    As already said, this would require that the parent process is still running.

Re: How to Find the Script that Called it
by kennethk (Abbot) on Dec 18, 2008 at 00:24 UTC

    Assuming you don't want to explicit pass the script name, you can do what you want using caller.

    Update: as per chromatic's comment, I should have queried as to how you are calling the other script. If you are invoking the OS (through backticks, system(), etc.) then Joost is right. A simple solution in that case would be to repackage hello.pl as a package, which would then allow you to directly call the hello subroutines and use the caller method as I suggested before.

    Some sample code (obviously I don't know how your files are set up):

      Do you mean if you use do? If not, can you show an example?