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

Perl was not necessarily designed to be Object Oriented, and the addition of OO to Perl has been called by some a hack at best. If this concern of yours about packages is great enough then i instead invite you to use the declarative syntax for Moose, MooseX::Declare.

jeffa

L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
B--B--B--B--B--B--B--B--
H---H---H---H---H---H---
(the triplet paradiddle with high-hat)

Replies are listed 'Best First'.
Re^3: OO Perl Baby Steps
by Clovis_Sangrail (Beadle) on Jun 19, 2013 at 17:14 UTC

    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.

      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

      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 ...

      jeffa

      L-LL-L--L-LL-L--L-LL-L--
      -R--R-RR-R--R-RR-R--R-RR
      B--B--B--B--B--B--B--B--
      H---H---H---H---H---H---
      (the triplet paradiddle with high-hat)
      

        Jeffa,

        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?

        Thanks

        Anne