Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I figure out what module is loading Moose?

I am trying to figure out which module in my CGI::Application is loading Moose. I attempted to overload "require" but I don't seem to have the syntax quite right. If someone could clean up the following code I would appreciate it:

use strict;
use warnings;
use Carp qw//;

BEGIN {
  *CORE::GLOBAL::require = sub (*) { 
    warn "Requiring $_[0] at: " . Carp::longmess();
    CORE::require (@_);
  };
}

Basically, the problem with this script is that it isn't actually loading anything. When CORE::require(@) is called is doesn't "do" anything. I tried just passing it the script name directly using $[0], but that just resulted in the script deadlocking until it timed out.

NOTE: The above script is at the beginning of my start up script

like image 660
Jeffrey Fuller Avatar asked Sep 28 '10 17:09

Jeffrey Fuller


3 Answers

Hows about:

BEGIN {
  unshift @INC, sub {
    printf "Moose first called by pkg %s at line %d in %s\n", (caller)[0,2,1]
      if $_[1] eq 'Moose.pm';
  };
}

This "works" because subroutine references in @INC are called and passed the coderef and filename as arguments. See require perldoc:

As mentioned by phaylon, you can also use Devel::TraceLoad to get a summary of all modules loaded while your application ran (and what line of what file loaded them) with

perl -MDevel::TraceLoad=summary my_progam.pl
like image 105
draegtun Avatar answered Oct 15 '22 04:10

draegtun


You can find out who is loading a particular module, by inserting a few lines at the start of the module to get the "caller". Find Moose.pm in your library tree (perl -mMoose -wle'print $INC{"Moose.pm"}', and edit it to:

package Moose;

use Data::Dumper;
warn "!!! Moose is being loaded by: ", Dumper([caller]);

This is quick and dirty, but it sounds like it's all you need. (Don't forget to unedit the changes to Moose.pm when you are done!)

like image 29
Ether Avatar answered Oct 15 '22 04:10

Ether


In *Nix:

mkdir dummy
echo > dummy/Moose.pm
perl -I./dummy /path/to/my_script.pl

The concept would be the same in Windows.

You should get:

Moose.pm did not return a true value at ...
like image 44
Axeman Avatar answered Oct 15 '22 04:10

Axeman