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

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

am 20.01.2008 06:32:06 von 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!

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

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

am 20.01.2008 07:28:22 von Gunnar Hjalmarsson

[ followup set to comp.lang.perl.misc ]

S P Arif Sahari Wibowo wrote:
> 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".

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

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

am 20.01.2008 07:28:22 von Gunnar Hjalmarsson

[ followup set to comp.lang.perl.misc ]

S P Arif Sahari Wibowo wrote:
> 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".

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

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

am 20.01.2008 07:52:43 von h3xx

On Jan 19, 11:32 pm, S P Arif Sahari Wibowo
wrote:
> 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";

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

am 20.01.2008 12:50:43 von hjp-usenet2

On 2008-01-20 06:28, Gunnar Hjalmarsson wrote:
> [ followup set to comp.lang.perl.misc ]
>
> S P Arif Sahari Wibowo wrote:
>> 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?


>> 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".

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

hp

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

am 20.01.2008 13:09:34 von Gunnar Hjalmarsson

Peter J. Holzer wrote:
> On 2008-01-20 06:28, Gunnar Hjalmarsson wrote:
>> [ followup set to comp.lang.perl.misc ]
>>
>> S P Arif Sahari Wibowo wrote:
>>> 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?
>
>>> 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".
>
> %INC seems to be more like what the OP wants.

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

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

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

am 20.01.2008 13:11:50 von Abigail

_
S P Arif Sahari Wibowo (arifsaha@yahoo.com) wrote on VCCLV September
MCMXCIII in :
<> 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?

say __FILE__;



Abigail
--
# Perl 5.6.0 broke this.
%0=map{reverse+chop,$_}ABC,ACB,BAC,BCA,CAB,CBA;$_=shift().AC ;1while+s/(\d+)((.)
(.))/($0=$1-1)?"$0$3$0{$2}1$2$0$0{$2}$4":"$3 => $4\n"/xeg;print#Towers of Hanoi

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

am 20.01.2008 13:11:50 von Abigail

_
S P Arif Sahari Wibowo (arifsaha@yahoo.com) wrote on VCCLV September
MCMXCIII in :
<> 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?

say __FILE__;



Abigail
--
# Perl 5.6.0 broke this.
%0=map{reverse+chop,$_}ABC,ACB,BAC,BCA,CAB,CBA;$_=shift().AC ;1while+s/(\d+)((.)
(.))/($0=$1-1)?"$0$3$0{$2}1$2$0$0{$2}$4":"$3 => $4\n"/xeg;print#Towers of Hanoi

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

am 20.01.2008 18:27:07 von S P Arif Sahari Wibowo

On Sun, 20 Jan 2008, Gunnar Hjalmarsson wrote:
> 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.

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

cancel Re: A do-file location: how the code inside that do-file findit?

am 20.01.2008 18:35:37 von S P Arif Sahari Wibowo

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

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

am 20.01.2008 18:52:04 von S P Arif Sahari Wibowo

On Sat, 19 Jan 2008, h3xx wrote:
> 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.

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

am 20.01.2008 18:52:04 von S P Arif Sahari Wibowo

On Sat, 19 Jan 2008, h3xx wrote:
> 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.

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

am 20.01.2008 18:54:47 von S P Arif Sahari Wibowo

On Sun, 20 Jan 2008, Abigail wrote:
> say __FILE__;

Exactly what I need! Thanks!

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

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

am 20.01.2008 18:54:47 von S P Arif Sahari Wibowo

On Sun, 20 Jan 2008, Abigail wrote:
> say __FILE__;

Exactly what I need! Thanks!

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

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

am 21.01.2008 13:26:01 von Joost Diepenmaat

S P Arif Sahari Wibowo writes:

> 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.

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

am 22.01.2008 00:02:46 von Charles DeRykus

On Jan 19, 9:32 pm, S P Arif Sahari Wibowo wrote:
> 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.

--
Charles DeRykus

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

am 22.01.2008 12:39:00 von Michele Dondi

On Mon, 21 Jan 2008 15:02:46 -0800 (PST), "comp.llang.perl.moderated"
wrote:

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

OTOH FindBin is somewhat bugged.


Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^ ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER 256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,

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

am 22.01.2008 12:39:00 von Michele Dondi

On Mon, 21 Jan 2008 15:02:46 -0800 (PST), "comp.llang.perl.moderated"
wrote:

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

OTOH FindBin is somewhat bugged.


Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^ ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER 256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,

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

am 29.01.2008 14:04:42 von Steven Hirsch

Michele Dondi wrote:
> On Mon, 21 Jan 2008 15:02:46 -0800 (PST), "comp.llang.perl.moderated"
> wrote:
>
>> I would have thought 'FindBin' (perldoc FindBin) would have been the
>> best answer if the former.
>
> OTOH FindBin is somewhat bugged.

For _real_ entertainment, try FindBin when the script is about 8 levels deep
in a large AFS filesystem.

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

am 29.01.2008 14:04:42 von Steven Hirsch

Michele Dondi wrote:
> On Mon, 21 Jan 2008 15:02:46 -0800 (PST), "comp.llang.perl.moderated"
> wrote:
>
>> I would have thought 'FindBin' (perldoc FindBin) would have been the
>> best answer if the former.
>
> OTOH FindBin is somewhat bugged.

For _real_ entertainment, try FindBin when the script is about 8 levels deep
in a large AFS filesystem.