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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hi All, I am looking for the per

Status
Not open for further replies.

refulgent

IS-IT--Management
Jun 12, 2001
24
US
Hi All,

I am looking for the perl function that will tell me the name of the file from which my current file is being called. Is there such thing at all??

Thank you in advance,

Kate
 
Are you looking for the name of the file containing the perl script which is being executed? This information is contained in the $0 variable.
 
If $0 doesn't give you what you want, check out the caller function. Try it with no parameter, and then 1, 2, etc. until you get the info you want.
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Hi Guys,

Thanks for the responces!
My problem is this:
I have two cgi (perl) files file1.cgi and file2.cgi that call another file, lets say test.cgi.
In test.pl i wanted to do this:
if it was called from file1.cgi{
...do something...
}

if it was called from file2.cgi{
..do something else..
}

However i dont know how would i retrieve the name of the file my test.cgi is called from...
Did i make my problem a little more clear?

Many thanks,

Kate
 
Try printing out the value of $0 like they said, and see if that has what you want.

If that doesn't do it, try the caller function, Without a parameter is returns a list with some basic info. WITH a parameter (a number indicating how far back in the stack to go) it returns even more info.
Code:
($package, $filename, $line) = caller;

($package, $filename, $line, $subroutine, $hasargs, $wantargs) = caller(n);
Try it with no parameter and print the returned values. If that doesn't give you what you want, try 1, then 2, etc.

Alternatively, you could pass the name of the calling file to the other file.
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Hi Tracy,

Thanks a lot! I will try to use caller...However could you please give me quick example how to use $0 .. just in case..

Many thanks,

Kate
 
$0 should contain the REAL path and filename of the current file. something like this:
Code:
/u/web/userid/cgi-local/test.pl
You might also be able to use the environment variable $ENV{'SCRIPT_FILENAME'}.
You can parse off the directory and the filename like this:
Code:
if ( $0 =~ /^(.*)\/([^\/]+)$/ ) {
   $CurrentDirectory = $1;
   $CurrentProgram = $2;
} elsif ( $ENV{'SCRIPT_FILENAME'} =~ /^(.*)\/([^\/]+)$/ ) {
   $CurrentDirectory = $1;
   $CurrentProgram = $2;
} else {
   $CurrentDirectory = '.';
   $CurrentProgram = '(unknown)';
}
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Hi Tracy,

Name of my current file is the only thing returned by $0??
But how do i retrieve name of the file from which my current file is called? Is it possible at all to do without passing it manually to my current file...?

Thanks a lot!!!

Kate
 
I think you'll have to use the caller function for that.
All I can say is, when you reach a situation like this, TRY IT OUT AND SEE WHAT HAPPENS! Print the value of $0 from your main progam and the called program and see what the value IS. Print the values you get from the caller function and see if there is anything there you can use. EXPERIMENT! That's how I learned about 80% of the stuff I know.
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Thanks Tracy!
I will play experiment with it then.
I appreciate your responce!

K
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top