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

Hi,

How can I detect if a certain script is called via another script (included) or called directly?

Replies are listed 'Best First'.
Re: Am I included?
by Corion (Patriarch) on Mar 21, 2011 at 08:31 UTC

    See caller. If your script is at the top level of the Perl interpreter, you won't have any information on your caller.

Re: Am I included?
by ambrus (Abbot) on Mar 21, 2011 at 08:49 UTC

    if ($0 eq __FILE__) { # executed directly } else { # included via another script, such as with do or require or use }
Re: Am I included?
by raybies (Chaplain) on Mar 21, 2011 at 12:21 UTC
    Also keep in mind that Perl can read environment variables by simply accessing the %ENV hash, so if you had any control over the script calling your script, you might could stuff an environment variable prior to calling your script, that would clue you into who was calling whom, etc. Likewise if your script alters or adds any keys to the %ENV hash all child processes fork'd by that script would reflect those same ENV settings, thus allowing you to manage child processes in the same way.