Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Perl: porters

Better OO support

 

 

Perl porters RSS feed   Index | Next | Previous | View Threaded


aas at oslonett

Sep 19, 1995, 8:48 AM

Post #1 of 3 (85 views)
Permalink
Better OO support

I have by now implemented a few OO modules in perl. One thing that
strikes me is that I get very tired by typing (and reading) "$self->"
(or "$this->") all the time. Can't we add some syntactic sugar to make
the life of OO programmers easier?

What I think would be desirable is to have a new keyword that would
replace "sub" for methods. I believe this was discussed on p5p a
year(?) ago but I don't remember much of that discussion.

In the examples below I have used "method" as the new keyword. When
you declare a sub as method these things could happen:

The statement "my $self = shift;" is automatically added to the
beginning of the procedure body.

Every access to a variable that is not previously declared "my" or
fully qualifed is looked up in $self->{}. I.e. $a is $self->{a}
and @a is @{$self->{a}}.

You can call another method for the same object without typing $self.
"->foo" is equivalent with "$self->foo;".

Does this make sense?

The problems I can see with this proposal (besides that it might has
to be implemented) are that objects are not always hashes, and we
really should be able to have both a $foo and @foo within the same
object.



package Foo;

$d = ..;

sub new { bless {}, $_[0] } # object represented by a blessed hash

method bar
{
my $a = $b;
push(@c, 5);
$d = $Foo::d;
}

# would be the same as

sub bar
{
my $self = shift;
my $a = $self->{b};
push(@{$self->{c}}, 5);
$self->{d} = $d;
}

# Easier method call for calls to other methods in the same object.

method xxx
{
->bar(1,2);
}

# would be the same as

sub xxx
{
my $self = shift;
$self->bar(1,2);
}


--
Gisle Aas <aas [at] oslonett>
Oslonett AS http://www.oslonett.no/home/aas/


david.avraamides at gs

Sep 19, 1995, 10:48 AM

Post #2 of 3 (83 views)
Permalink
Re: Better OO support [In reply to]

>>>>> Gisle Aas writes:

Gisle> I have by now implemented a few OO modules in perl. One thing that
Gisle> strikes me is that I get very tired by typing (and reading) "$self->"
Gisle> (or "$this->") all the time. Can't we add some syntactic sugar to make
Gisle> the life of OO programmers easier?

Gisle> What I think would be desirable is to have a new keyword that would
Gisle> replace "sub" for methods. I believe this was discussed on p5p a
Gisle> year(?) ago but I don't remember much of that discussion.

Gisle> In the examples below I have used "method" as the new keyword. When
Gisle> you declare a sub as method these things could happen:

Gisle> The statement "my $self = shift;" is automatically added to the
Gisle> beginning of the procedure body.

Gisle> Every access to a variable that is not previously declared "my" or
Gisle> fully qualifed is looked up in $self->{}. I.e. $a is $self->{a}
Gisle> and @a is @{$self->{a}}.

Gisle> You can call another method for the same object without typing $self.
Gisle> "->foo" is equivalent with "$self->foo;".

Gisle> Does this make sense?

Gisle> The problems I can see with this proposal (besides that it might has
Gisle> to be implemented) are that objects are not always hashes, and we
Gisle> really should be able to have both a $foo and @foo within the same
Gisle> object.

[stuff deleted]

Gisle,

The problem with these types of solutions (as you pointed out) is that
they force restrictions on the programmer or make assumptions that
aren't always true. Although they may make some code a little easier
to write, in my opinion, they go against the grain of Perl: flexibility.

For a lot of your suggestions, you could easily write a preprocessor
(in Perl!) to accomplish what you want without changing the language.
I often just create macros in emacs to do a lot of the redundant work
(globally replace $_x with $self->{x}, for instance, or copy paste the
'my $self = shift;' statement). I think the features Larry added to
Perl5 (references, etc.) to allow one to create objects gives us the
toolkit we needed, but I like the fact that he left it to the
programmer to decide how to use those tools. For instance, I often use
arrays for objects, or sometimes when I use hashes I "hide" the
members (through obfuscated names or subpackages) and protect access
to the members through subs. I choose among these alternatives based
on the job at hand. But I like the fact that all of the
implementations are based on the same simple building blocks in Perl.

My 2 cents...
-Dave


gerti at BITart

Sep 19, 1995, 9:13 PM

Post #3 of 3 (83 views)
Permalink
Re: Better OO support [In reply to]

> I have by now implemented a few OO modules in perl. One thing
> that strikes me is that I get very tired by typing (and reading)
> "$self->" (or "$this->") all the time. Can't we add
> some syntactic sugar to make the life of OO programmers easier?

I am not sure about this one. Readability of the code is an issue
here, and also the problem, that since perl5 almost everything is
syntactically legal anyway, and that makes debugging quite a
pain...

> What I think would be desirable is to have a new keyword that would
> replace "sub" for methods. I believe this was discussed on p5p a
> year(?) ago but I don't remember much of that discussion.
>
> In the examples below I have used "method" as the new keyword.
> When you declare a sub as method these things could happen:
>
> The statement "my $self = shift;" is automatically added to the
> beginning of the procedure body.

Makes sense (other that I personally hate my and prefer local, but
that is another story). $self should also be tested on being an
object.

However, there should be a distinction between class methods and
Instance methods (Objective C uses '+' for class methods and
'-' for instance methods, where perl uses the sub), and in a
class method $self should be tested being a package reference.

> Every access to a variable that is not previously declared "my"
> or fully qualified is looked up in $self->{}. I.e. $a is
> $self->{a} and @a is @{$self->{a}}.

Again, this looks like to much magic for me. From my experience,
especially in larger OO projects (and OO projects tend to be that
way), I usually wished, Perl would rather be on the more
restrictive side. With that much 'magic' going on, it is very
easy to get hopelessly lost. But what about using a character like
'!' for the '$self->{}' part?

So, $self->{'foo'} would become $!foo etc.

You can call another method for the same object without typing $self.
"->foo" is equivalent with "$self->foo;".

And, of course, I have to ask for the '$super' again...

[munch]
> --
> Gisle Aas <aas [at] oslonett>
> Oslonett AS
> <http://www.oslonett.no/home/aas/

Gerd Knops
gerti [at] BITart

Perl porters RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.