Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling a subroutine in OOP Perl

When looking through some code I took over, I came across this line:

 my @files = My::Module::DB::raw_info->search_like(customer_handle => $config->{client}, feed => $config->{site}, arrival =>"$date")

I know that this returns an array from a package called My::Module::DB::raw_info.

What I'm not sure of (and I am just learning OOP), is what ->search_like refers to.

I didn't see that as a variable or as a subroutine in My::Module::DB::raw_info

Any hints would be appreciated. I'm only beginning to learn this stuff. It's like bathing in fire. (I know I'll be happier later though) Yikes!

like image 717
Jane Wilkie Avatar asked Oct 27 '10 14:10

Jane Wilkie


Video Answer


2 Answers

This is probably due to the method being inherited from a base class. However, in the extremely weird situations, it COULD also be injected into the module's namespace dynamically which is much harder to figure out.

You can find your sub either by brute force searching or by figuring out the base class of a module (and possibly higher up the inheritance chain) and searching just the base classes code. I will show how to do both:


Brute force search: This is probably the easiest solution in complicated cases since the sub could have been injected into the module's namespace dynamically by non-ancestor module and finding ancestor modules is not 100% easy due to multiple ways of defining inheritance that could have been used (use base, use parent, Moose stuff, AUTOLOADED stuff)

First, find out which other modules are loaded with My::Module

perl -e 'use My::Module::DB::raw_info; print "$INC{$_}\n" foreach keys %INC'

This will print the location of ALL those modules

Then, search for the sub definition in ALL that code (the following should be all one line, I split it up for readability into 2 lines):

grep search_like 
   `perl -e 'use My::Module::DB::raw_info; print "$INC{$_}\n" foreach keys %INC'`

If this returns too many results, change the grep to

grep "sub search_like"
   `perl -e 'use My::Module::DB::raw_info; print "$INC{$_}\n" foreach keys %INC'`

This will find you the definition in whichever module My::Module::DB::raw_info inherits from without actually analyzing the module code for inheritance.


Inheritance:

Find out the module's parent using ISA as follows:

perl -e 'use My::Module::DB::raw_info; print "@My::Module::DB::raw_info::ISA\n";'

To clarify, this only works for "classically inherited" modules using @ISA, not Moose stuff. It also doesn't work if the routine is called using AutoLoader or is injected into the symbol table dynamically which can happen in any code, not necessarily in the parent one.

like image 63
DVK Avatar answered Nov 12 '22 15:11

DVK


The likely cause of your conundrum is that My::Module::DB extends some other class. Look for a block along the lines of

use parent Some::Module;

or

BEGIN { extends Some::Module }

near the top of My/Module/DB.pm

Edit: As some commenters are helpfully pointing out below, there are a number of ways to subclass a Perl class, but these are probably the most common. (Maybe.)

like image 20
Williham Totland Avatar answered Nov 12 '22 13:11

Williham Totland