Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Calling undefined method

by shyju (Initiate)
on Dec 15, 2005 at 10:03 UTC ( #516903=perlquestion: print w/ replies, xml ) Need Help??
shyju has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I have seen accessing an object thruogh a method which is not defined,like
$Q::md->getName()
which gets the value of a variable named "Name". But I couln't find any method getName() defined inside that package.
Also when I tried to demonstrate the same, it didnt worked. Please give reply to my question that, is it possible to call such a "getSomething" method without defining.
Thanksfully, Shyju Thomas

Comment on Calling undefined method
Re: Calling undefined method
by Corion (Pope) on Dec 15, 2005 at 10:11 UTC

    There are many ways of calling code that is not immediately visible:

    • If your package inherits from another package, Perl will look for methods in that other package too. Inheritance from a package foo is commonly done via use base 'foo'; or via direct manipulation of @ISA.
    • Methods can be created dynamically. Class::Accessor does that, and you can do it yourself:
      eval q{sub foo{print "Hello World"}} # or, somewhat cleaner, with Perl checking your # syntax at compile time: { no strict 'refs'; *{__PACKAGE__ . "::foo"} = sub { print "Hello World"; }; }; # ... and many more methods
    • If there is an AUTOLOAD subroutine in the package, it will be called for every undefined subroutine:sub AUTLOAD { print "You called '$AUTOLOAD(@_)' via AUTOLOAD but it doesn't exist\n" };
    • AUTOLOAD can also be inherited. This is a dangerous practice and I think Perl warns you if an inherited AUTOLOAD handler is called.

    I think this catches all common cases of creating methods, but I might have overlooked something.

      hai corion,
      Thanks for a comprehensive reply.
      I have checked it in the code I have seen, and I found out that they have used AUTOLOAD subroutine there.
      Thanksfully,
      Shyju Thomas
Re: Calling undefined method
by blazar (Canon) on Dec 15, 2005 at 14:19 UTC

    Hehe! Magic enough, isn't it? That's because Perl can create methods. Or better: you can create methods in Perl.

    Probably here you have a case in which one is being generated at runtime by means of AUTOLOAD (look it up in Perl's OO docs), but it may be inherited as well. Oh, and I'm far from being a Perl OO expert, but I suppose there may be other ways for it to pop out (appearently) out of nothing...

Re: Calling undefined method
by vennirajan (Friar) on Dec 16, 2005 at 06:28 UTC
    Hi shyju,

         We have some thing called as "AUTOLOAD". In the oops methods this helps very much in exception handling. The common error that will happen in the OOPS programming is calling the undefined function. This AUTOLOAD is used to handle that situation. Also you can use the value of the global variable "$AUTOLOAD". That variable will contain the value in the format of "Called Package::Called Function". You can use that variable to display the information to the user. For more information have a look at "perltoot, perlboot, perltot,perltoc".


    Regards,
    S.Venni Rajan.
    "A Flair For Excellence."
                    -- BK Systems.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (9)
As of 2014-09-22 17:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (198 votes), past polls