I've written a whole bunch of perl modules, and I have to admit that I find some of the syntax cumbersome. There are two things I'd like to see new syntactic sugar for:
a) quickly accessing members in a hash reference
b) implicit first argument to method calls
c) let methods look like data within the object

Let me explain by way of an example:

sub somemethod { my($self, $v1, $v2) = @_; my($x1) = $self->{'key1'}; my($x2) = $self->{'key2'}; print "x1=$x1, x2=$x2\n"; my($ret) = $self->_othermethod($v1); ...

Instead, I would love to be able to say:

method somemethod { my($v1, $v2) = @_; print "x1=$self.x1, x2=$self.x2\n"; my($ret) = $self._othermethod($v1); ...

I realize parts of this have been discussed many times before. Is this *way* too hard to do? Is it just stupid?

I think if there could be a safe presumption that objects will be hash references, and that "->{'...'}" should be condensed to one unshifted keystroke such as "." we'd all be much much happier :)

Edit: chipmunk 2001-05-08

Replies are listed 'Best First'.
(ar0n) Re: Heresy
by ar0n (Priest) on May 08, 2001 at 18:41 UTC
    Like the doc said, the "dot thing" is covered in RFC 222.

    As for wanting to be able to say $self.x1, I have to disagree, because it breaks the whole data-abstraction idea. Objects may very well be implemented with hash-refs 95% of the time, but they need not be. An object may just as well be implemented through a tie.

    And saying $self->{'x1'} is bad form, btw. Your class should provide accessor functions to object-variables. Never allow direct access. Or so the object-orientation fairies tell me...

    ar0n ]

      You can enforce this rather trivially too. "The Damian"'s book shows how to let the return value from a constructor simply be a blessed scalar that is used as a hash key to a lexical hash -- the value of which is the real object! So there's never a chance that anyone outside the file could ever get at the guts of the object, at the cost of one small indirection during the first step of the call. You want privacy, you can have it!

      -- Randal L. Schwartz, Perl hacker

Re: Heresey
by Masem (Monsignor) on May 08, 2001 at 18:18 UTC
    If you look at Larry's Apocalypse 2, you'll see that the dot shorthand will be implemented into 6 in the fashion that you describe.


    Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
Re: Heresy
by tadman (Prior) on May 08, 2001 at 19:12 UTC
    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.

      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.