Beefy Boxes and Bandwidth Generously Provided by pair Networks Bob
laziness, impatience, and hubris

Re: Tutorial: Introduction to Object-Oriented Programming

by demerphq (Chancellor)
on Dec 10, 2002 at 16:24 UTC ( #218817=note: print w/ replies, xml ) Need Help??

in reply to Tutorial: Introduction to Object-Oriented Programming


What a post. ++ to you dude.

A few minor thoughts. You need to be careful about transplanting idioms and the like from other langauges into perl. While its not strictly speaking _wrong_ it does lose a lot of the elegance and beauty of Perl. To what do i refer?

sub setPhrase { my $self = shift; my $phrase = shift; $self->{phrase} = $phrase; } sub getPhrase { my $self = shift; return $self->{phrase}; }
This has "i learned to program OO with java" written all over it. :-)

First off it breaks the generally held style of conventions of Perl, which I wont get too hung up about except to quote from perlstyle.

Java perhaps never considered the ease of use of its conventions outside of the English community (standard English speakers blindness) but as so many contributors and programmers of Perl are non-native english speakers its taken a bit more seriously in Perl circles.

The second point about this is that the seperate set/get accessors are not at all a perl convention. In Java I suppose it makes sense due to method signatures and the like, but with Perls flexible calling conventions it is _much_ more common to see property accessors writen like this:

sub Phrase { my $self=shift; $self->{phrase}=shift if @_; return $self->{phrase} }
or like this
sub Phrase { my $self=shift; if (@_) { $self->{phrase}=shift; return $self; } return $self->{phrase} }
I personally prefer the latter as it allows for method chaining:
my $dump=Data::Dumper->new([$foo])->Terse(1)->Purity(0)->Names(['foo'] +)->Dump()
This is an extremely common convention in perl and should not be ignored by a class/module designer.

Personally I wont use classes (except as a last resort) with the type of accessor design that you describe. Its too clunky and noisy for my tastes. Perl is supposed to flow like poetry...


I dont understand where you got this idea from. eval{} die() are perls standard exception handling mechanism. I dont think the alternatives are anywhere near as useful or powerful for many tasks, and often result in torturous code. Of course that doesnt mean that one shouldnt take advantage of undef returns and the like as well, but personally if it means gunking up the interface to avoid dying I wont. Some things (like opening a file) IMO _should_ die when they go wrong.

my $saver  = new Saver::Database($db, $user, $pass);

This one I also have a problem with. I do not think that it is at all a good idea in a tutorial to recommend what is generally considered bad practice (unfortunately this particular one is all too common). The indirect object syntax is dangerous and should be avoided. It doesnt always parse the way one might think it should and when it doesnt the bugs are _hard_ to track down. Much better to use the absolutely unambiguous

my $saver = Saver::Database->new($db, $user, $pass);
Especially when teaching potential beginers.

My last thought goes back to an earlier one. Perl provides great flexibility in interface design. It is well worth the effort to utilize this flexibilty to give your classes and objects intuitve and easy to use interfaces. For instance:

$quote1->setPhrase($phrase); $quote1->setAuthor($author);
If I thought that this was going to happen anymore often then very very rarely I would replace that with a
or maybe (actually probably not, but possibly)
Anyway, dont get these comments wrong. My primary intention is to point out that importing concepts from other languages is a fine thing, but it shouldnt go so far as to obscure the beauty elegance and power of Perl.

From what I can tell this is a worthy post and I thank you for your efforts. I will definately be voting that this goes to tutorials.

(Although i think you should link to the standard Perl OO tutorials:perlboot perltoot etc...)

Once again, kick ass post ++

Ps: Next time you write something this big stick a few readmore tags in... :-)

--- demerphq

Comment on Re: Tutorial: Introduction to Object-Oriented Programming
Select or Download Code
Re: Re: Tutorial: Introduction to Object-Oriented Programming
by pdcawley (Hermit) on Dec 11, 2002 at 12:03 UTC
    I know that it's common not to have separate set_foo methods, but I still think they're the right thing. That way it's easy to set up attributes that are read only or read/write accessible to the outside world. If you just have the single method you're reliant on people reading the docs to find out whether they can call the accessor with a new value.

    You're bang on about returning $self though.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://218817]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (13)
As of 2014-03-13 14:16 GMT
Find Nodes?
    Voting Booth?

    Have you used a cryptocurrency?

    Results (281 votes), past polls