Re: Coderefs in objects: tricky questions



peterkayatwork@xxxxxxxxx wrote:

3.  Is there a way to set $self->{Sub} without using 'sub'?

I want to pull this effect off:

$self->{Sub} = sub { return $self->Real(@_) };

without using 'sub'.

Why? A coderef is created using 'sub'. A coderef is executed by invoking it as a function. Assuming you could create a coderef without 'sub', it would still have to be invoked as a subroutine/function to get executed.

Besides, can't I claim that the "sub" adds another layer of
subroutines, thus hurting the execution time of my script?

No. You have to go through internal process of pushing things onto the call stack to execute the code; so there is no getting around that bit of overhead. -Joe .