Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Determine if run directly or called? 1

Status
Not open for further replies.

CJason

Programmer
Oct 13, 2004
223
US
Is there a way to determine if your perl script is being run directly:
Code:
./script.pl
or called from another script:
Code:
require("script.pl");
during runtime?

I have some scripts that can either be called directly or via a "require". The script has a return(1) in it...which is perfect for the require, but gets a "can't return outside a subroutine" warning if run directly. So, if I can determine how it is being run, I can determine if I need to "exit" or "return"...if that makes sense.
 
I think I got it...
Code:
exists($INC{"script.pl"})?return(1):exit(1);
Anyone see a better way than that?
 
If your script.pl has a "return 1;" in it, simply drop the word "return from it thus: "1;".




Trojan.
 
That is a very good idea, Trojan...let me look into that a bit. Thanks for the advice!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top