In Perl how do you dynamically pick what method to use as a callback? -


In Perl, callback or code is too trivial to specify references if its package is known:

< Pre> package foo; Subfoo {print "foo" in} # and then package main; The sub-ring (my $ code = shift; $ Code- & gt; ();} Buzz (\ & amp; FU :: FU);

and in this Foo < / Code> prints.

Let's say that you have an object, sometimes such a trivial, like this:

  package fu;  

You can see the method using the above method (\ & Amp; package: method) and call it

  package main; My $ foo = Foo- & gt; ; New (); My $ ref = \ & amp; Foo :: bar; $ Foo- & gt; $ ref ();  

But sometimes (well, often) Make you clear I do not know, I know that foo , bar , Baz , and they have their own blat The way you want to get the context of the proper method depending on the object instead of the package. How can you go about it?

  my $ ref = $ obj- & gt; 'blat');  

If the $ ref is down, then your object can not blow off. If there is no $ ref, then this is a valid CODE reference for the function in the appropriate question to call "$ obj -> $ ref (@args)".


Comments

Popular posts from this blog

c++ - Linux and clipboard -

Visual Studio 2005: How to speed up builds when a VSMDI is open? -

booting ubuntu from usb using virtualbox -