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

Re: thoughts about overloading method calls

Thread Previous | Thread Next
From:
Ævar Arnfjörð Bjarmason
Date:
May 6, 2008 12:08
Subject:
Re: thoughts about overloading method calls
Message ID:
51dd1af80805061208u6f696928w9473d1a09d94e23a@mail.gmail.com
On Tue, May 6, 2008 at 3:36 PM, Ricardo SIGNES
<perl.p5p@rjbs.manxome.org> 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


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About