In Perl, is it possible to determine if a script is being executed within another script (presumably via system or qx)?
$ cat foo.pl
print "foo";
print "\n" if not $in_qx; # or the like.
I realize this is not applicable if the script was being run via exec.
I know for certain that system runs the process as a fork and I know fork can return a value that is variable depending on whether you are in the parent or the child process. Not certain about qx.
Regardless, I’m not certain how to figure out if I’m in a forked process without actually performing a fork.
All processes are forked from another process (except init). You can sort of tell if the program was run from
open,qx//,open2, oropen3by using theisattyfunction from POSIX, but there is no good way to determine if you are being run bysystemwithout looking at the process tree, and even then it can get murky (for instancesystem "nohup", "./foo.pl"will not have the calling perl process as its parent).