public
Last active

  • Download Gist
Animal3.pm
Perl
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
# NOTE: This is simply an example of a legacy array-based object.
# I do not recommend using it.
package Animal3;
use strict;
 
our $NAME_INDEX = 0;
 
sub new {
my $class = shift;
my $self = [];
$self->[$NAME_INDEX] = shift;
bless $self, $class;
return $self;
}
 
sub name {
my $self = shift;
if (@_) { $self->[$NAME_INDEX] = shift }
return $self->[$NAME_INDEX];
}
 
1;
Camel.pm
Perl
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
package Camel;
use Moose;
use MooseX::NonMoose::InsideOut;
extends 'Animal3';
 
has 'humps' => ( is => 'ro', isa => 'Num' );
 
sub FOREIGNBUILDARGS {
my $class = shift;
# We expect a hashref with 'name' and 'humps'.
my $args = shift;
 
# Give Animal3 what it wants: just the name, and nothing else.
return $args->{name};
}
 
no Moose;
__PACKAGE__->meta->make_immutable;
example3.pl
Perl
1 2 3 4 5 6
#!/usr/bin/perl
use Modern::Perl;
use Camel;
 
my $c = Camel->new({ name => "Samuel Camel", humps => 2 });
say $c->name, " has ", $c->humps, " humps";

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.