develooper Front page | perl.perl5.porters | Postings from May 2008

Re: thoughts about overloading method calls

Thread Previous | Thread Next
Ævar Arnfjörð Bjarmason
May 6, 2008 12:08
Re: thoughts about overloading method calls
Message ID:
On Tue, May 6, 2008 at 3:36 PM, Ricardo SIGNES
<> wrote:
>  What if, instead, I could overload method calls on an object?

One of the first things I /wanted/ to do to the core but never found
the time to implement was the blessing of refs into CODE instead of
packages, that is:

my $obj = bless [] => sub {
    my ($self, $name, @arg) = @_;
    warn ref($self) . " $name @arg";

$obj->moo(qw(foo boo))
"ARRAY moo foo boo at line X"
$obj->x(qw(y z));
"ARRAY x y z at line X"

a) I don't think the idea is stupid if I understand it correctly,
would the alternate way I demonstrated accomplish what you want to do
as well?

b) Implementing /my/ way of doing it looked difficult, the way I
thought of doing it was changing everything that uses a stash now to
using a refcounted SV* which would be a huge task that would break all
sorts of things. But if I was going to do it now that I'm somewhat the
wiser in internals I'd look at the method dispatch mechanism, maybe
attach the CODE as a new kind of magic and put some stub in the stash.

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About