Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Function Identification.

by murugu (Curate)
on Jul 26, 2005 at 11:54 UTC ( [id://478146]=perlquestion: print w/replies, xml ) Need Help??

murugu has asked for the wisdom of the Perl Monks concerning the following question:

Hi Great Monks,

Say i have two modules named A.pm and B.pm each of them containing a subroutine named 'func'. If i use both of these modules in a program and if i call &func(), which subroutine will be executed. In the mean time, im using Exporter module in both the modules A and B.

Please regret me incase if this question seems to be insane.

As always, million thanks in advance.

Regards,
Murugesan Kandasamy

Replies are listed 'Best First'.
Re: Function Identification.
by holli (Abbot) on Jul 26, 2005 at 11:57 UTC
    If i use both of these modules in a program and if i call &func(), which subroutine will be executed.
    Without having tested it, I'd say the one that loaded (used) last. To make it clear, simply call the functions with their fully qualified name: &A::func and &B::func respectively.


    holli, /regexed monk/
Re: Function Identification.
by jkeenan1 (Deacon) on Jul 26, 2005 at 12:38 UTC
    A side note:

    Even in constructing little test cases, B::ware of writing packages that begin with 'B', as they will interfere with the B:: modules included with core. Even the gods get tripped up with this one; cf. Is Conway's NEXT.pm broken?.

Re: Function Identification.
by mattk (Pilgrim) on Jul 26, 2005 at 12:48 UTC
    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
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://478146]
Approved by holli
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2024-04-19 11:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found