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

In the process of finishing up Thread::Apartment (see also Apartment Threading in Perl), I realized I had completely overlooked support for operator overloading. Based on my research, I suspect it may be possible to expose operator overloading in T::A objects, using the
use overload nomethod => \&operAutoLoad;
in the client stub, but the docs are a bit sketchy about what to return if the nomethod coderef doesn't implement the specified operator; should it just return undef, or something else ? Do I need to use 'fallback => 1' with nomethod ?

Also, how do I go about introspecting a class to find its overloads ? (I need the latter so that POPOs can be handled by Thread::Apartment::Server's default introspection, which would return a list of operators which the class overloads).

Any help much appreciated.

Replies are listed 'Best First'.
Re: Operator overloading, nomethod, and Introspection
by diotalevi (Canon) on Sep 13, 2005 at 17:30 UTC

    The `nomethod' function does not have the ability to decline to handle a request. If you have implemented some of the methods, the value of `fallback' becomes relevant. The `undef' value is reasonably sane - if an unimplemented method is called, it attempts to create one from what is already implemented. If that fails, it calls your `nomethod' function. If you didn't have a `nomethod' function, it would throw an exception. Since you promised to handle everything by specifying a `nomethod' function, no exceptions will be thrown. If you really want to decline to handle a method, throw the exception yourself from inside your `nomethod' function.

    The `nomethod' function will not handle ${}, @{}, %{}, &{}, or *{} overloading. You will have to overload those manually.

    For Data::Postponed, I explicitly overloaded everything. Everything. The idea is, when I promised that the value would appear to act like a normal value, I took that and applied it to everything available. This had the side effect of not allowing me to actually store anything inside the object because now I couldn't dereference it either. I used an inside-out implementation to avoid that problem.

    The following snippet produces a list of overloaded methods.

    grep overload::Method( $some_object, $_ ), values %overload::ops
Re: Operator overloading, nomethod, and Introspection
by ikegami (Patriarch) on Sep 13, 2005 at 17:26 UTC

    If nomethod doesn't implement a specified operator, that means an operator was used on the object, but the object doesn't support it. I'd expect perl to die in that case.

    Here's an example of what happens on non-objects:

    >perl -e "%hash++"; Can't modify hash dereference in postincrement (++) at -e line 1, near + "%hash++" Execution of -e aborted due to compilation errors.

    Here's what would happen nomethod wasn't there:

    >perl -le "$r=bless({},''); print $r; printf '%x', ($r+1)-1" main=HASH(0x1abefa0) 1abefa0