Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: how to find the module of a specific function?

by kcott (Bishop)
on Oct 26, 2017 at 19:55 UTC ( #1202090=note: print w/replies, xml ) Need Help??


in reply to how to find the module of a specific function?

G'day rumpumpel1,

"how can I systematically find the module, in which the login function appears?"

In whatever OO hierarchy you have, it's entirely possible that any method could be present in more than one class. I'm going to assume you mean the class where the method is found first according to whatever method resolution order you're using. See "mro - Method Resolution Order" for more about that; and for any questions you might have regarding how I've used that pragma in the following code.

#!/usr/bin/env perl use 5.010; use strict; use warnings; package A; sub new { bless {} => $_[0] } sub seen_once { 1 } sub seen_twice { 1 } sub seen_thrice { 1 } package A::B; use parent -norequire => 'A'; sub seen_twice { 1 } sub seen_thrice { 1 } package A::B::C; use parent -norequire => 'A::B'; sub seen_thrice { 1 } package main; use mro; my $instantiation_class = 'A::B::C'; my @methods = qw{seen_once seen_twice seen_thrice seen_never}; my $isa = mro::get_linear_isa($instantiation_class); my %found; say "Instantiation class: $instantiation_class"; my $obj = $instantiation_class->new; say 'Objects blessed into class: ', ref $obj; say 'Method Resolution:'; METHOD: for my $method (@methods) { next METHOD if exists $found{$method}; CLASS: for my $class (@$isa) { { no strict 'refs'; next CLASS unless exists ${$class . '::'}{$method}; } ++$found{$method}; say "$method()\tfound first in $class"; last CLASS; } unless (exists $found{$method}) { say "$method()\tNOT found in OO hierarchy!"; } }

Output:

Instantiation class: A::B::C Objects blessed into class: A::B::C Method Resolution: seen_once() found first in A seen_twice() found first in A::B seen_thrice() found first in A::B::C seen_never() NOT found in OO hierarchy!

Update: After posting I thought the "Objects blessed into class: A" looked a bit odd; then realised I'd screwed up the constructor: "sub new { bless {} => __PACKAGE__ }". I've fixed that (now: sub new { bless {} => $_[0] }); after rerunning, the output now has "Objects blessed into class: A::B::C" (which looks somewhat saner). None of the other code or output has changed.

— Ken

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1202090]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (2)
As of 2022-05-23 03:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (81 votes). Check out past polls.

    Notices?