Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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


In reply to Re: how to find the module of a specific function? by kcott
in thread how to find the module of a specific function? by rumpumpel1

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

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



    Results (76 votes). Check out past polls.

    Notices?