Enum.pm

Code Index:



NAME

        Enum


SYNOPSIS

        # comme constante !
        my $_o_ALLOW_FIELDS = New Enum( 'un', 'deux', 'trois');
        my $val = 'deux';
        print 'correct' if( $_o_ALLOW_FIELDS->IsOk( $val ) );
        $val = 'quatre';
        print 'inconnu' if( $_o_ALLOW_FIELDS->IsOk( $val ) );


DESCRIPTION

        This is an object way of putting enum data.
        The other way is to use $hash{$val}

#
#       Sebastien.Letort@toulouse.inra.fr
#       Created: August 30, 2007
#       Last Updated: August 30, 2007
#       $Id: Enum.pm 86 2007-10-10 08:05:41Z sletort $
#

package Enum;

use strict;
use warnings;

BEGIN
{
    our $VERSION = do {my @r = (q$Rev: 86 $ =~ /\d+/g); $r[0]};
}

sub New
{
    my $class = shift;
    my $self  = {};
    bless($self, $class);

    foreach my $val (@_)
    {
        $self->{$val} = 1;
    }

    return $self;
}

sub IsOk
{
    my $self = shift;
    my ($val) = @_;

    return defined $self->{$val} ? 1 : 0;
}

1;