in reply to Heresy

On the subject of heresy, I'd like to add a few remarks:

Your 'method' proposal is something that I tried to implement using existing technology, but could only come up with something that looked a sight more awful:
method 'somemethod', sub { # ... }
This wasn't as syntactically sexy as I had been hoping for, due to the fact that you can't pass named subs to a function like you can pass anonymous ones.

Further reflection on this problem, though, made me realize that one way to insert this functionality into the language without major syntactic complications is to define a 'package' like operator called 'class' which operates differently.
class Thing; our ($property,$size); sub new { return $self; # Returns a handle to this object } sub Implode { $size = 0; # Local to this object instance }
Any 'sub' within a 'class' declaration will always have a variable '$self' defined. If the sub is called bare, as in:     my ($thing) = new Thing(); If no existing variable is referenced, then a new $self is created, and is populated with any 'our' declarations from the module. This avoids the problem that plagues 'OO' programs now where an object sub may be called with no object, meaning that the first item in the stack is not actually $self, but the first parameter, forcing you to check that the first param is actually an object.

On the other hand, if the function was called with reference to a specific object, such as:     $thing->Implode(); Then $self will have the value of $thing, and the namespace will be switched to be local for that object instance, such that any changes to things like '$size' will not bleed into other instances.

Replies are listed 'Best First'.
(bbfu) Re: 'class' syntax (was: Heresy)
by bbfu (Curate) on May 09, 2001 at 03:31 UTC

    I'm curious how you went about implementing that. Did you do it with code pre-processing, or was 'class' actually a function call?

    Oh, and how did you go about switching the namespace for other code on the fly?

    bbfu
    Seasons don't fear The Reaper.
    Nor do the wind, the sun, and the rain.
    We can be like they are.