| Object-Enum documentation | Contained in the Object-Enum distribution. |
Object::Enum - replacement for if ($foo eq 'bar')
Version 0.072
use Object::Enum qw(Enum);
my $color = Enum([ qw(red yellow green) ]);
# ... later
if ($color->is_red) {
# it can't be yellow or green
See Sub::Exporter for ways to customize this module's exports.
An optional shortcut for Object::Enum->new.
my $obj = Object::Enum->new(\@values); # or $obj = Object::Enum->new(\%arg);
Return a new Object::Enum, with one or more sets of possible values.
The simplest case is to pass an arrayref, which returns an object capable of having any one of the given values or of being unset.
The more complex cases involve passing a hashref, which may have the following keys:
whether this object can be 'unset' (defaults to true)
this object's default value is (defaults to undef)
an arrayref, listing the object's possible values (at least one required)
my $new = $obj->clone; my $new = $obj->clone($value);
Create a new Enum from an existing object, using the same arguments as were
originally passed to new when that object was created.
An optional value may be passed in; this is identical to (but more convenient
than) calling value with the same argument on the newly cloned object.
This method was formerly named spawn. That name will still work but is
deprecated.
The current value as a string (or undef)
Note: don't pass in undef; use the unset method instead.
The possible values for this object
Unset the object's value (set to undef)
Automatically generated from the values passed into new.
None of these methods take any arguments.
The set_* methods are chainable; that is, they return
the object on which they were called. This lets you do useful things like:
use Object::Enum Enum => { -as => 'color', values => [qw(red blue)] };
print color->set_red->value; # prints 'red'
Hans Dieter Pearcey, <hdp at cpan.org>
Please report any bugs or feature requests to
bug-object-enum at rt.cpan.org, or through the web interface at
http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Object-Enum.
I will be notified, and then you'll automatically be notified of progress on
your bug as I make changes.
You can find documentation for this module with the perldoc command.
perldoc Object::Enum
You can also look for information at:
Copyright 2006 Hans Dieter Pearcey, all rights reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
| Object-Enum documentation | Contained in the Object-Enum distribution. |
package Object::Enum; use strict; use warnings; use 5.006001; use Carp (); use Sub::Install (); use base qw( Class::Data::Inheritable Class::Accessor::Fast ); __PACKAGE__->mk_classdata($_) for ( '_values', '_unset', '_default', ); __PACKAGE__->mk_accessors( 'value', ); __PACKAGE__->_unset(1); use overload ( q{""} => '_stringify', fallback => 1, ); use Sub::Exporter -setup => { exports => [ Enum => \&_build_enum ], }; sub _build_enum { my ($class, undef, $arg) = @_; return sub { $class->new({ %$arg, %{shift || {} } }) }; }
our $VERSION = '0.072';
my $id = 0; sub _generate_class { my $class = shift; no strict 'refs'; my $gen = sprintf "%s::obj_%08d", $class, ++$id; push @{$gen."::ISA"}, $class; return $gen; } sub _mk_values { my $class = shift; for my $value (keys %{ $class->_values }) { Sub::Install::install_sub({ into => $class, as => "set_$value", code => sub { $_[0]->value($value); return $_[0] }, }); Sub::Install::install_sub({ into => $class, as => "is_$value", code => sub { (shift->value || '') eq $value }, }); } } sub new { my ($class, $arg) = @_; $arg ||= []; if (ref $arg eq 'ARRAY') { $arg = { values => $arg }; } unless (@{$arg->{values} || []}) { Carp::croak("at least one possible value must be provided"); } exists $arg->{unset} or $arg->{unset} = 1; exists $arg->{default} or $arg->{default} = undef; if (!$arg->{unset} && !defined $arg->{default}) { Carp::croak("must supply a defined default for 'unset' to be false"); } if (defined($arg->{default}) && ! grep { $_ eq $arg->{default} } @{$arg->{values}}) { Carp::croak("default value must be listed in 'values' or undef"); } my $gen = $class->_generate_class; $gen->_unset($arg->{unset}); $gen->_default($arg->{default}); $gen->_values({ map { $_ => 1 } @{$arg->{values}} }); $gen->_mk_values; return $gen->spawn; } sub _stringify { my $self = shift; return '(undef)' unless defined $self->value; return $self->value; }
sub clone { my $class = shift; my $self = bless { value => $class->_default, } => ref($class) || $class; $self->value(@_) if @_; return $self; } BEGIN { *spawn = \&clone }
sub value { my $self = shift; if (@_) { my $val = shift; Carp::croak("object $self cannot be set to undef") unless defined $val; unless ($self->_values->{$val}) { Carp::croak("object $self cannot be set to '$val'"); } return $self->_value_accessor($val); } return $self->_value_accessor; }
sub values { my $self = shift; return keys %{ $self->_values }; }
sub unset { my $self = shift; unless ($self->_unset) { Carp::croak("object $self cannot be unset"); } $self->_value_accessor(undef); }
1; # End of Object::Enum