martes, 24 de febrero de 2015

Perl: Constants

Constantes en Perl y Moose


Ejemplo de como usar constantes en Perl usando Moose


{
    package Parent;
    use Moose;
    use namespace::autoclean;

    use constant MY_CONSTANT => 'CONSTANT';

    use constant {
        MY_CONSTANT => 'CONSTANT'
        NO_LEVEL => 0,
        MY_LEVEL => 1,
    };

    sub printMyConstant {
        my $self = shift;
        print &MY_CONSTANT;
    }


    __PACKAGE__->meta->make_immutable;
};

{
    package Child;
    use Moose;
    use namespace::autoclean;

    extends 'Parent';

    sub printMyLevel {
        my $self = shift;
        my $class = ref $self;

        print $class->MY_LEVEL;
        print &Parent::MY_CONSTANT;
    }
    __PACKAGE__->meta->make_immutable;
}

package main;

my $child = Child->new;
$child->printMyLevel;



No hay comentarios:

Publicar un comentario