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

Discussion in 'Perl Misc' started by S P Arif Sahari Wibowo, Jan 20, 2008.

  1. 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/
     
    S P Arif Sahari Wibowo, Jan 20, 2008
    #1
    1. Advertising

  2. [ 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
     
    Gunnar Hjalmarsson, Jan 20, 2008
    #2
    1. Advertising

  3. S P Arif Sahari Wibowo

    h3xx Guest

    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";
     
    h3xx, Jan 20, 2008
    #3
  4. 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
     
    Peter J. Holzer, Jan 20, 2008
    #4
  5. 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
     
    Gunnar Hjalmarsson, Jan 20, 2008
    #5
  6. 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/
     
    S P Arif Sahari Wibowo, Jan 20, 2008
    #6
  7. cancel Re: A do-file location: how the code inside that do-file findit?

    cancel Re: A do-file location: how the code inside that do-file find it?
     
    S P Arif Sahari Wibowo, Jan 20, 2008
    #7
  8. 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.
     
    S P Arif Sahari Wibowo, Jan 20, 2008
    #8
  9. On Sun, 20 Jan 2008, Abigail wrote:
    > say __FILE__;


    Exactly what I need! Thanks!

    --
    (stephan paul) Arif Sahari Wibowo
    _____ _____ _____ _____
    /____ /____/ /____/ /____
    _____/ / / / _____/ http://www.arifsaha.com/
     
    S P Arif Sahari Wibowo, Jan 20, 2008
    #9
  10. 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.
     
    Joost Diepenmaat, Jan 21, 2008
    #10
  11. 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
     
    comp.llang.perl.moderated, Jan 21, 2008
    #11
  12. 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^<R<Y]*YB='
    ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
    256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
     
    Michele Dondi, Jan 22, 2008
    #12
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Luke Dalessandro
    Replies:
    0
    Views:
    629
    Luke Dalessandro
    Jan 15, 2006
  2. keithb
    Replies:
    1
    Views:
    947
    Bruce Barker
    Mar 29, 2006
  3. zxo102
    Replies:
    0
    Views:
    439
    zxo102
    Aug 1, 2006
  4. saiho.yuen
    Replies:
    3
    Views:
    472
    kaeli
    Sep 14, 2004
  5. John Bailo

    IE7 shows location even if location=no

    John Bailo, Jan 19, 2007, in forum: Javascript
    Replies:
    4
    Views:
    209
    OmegaJunior
    Mar 11, 2007
Loading...

Share This Page