Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: OOP's setter/getter method - is there a way to avoid them?

by Discipulus (Canon)
on Oct 27, 2015 at 08:44 UTC ( [id://1146077]=note: print w/replies, xml ) Need Help??


in reply to OOP's setter/getter method - is there a way to avoid them?

Hello, you receive for sure good replies, but even if i'm not a ObjectOrientedMonk (read Damian Conway's ten rules for when to use OO), i recall that some framework for OOPerl gives theese method for free if you want: Moose and Moo.

In a very lazy and minimalist perspective i recall also a good trick to have the setter and getter method for free without extra packages using the AUTOLOAD (a Perl mechanism that intercepts all possible undefined method calls) trick:
package Person; use strict; use Carp; use vars qw($AUTOLOAD %ok_field); # Authorize four attribute fields for my $attr ( qw(name age peers parent) ) { $ok_field{$attr}++; } sub AUTOLOAD { my $self = shift; my $attr = $AUTOLOAD; $attr =~ s/.*:://; return unless $attr =~ /[^A-Z]/; # skip DESTROY and all-cap metho +ds croak "invalid attribute method: -> $attr()" unless $ok_field{$attr}; $self->{uc $attr} = shift if @_; return $self->{uc $attr}; } sub new { my $proto = shift; my $class = ref($proto) || $proto; my $parent = ref($proto) && $proto; my $self = {}; bless($self, $class); $self->parent($parent); return $self; } 1;
If i remember this code is from the old but still worth reading book Perl CookBook. In this book an entire chapter is dedicated to Object but starting from the very primitive concepts (the book is aged).

Even in the recent and very good book Modern Perl ther is tha chapter object oriented Perl that is very interesting.

About your other general questions... is a matter of aptitude i think. Remember also that an OO Perl program is generally slower than a normal one. Anyway setter and getter methods are needed if you want do something with that fat Object. And yes OO is a general way of thinking to data and behaviours and relationships.
Here at Perlmonks is a subject discussed many times: see super search: ?node_id=3989;HIT=object;re=N;BR;MR;M (notabely The Power of Objects Object-Oriented Perl - Introduction in The Perl Review Object Oriented Perl - very basic guide When to Use Object Oriented approach in Perl? (RFC))

HtH
L*
There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

Replies are listed 'Best First'.
Re^2: OOP's setter/getter method - is there a way to avoid them?
by tiny_monk (Sexton) on Oct 27, 2015 at 09:44 UTC

    Thank you, Discipulus. I'll look into all the sources that you have suggested. Damian Conway's rules were particulary useful. And, yes, thank you for confirming that OOP is slower - I think by 30%. If I remember it correctly, the author Randal Schwartz had mentioned this piece of information in his book. However, it was in 2003 that he made this statement. I'm unsure how OOP behaves on present day computers assuming that the trend for computer performance is to improve each year. I have heard of Moose and Moo lately but I do not have any idea what they could actually do. I'll look into it as well. If you have the time, I'd appreciate it if you could share with me the author of Perl Cookbook. :)

      Well, it's likely to be consistent in that it'll always be _slower_. But that's never been the point of OO. The point is to segregate and isolate chunks of code such that you always know where you're looking for problems and tracing bugs. And 10 years of Moore's law means that speed is increasingly not a concern - and when it is, it's time to whip out a profiler, and decide on a risk managed way, if you can afford to rewrite

      I'd appreciate it if you could share with me the author of Perl Cookbook. :)

      Perl Cookbook is written by Tom Christiansen and Nathan Torkington. Tom also co-wrote Programming Perl (among other highly-regarded endeavours).

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2024-04-25 23:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found