Learning Object Oriented Programming (newbie)

T

Ted Zlatanov

PS> Stylistic concerns aside, the original poster's statement returns the
PS> value of the attribute if no argument is passed to the method, a common
PS> mixed getter/setter pattern. You'd need to add a return statement and
PS> repeat the hash key anyway to get that behavior.

Yeah, I wasn't trying to fix all the code, just the part that hurt my
eyes :)

I'd suggest Moose in general for this kind of drudgery, unless
performance was a concern. Writing your own setters and getters is kind
of a pain to do *right*, and quite a few book chapters have been devoted
to it, with varying degrees of success. Once you add validation,
optional and compound parameters, initializers, and so on, it's quickly
obvious that you're reinventing Moose.

Ted
 
T

Ted Zlatanov

R> "If no return is found and if the last statement is an expression, its
R> value is returned. "

R> Perldoc perlsub

Unless you're writing Lisp, I'd avoid that idiom. It's usually better
in imperative languages like Perl to return explicitly. It will work,
but you run the risk that a) you'll add another statement afterwards
accidentally, and b) a maintainer 10 years later will miss it. I've
seen both happen :)

Ted
 
T

Ted Zlatanov

SS> Out of all the things one can pull off in Perl *that's* near the *top*
SS> of your "...list for misguided
SS> almost-getting-it-but-not-quite use of Perl."

I thought it was clear I was using hyperbole and humor (hence "otherwise
it was OK"), but just in case: no, this is not that close to the top.

I should have added a "return" statement as others mentioned; sorry
about that.

Ted
 
P

Peter Scott

PS> Stylistic concerns aside, the original poster's statement returns
the PS> value of the attribute if no argument is passed to the method, a
common PS> mixed getter/setter pattern. You'd need to add a return
statement and PS> repeat the hash key anyway to get that behavior.

Yeah, I wasn't trying to fix all the code, just the part that hurt my
eyes :)

I'd suggest Moose in general for this kind of drudgery, unless
performance was a concern. Writing your own setters and getters is kind
of a pain to do *right*, and quite a few book chapters have been devoted
to it, with varying degrees of success. Once you add validation,
optional and compound parameters, initializers, and so on, it's quickly
obvious that you're reinventing Moose.

Well yes. But in fairness, there *is* a narrow window of acceptability
for writing this code, if you've been expanding a procedural script to
the point where there's more than a couple of parameters being passed
around to multiple places and you realize that a very small object would
make it easier to maintain and you don't yet have Moose installed.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,149
Latest member
Vinay Kumar Nevatia0
Top