Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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.

In reply to Re: OOP's setter/getter method - is there a way to avoid them? by Discipulus
in thread OOP's setter/getter method - is there a way to avoid them? by tiny_monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (3)
As of 2024-04-19 17:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found