Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

function availablity checking inside package

by manoj_speed (Prior)
on Apr 06, 2009 at 10:47 UTC ( #755684=perlquestion: print w/replies, xml ) Need Help??

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

I executed the following code.
#!/usr/bin/perl { package UNIVERSAL; sub AUTOLOAD { print "Calling from Universal\n"; } 1; } { package A; our $var=0; 1; } { package Bn; use base qw(A); #sub AUTOLOAD #{ # print "Calling from Package Bn\n"; #} 1; } { package C; use base qw(Bn); 1; } C->hai();

In the above code, I called the hai() function.

If I declared the function AUTOLOAD() in Bn package (as well as in universal package also), and executed the script, it prints the message as "Calling from Package Bn".

After commenting the AUTOLOAD() function(which is declared in Bn package) I executed the same program it prints the message as "Calling from Universal".

In which order perl checks the function availablity.(In which order it gets executed).

-- The wisest mind has something yet to learn.

Replies are listed 'Best First'.
Re: function availablity checking inside package
by leslie (Pilgrim) on Apr 06, 2009 at 11:01 UTC

    Hi friend,

    According to your script, First perl checks the hai() in parent package(C). If it is not available then it will check the package Bn. If it is not available there also then it will check package A. If that hai() is not available in package A then it will check the UNIVERSAL package.

    If that function is not available in Universal package then control comes to package C(parent packege). Here it will check the Autoload(), If it is not available then it will check the Autoload() in the following order as package Bn, package A , Universal. That's why your script prints "Calling from Universal".

Re: function availablity checking inside package
by vinoth.ree (Monsignor) on Apr 06, 2009 at 11:20 UTC
    Hi Manoj_speed

    If no method definition with the right name is found after searching the invocant's class and all its ancestor classes recursively, one more check for a method of that name is made in the special predefined class called UNIVERSAL. This package never appears in an @ISA, but is always consulted when an @ISA check fails. You can think of UNIVERSAL as the ultimate ancestor from which all classes implicitly derive.

    For more information have a look on this it will help you I believe


Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://755684]
Approved by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (7)
As of 2021-10-28 07:04 GMT
Find Nodes?
    Voting Booth?
    My first memorable Perl project was:

    Results (96 votes). Check out past polls.