A do-file location: how the code inside that do-file find it?

  • Thread starter S P Arif Sahari Wibowo
  • Start date
S

S P Arif Sahari Wibowo

Hi!

Let's say script A call a subscript B either by do, require, or
use. Now the code inside subscript B need to know what the file
location of subscript B. How this can be done?

The perldoc page of do mentioned that perl will keep track the
file name (e.g. for error reporting), how to get access to this
file name recorded by perl?

Thanks!
 
G

Gunnar Hjalmarsson

[ followup set to comp.lang.perl.misc ]
Let's say script A call a subscript B either by do, require, or use. Now
the code inside subscript B need to know what the file location of
subscript B. How this can be done?

The perldoc page of do mentioned that perl will keep track the file name
(e.g. for error reporting), how to get access to this file name recorded
by perl?

Read about the $0 variable in "perldoc perlvar".
 
H

h3xx

Hi!

Let's say script A call a subscript B either by do, require, or
use. Now the code inside subscript B need to know what the file
location of subscript B. How this can be done?

The perldoc page of do mentioned that perl will keep track the
file name (e.g. for error reporting), how to get access to this
file name recorded by perl?

Thanks!

--
(stephan paul) Arif Sahari Wibowo
_____ _____ _____ _____
/____ /____/ /____/ /____
_____/ / / / _____/ http://www.arifsaha.com/

Looks like the %INC hash is the key. For instance, let's say you have
a perl module with extension `.pm' somewhere on Per's include path:

package foo;

sub where_am_i {
%INC{'foo.pm'}
}

Then calling &foo::where_am_i should produce the absolute file name
where Perl [first] found the file containing the where_am_i() sub.

For individual scripts, it's a bit easier:

#!/usr/bin/perl -w
print "I'm being called from $0\n";
 
P

Peter J. Holzer

[ followup set to comp.lang.perl.misc ]
Let's say script A call a subscript B either by do, require, or use. Now
the code inside subscript B need to know what the file location of
subscript B. How this can be done?

Why does it need to know that?

Read about the $0 variable in "perldoc perlvar".

%INC seems to be more like what the OP wants.

hp
 
G

Gunnar Hjalmarsson

Peter said:
[ followup set to comp.lang.perl.misc ]
Let's say script A call a subscript B either by do, require, or use. Now
the code inside subscript B need to know what the file location of
subscript B. How this can be done?

Why does it need to know that?
Read about the $0 variable in "perldoc perlvar".

%INC seems to be more like what the OP wants.

You are right; I replied too hasty. Thanks for pointing it out!
 
S

S P Arif Sahari Wibowo

Read about the $0 variable in "perldoc perlvar".

Sorry you are mistaken. I know about $0, it give the location of
the main script. It won't give the location of the subscript the
main script called / include using do / require / use.

Thanks anyway.
 
S

S P Arif Sahari Wibowo

cancel Re: A do-file location: how the code inside that do-file find it?
 
S

S P Arif Sahari Wibowo

sub where_am_i {
%INC{'foo.pm'}
}

Yes, I am thinking about %INC as well, but this require the
knowledge about the file name. What it the file name changed
(especially in subscript called by do)? What if more than one
file with the same name with different location are called
(e.g. using do)?

--
(stephan paul) Arif Sahari Wibowo
_____ _____ _____ _____
/____ /____/ /____/ /____
_____/ / / / _____/ http://www.arifsaha.com/

Disclaimer: IANAL, IANALP, IANAMD, IANAMP, IANAAP
my statements - if any - should be treated as such.
 
J

Joost Diepenmaat

S P Arif Sahari Wibowo said:
Hi!

Let's say script A call a subscript B either by do, require, or
use. Now the code inside subscript B need to know what the file
location of subscript B. How this can be done?

The perldoc page of do mentioned that perl will keep track the file
name (e.g. for error reporting), how to get access to this file name
recorded by perl?

__FILE__ is the current filename, __LINE__ is the current line,
__PACKAGE__ is the current package.

See perldata.

Joost.
 
C

comp.llang.perl.moderated

Hi!

Let's say script A call a subscript B either by
do, require, or use. Now the code inside
subscript B need to know what the
** file location of subscript B ***.
How this can be done? The perldoc page of do
mentioned that perl will keep track the
file name (e.g. for error reporting), how to
get access to this
** file name ** recorded by perl?

Did you need the script's "file location"... or "file name"... because
you mention both.

I would have thought 'FindBin' (perldoc FindBin) would have been the
best answer if the former.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top