Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Getting perl file path from Function

by clintonm9 (Sexton)
on Oct 04, 2010 at 04:12 UTC ( [id://863240]=perlquestion: print w/replies, xml ) Need Help??

clintonm9 has asked for the wisdom of the Perl Monks concerning the following question:

I have a perl function and I need to figure out which script file path this function is in. I do not want to call the perl function to get the script file path. Is there anyway to do this with perl code?

Example: test.pl

#!/usr/bin/perl sub test { print "test\n"; } 1;
main.pl
#!/usr/bin/perl require 'test.pl'; # what file is &test() from? I would like to see test.pl?
Thanks for your help

Replies are listed 'Best First'.
Re: Getting perl file path from Function
by eyepopslikeamosquito (Archbishop) on Oct 04, 2010 at 04:38 UTC

    Yes, modules are preferable. Code like:

    require 'test.pl';
    shocks me back to the mid 1990s, Perl 4-style, about fifteen years behind the curve. For both modules and your old style require 'test.pl', try dumping the %INC hash. For example:
    use strict; use warnings; use Data::Dumper; require 'test.pl'; # old style use MyTest; # module (preferred) print Dumper(\%INC); # show PATHs of all files loaded via do, use, r +equire
    See also the core FindBin module.

Re: Getting perl file path from Function
by Anonymous Monk on Oct 04, 2010 at 04:29 UTC
    Please use modules, its just easier (see Simple Module Tutorial)
    $ echo sub crap { ' crap '; } 1; >crap.pl $ perl -MDevel::Peek -e" require 'crap.pl' ; Dump(\&crap )" SV = RV(0x3e8cb8) at 0x3e8cac REFCNT = 1 FLAGS = (TEMP,ROK) RV = 0x3e8cbc SV = PVCV(0x9b50b4) at 0x3e8cbc REFCNT = 2 FLAGS = () COMP_STASH = 0x3e8acc "main" START = 0x9b25f4 ===> 0 ROOT = 0x9b25b8 GVGV::GV = 0x98a484 "main" :: "crap" FILE = "crap.pl" DEPTH = 0 FLAGS = 0x0 OUTSIDE_SEQ = 96 PADLIST = 0x98a4b4 PADNAME = 0x3e8c8c(0x9961c4) PAD = 0x98a4f4(0x9abc2c) OUTSIDE = 0x3e8abc (UNIQUE) $
      I just saw this post. I tried this and it returned the wrong file path. It wasnt the script that the function was in.
      SV = RV(0x1ee3f430) at 0x1ee8f8c0 REFCNT = 1 FLAGS = (TEMP,ROK) RV = 0x1ee8fbc0 SV = PVCV(0x1ee66a10) at 0x1ee8fbc0 REFCNT = 2 FLAGS = () IV = 0 NV = 0 COMP_STASH = 0x1a6ad180 "main" ROOT = 0x0 XSUB = 0x0 XSUBANY = 0 GVGV::GV = 0x1edf27a0 "main" :: "MODULES_ORG_PERSONAL_SETTIN +GS_userManager_emergencyContacts" FILE = "/home/website/API/APIScripts/Admin.pl" DEPTH = 0 FLAGS = 0x0 OUTSIDE_SEQ = 40228 PADLIST = 0x1ee8fb90 PADNAME = 0x1ee7c6a0(0x0) PAD = 0x1ee7c6b0(0x1f042fc0) OUTSIDE = 0x0 (null)
      This function is not in Admin.pl
        You are correct. This does work! I was using the wrong Namespace. Now the only issues is it prints using warn or something. Can i retrun the data with Dump?
Re: Getting perl file path from Function
by mjscott2702 (Pilgrim) on Oct 04, 2010 at 08:16 UTC
    I asked something similar, related to modules - see node Get path to module

    As previous responders stated, the use of modules in such a case will make your life easier here - and it's not a big jump to turn your script into a module.

Re: Getting perl file path from Function
by clintonm9 (Sexton) on Oct 04, 2010 at 13:41 UTC
    I am trying to get the script path not from the loaded modules but from a function name. the only thing i have is the module is called &test(). Can i take that and get the file path?

    The problem is this is a legacy system I am trying to add something to. So switching to modules is probably not going to happen.

    But if i wanted to take on the task in the leggacy code it has die { require'/....'; }; can you do that with loaded modules? and catch the error with $@?

      See require for information on require and caller for information on how/where a function was called.

        The only issues is I am not calling the sub. So can i use caller to get the information i need? See an example below:
        my ($package, $filename, $line) = caller(\&MODULES_ORG_PERSONAL_SETTIN +GS_userManager_emergencyContacts);

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://863240]
Approved by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (4)
As of 2024-04-19 13:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found