In Perl, how can I check from which module a given function was imported?
Asked Answered
T

4

23

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?

Tanana answered 10/9, 2010 at 13:26 Comment(3)
What problem are you trying to solve by knowing this? There might be a way to fix that before it happens. :)Norbert
Modifying functions from other classes may be common in Ruby, but is a black art in Perl that should be reserved for extremely rare situations. Why do you need to do this?Guertin
@'brian d foy' Three years later I'm trying to write a unit test to confirm that I correctly get a pure-Perl version of a function under one condition, or an XS version under other another condition, and suddenly I have what I feel is a legitimate use-case for this OP's quesiton. ;)Althaalthea
T
31

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.

Thrasher answered 10/9, 2010 at 13:36 Comment(1)
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.Mucky
A
12

Perl's debugger can dig down the way you want. For example:

main::(-e:1):  0
  DB<1> sub foo {}

  DB<2> x \&foo
0  CODE(0xca6898)
   -> &main::foo in (eval 5)[/usr/share/perl/5.10/perl5db.pl:638]:2-2

It does this using Devel::Peek:

=head2 C<CvGV_name_or_bust> I<coderef>

Calls L<Devel::Peek> to try to find the glob the ref lives in; returns
C<undef> if L<Devel::Peek> can't be loaded, or if C<Devel::Peek::CvGV> can't
find a glob for this ref.

Returns C<< I<package>::I<glob name> >> if the code ref is found in a glob.

=cut

sub CvGV_name_or_bust {
    my $in = shift;
    return unless ref $in;
    $in = \&$in;            # Hard reference...
    eval { require Devel::Peek; 1 } or return;
    my $gv = Devel::Peek::CvGV($in) or return;
    *$gv{PACKAGE} . '::' . *$gv{NAME};
} ## end sub CvGV_name_or_bust

You might exercise it with

#! /usr/bin/perl

use warnings;
use strict;

package Foo;

sub bar {}

package main;

BEGIN { *baz = \&Foo::bar }

sub CvGV_name_or_bust { ... }

print CvGV_name_or_bust(\&baz), "\n";

Output:

Foo::bar

Note that the example above gives Foo:bar a different name, but you get both the package where the aliased sub resides and also its name there.

Argyres answered 10/9, 2010 at 14:13 Comment(0)
S
3

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
Standstill answered 10/9, 2010 at 13:36 Comment(0)
Y
1

You can pass to Sub::Identify::sub_fullname any subroutine reference and it will show you the module where this sub was defined:

use Sub::Identify qw/sub_fullname/;
sub foo {
    print sub_fullname( \&foo );  # main::foo
    print sub_fullname( sub{} );  # main::__ANON__
}

foo();

For details see Sub::Identify

Yu answered 18/1, 2018 at 18:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.