views:

121

answers:

3

I have a code which calls the function. But I don't know the module this function belongs to. I need it to modify this function.

How can I check it?

+1  A: 

If the function was automatically imported from another module using Exporter, it can be found in this module's @EXPORT global variable:

perl -MEncode -e 'print join "\n", @Encode::EXPORT'
decode   
decode_utf8
...   

You can provide a list of functions to use. This way you will always know which package a function belongs:

use Encode       qw[ encode ]; # encode() imported from the Encode module
use Data::Dumper qw[];         # no functions imported from Data::Dumper
eugene y
+18  A: 

The Devel::Peek module is very handy to get all sorts of information about variables. One of the things you can do with it is dump a reference to a subroutine and get the name of the glob it came from:

$  perl -MDevel::Peek -MList::Util=first -e'Dump(\&first)'
SV = IV(0x1094e20) at 0x1094e28
  REFCNT = 1
  FLAGS = (TEMP,ROK)
  RV = 0x11183b0
  SV = PVCV(0x10ff1f0) at 0x11183b0
    REFCNT = 3
    FLAGS = (POK,pPOK)
    PROTOTYPE = "&@"
    COMP_STASH = 0x0
    XSUB = 0x7f7ecbdc61b0
    XSUBANY = 0
    GVGV::GV = 0x11183c8        "List::Util" :: "first"
    FILE = "ListUtil.c"
    DEPTH = 0
    FLAGS = 0x800
    OUTSIDE_SEQ = 0
    PADLIST = 0x0
    OUTSIDE = 0x0 (null)

the GVGV::GV part in there is the important bit.

An alternative solution would be Sub::Identify, which really only gives you names for code references you hand to it. However, knowing about Devel::Peek is handy in many other situations too, so I mentioned that first.

rafl
+1, very helpful.
eugene y
This is much better than what I would have done. I would have added at `()` after each module being used until the code failed to compile.
Chas. Owens
+2  A: 
Greg Bacon