Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Function Identification.

by mattk (Pilgrim)
on Jul 26, 2005 at 12:48 UTC ( #478160=note: print w/replies, xml ) Need Help??


in reply to Function Identification.

Subroutine calls will be inherited differently depending on whether you're calling them as functions, or as object methods. We can test this with two different modules:
package Foo; use base 'Exporter'; @EXPORT = "func"; sub func { return "Foo" }
package Bar; use base 'Exporter'; @EXPORT = "func"; sub func { return "Bar" }

Exporter overwrites the current package's symbol table entry for func() when you use a module. So, as holli has pointed out, if two seperate modules export the same function, the most recent one will be the one that is called: when you use Foo, func() is created locally as a reference to Foo::func(), and then when Bar is imported it replaces the existing reference with one to Bar::func():
[matt@blue 478146] cat func.pl package func; use Foo; use Bar; print func(); [matt@blue 478146] perl -l func.pl Bar

OO Perl's inheritance chain works differently - it's done at run-time, left-to-right, depth first (left-to-right is referring to the contents of @ISA). So, if you're using OO modules, you will get the opposite result - a call to func() will go to the *first* module Perl finds, that provides the requested method (function):
[matt@blue 478146] cat meth.pl package meth; use base 'Foo'; use base 'Bar'; sub new { bless {}, __PACKAGE__ } $o = new meth; print $o->func; [matt@blue 478146] perl -l meth.pl Foo

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://478160]
help
Chatterbox?
[Corion]: Meh. Some Open Source people get bought out by advertising firms to change their projects to include advertising. But nobody has approached me to buy out WWW::Mechanize ::$browser from me, to make the browsers launch an ad page ...
[Corion]: ... instead of about:blank, which the modules currently do at startup.
[Corion]: Maybe I'm too pricey. But I haven't received any offers at all ;-)
[Corion]: The ad hits from CPAN testers alone should bring in a pretty penny IMO. But maybe I misestimate the CPC that ads pay.

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (11)
As of 2017-07-24 14:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I came, I saw, I ...
























    Results (354 votes). Check out past polls.