Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to overload/intercept function calls on overloaded objects in Perl?

In a manner similar to what is possible with overloaded operators, is there a way to apply some sort of magic to a variable so that the following:

my $magic_var = ...;

some_function($magic_var);

will result in something like this:

call_function(\&some_function, $magic_var)

If some_function was not defined, I could just use an AUTOLOAD sub to catch the call, but when it is defined, I am not aware of a way to intercept the call (without something crazy like injecting wrappers around all of the caller's subs, or temporarily undefining all the subs).

Another way to put it is I would like to write use overload sin => \&my_sin; but have sin be any function, and have it call a nomethod like sub whenever the overloaded object is used in a function call.

EDIT:

By popular request, the reason that I was looking into this was because I am working on a Perl5 version of the Perl6 whatever star. However, after looking into the Perl6 implementation more, it seems that it also does not deal with functions being called on Whatever, so it seems like I won't have to try and deal with function calls (just operators and method calls).

If anyone is interested, the module is up on CPAN: Whatever.

Feedback / Comments are welcome.

like image 233
Eric Strom Avatar asked Nov 20 '25 06:11

Eric Strom


1 Answers

Can you use the tie mechanism?

like image 131
Jonathan Leffler Avatar answered Nov 21 '25 23:11

Jonathan Leffler



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!