domingo, 28 de septiembre de 2014

Perl: POO con Moose, Estructura de datos

Estructura de datos con Moose

Las clases en Moose (con perl 5) pueden tener estructuras de datos como atributos usando Types en la definición del isa.

1) Clase con Atributos Hash

#This is perl 5, version 14, subversion 2 (v5.14.2)
#File: MyHash.pm
package MyHash;

    use Moose;
    use strict;
    use warnings;
    use namespace::autoclean;

    has 'myHashRef' => (
is => 'rw',
isa => 'HashRef',
reader  => 'getMyHashRef',
writer  => 'setMyHashRef',
default => sub {
return {
'0' => "first",
'1' => "second"
}
                                    }
                     );

    sub printOptions {
        my ( $self ) = @_;
        my $str = $self->getMyHashRef->{'0'};
        $str .= "\n";
        $str .= $self->getMyHashRef->{'1'};
        $str .= "\n";
        print $str;
    }

no Moose;
__PACKAGE__->meta->make_immutable;
1;

#................................................................................................
#File: script_test_MyHash.pl
#!/usr/bin/perl
use strict;
use warnings;
use MyHash;

my $instancia = MyHash->new();
$instancia->printOptions();
$instancia->setMyHashRef({'0' => "primero", '1' => "segundo"});
$instancia->printOptions();
#.............................................................................................end



Referencias:
-Types in Moose.
-Attribute Native.


No hay comentarios:

Publicar un comentario