When we use other people’s Perl programs or packages, we want to see the source code of Perl subroutine.
Here is Steve’s practice to find the root of a package and a subroutine. The content is from online and is tested in my local environment.
Find Package
Save the following content into a file fine_pm, and make it executable.
Then use find_pm <module name> to find the module.
#!/usr/bin/env perl
use strict;
use warnings;
my $sub_path = shift @ARGV;
if (not defined $sub_path)
{
  print "\nERROR: No arguments supplied.\n\n";
  print "Syntax: find_pm [Perl Module]\n\n";
  exit 1;
}
my $sub_path_orig = $sub_path;
$sub_path =~ s/::/\//g;
foreach my $lib_path (@INC)
{
  my $full_path = "$lib_path/$sub_path";
  if ($full_path !~ /\.pm$/)
  {
    $full_path .= ".pm";
  }
#   print "DEBUG: $full_path\n";
  if (-e $full_path)
  {
    print "$full_path\n";
    exit 0;
  }
}
print "$sub_path_orig: module not found\n";
Find Subroutine
The following script can help to find the subroutine and print out the subroutine’s source file location and number of line.
use mypl ;
sub find_sub (\&) {
    my $code = shift;
    require B;
    my $obj = B::svref_2object($code);  # create a B::CV object from $code
    print "$code:\n";
    print "  $$_[0]: $$_[1]\n" for
        [file    => $obj->FILE],
        [line    => $obj->GV->LINE],
        [name    => $obj->GV->NAME],
        [package => $obj->STASH->NAME];
}
find_sub &::mypl::dev_test ;
        © Copyright notes
The copyright of the article belongs to the author, please do not reprint without permission.
Related posts
No comments...