Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Re^3: OO Perl Baby Steps

by Clovis_Sangrail (Beadle)
on Jun 19, 2013 at 17:14 UTC ( #1039805=note: print w/replies, xml ) Need Help??

in reply to Re^2: OO Perl Baby Steps
in thread OO Perl Baby Steps

I do want to check out Moose and MooseX (I'm tempted to call it 'Moose++'), it seems like alot of people use it. But I first want to get thru the, err, 'official' implementation of OO Perl as described in the Alpaca book.

As I was reading the book and got to the section on closures, I thought to myself: "Ahh, this is how they are going to do private variables in a class." Then it turns out that it doesn't work that way at all. The private data is just all the fields in the whatever-it-is that the blessed reference references. And some people complain that it isn't as really private as they'd like.

I'm surprised that there is not more use of closures. OTOH, maybe that would not work well with inheritance? In Googling about on the issue I see that I'm far from the first person to think about this. Well, exploring that question is likely much further over my head at this point than is Moose.

Replies are listed 'Best First'.
Re^4: OO Perl Baby Steps
by tobyink (Abbot) on Jun 20, 2013 at 11:46 UTC

    Closures are totally workable as a basis for OO Perl...

    #!/usr/bin/env perl use strict; use warnings; use Data::Dumper; { package Person; sub new { my $class = shift; my %store; bless sub { my ($action, $slot, $value) = @_; for ($action) { if (/fetch/) { return $store{$slot} } if (/store/) { return $store{$slot} = $value } if (/delete/) { return delete $store{$slot} } if (/exists/) { return exists $store{$slot} } } die; } => $class; } sub name { my $self = shift; @_ ? $self->(store => "name", @_) : $self->(fetch => "name") } sub age { my $self = shift; @_ ? $self->(store => "age", @_): $self->(fetch => "age") } } my $person = Person->new; $person->name("Bob"); $person->age("42"); print $person->name, " is ", $person->age, " years old.\n"; print Dumper($person);

    However, they're slower than the more usual blessed hashref storage, because a method call will typically involve at least two sub calls.

    For some reason I feel compelled to produce a MooseX module allowing you to use blessed coderefs for Moose objects like the example above.

    package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name
Re^4: OO Perl Baby Steps
by jeffa (Bishop) on Jun 19, 2013 at 17:24 UTC

    I see two primary concerns here regarding OO: encapsulation and security. Perl does not provide acceptable security here, because to do so would be very un-Perl like. As someone who was trained on classic OO before he studied and used Perl, i have absolutely no desire to use that private or protected nonesense in my code, because i dislike the restrictions they impose on future hackers of my code.

    Othewise i would use Java or Ruby or perhaps Perl 6 in 100 years ...


    (the triplet paradiddle with high-hat)


      If you have the time, I would greatly appreciate it if you would give an explanation or example(s) of the security risks related to using OO in perl. Or perhaps suggest a link to where I could learn more?



        I was merely referring to "locking down" access to certain methods via the concepts (and implementation) of private, protected and public method modifiers. In other words, secure your methods' accessibility from clients.


        (the triplet paradiddle with high-hat)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1039805]
[hippo]: Modularity is not the Apple way.

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (6)
As of 2018-07-23 10:21 GMT
Find Nodes?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

    Results (463 votes). Check out past polls.