http://www.perlmonks.org?node_id=320522


in reply to Object Terminology

Nice introduction. I would like to offer a few comments.

One concept that you haven't touched and should deserve some explanation is Encapsulation, which in Perl has a few peculiar aspects.

Encapsulation is restricting data into an object container, with the purpose of protecting data against accidental manipulation and reducing the program complexity. One basic idea of OOP is to allow (or at least to recommend) data access through class methods only.

Perl encourages encapsulation "by good manners", as Damian Conway puts it, even though it allows stronger encapsulation through some tricks.

Also, Polymorphism should be explained a bit more. Perl allows polymorphism with and without inheritance. For example, you can do something like the following even with a non-OOP language. (Actually this is what I used to do - using C - in the late 1980s, when I learned OOP but C++ compilers weren't easy to get.)

#!/usr/bin/perl -w use strict; # polymorphism without inheritance my @objects = ( { name => 'sheep', speak => sub { print "baaah"; } }, { name => 'dog', speak => sub {print "woof";} }, { name => 'mouse', speak => sub {print "squeak (almost silently)" ;} }, { name => 'fish', speak => sub { print ".oO()" } } ); for (@objects) { print "a $_->{name} goes "; &{$_->{speak}}; print "\n" } __END__ a sheep goes baaah a dog goes woof a mouse goes squeak (almost silently) a fish goes .oO()

Polymorphism through inheritance is what is more commonly accepted as part of the OOP paradigm and it is the basis for object reuse. (At least in theory. Things in the real world could take different shapes).

 _  _ _  _  
(_|| | |(_|><
 _|   

Replies are listed 'Best First'.
Re: Re: Object Terminology
by stvn (Monsignor) on Jan 11, 2004 at 21:59 UTC
    Excellent point. Both of those terms deserve a space in this document. As I said, I am continually editing the original, as its just the first draft. I will add this in there ASAP.
Re^2: Object Terminology
by Coruscate (Sexton) on Jan 12, 2004 at 07:55 UTC

    I know this was just meant as a quick example for demonstration purposes, but you'd think you would want a return value from the subs, rather than a printed value. This allows a greater functionality of that method, such as placing the value in a scalar rather than printing it to the screen immediately. Meaning:

    #!/usr/bin/perl -w use strict; my @objects = ( { name => 'sheep', speak => sub { "baaah" } }, { name => 'dog', speak => sub { "woof" } }, { name => 'mouse', speak => sub { "squeak (almost silently)" } }, { name => 'fish', speak => sub { ".oO()" } } ); printf "a %s goes %s.\n", $_->{name}, $_->{speak}->() for @objects; __END__ a sheep goes baaah. a dog goes woof. a mouse goes squeak (almost silently). a fish goes .oO().

    (This is not even nitpicking or saying you did it a dumb way. I was just proud of my own observation on how to improve such a thing for wider use :P)