NAME
Data::Type::Tied - bind variables to datatypes
DESCRIPTION
SYNOPSIS
use Data::Type::Tied qw(:all);
try
{
typ STD::ENUM( qw(DNA RNA) ), \( my $a, my $b );
print "a is typ'ed" if istyp( $a );
$a = 'DNA'; # $alias only accepts 'DNA' or 'RNA'
$a = 'RNA';
$a = 'xNA'; # throws exception
untyp( $alias );
}
catch Data::Type::Exception with
{
printf "Expected '%s' %s at %s line %s\n", $e->value, $e->type->info, $e->file, $e->line;
};
TYPE BINDING
A tie-interface for Data::Type's is introduced via typ()
. Once a variable is typ'ed, valid()
is called in the background for every fetch on the value.
FUNCTIONS
typ( $type, @ref_variables )
Once an invalid value was assigned to a typ
'ed var an exception gets thrown, so place your code in a try+catch block to handle that correctly. To unglue a variable from its type use untyp()
(see below). @ref_variables
may be a list of references which suite to the $type used. Mostly its a reference to a scalar.
try
{
typ EMAIL( 1 ), \( my $typed_var, my $typed_etc, .. ); # \( ... ) returns an array of references to its elements (perlreftut)
$typed_var = 'john@doe.de'; # ok
$typed_var = 'faked&fake.de'; # throws exception
}
...
[Advanced Note] typ
adds all references to a central registry and then tie
s them to Data::Type::Tied. So don't use tie
directly, otherwise the other functions are confused and wont work.
$scalar = istyp( $type )
typ
'd variables obscure themselfs, istyp() reveals $typed_var 's type. It does this via maintaining an internal registry of all typ'd varaibles.
if( $what = istyp( $type ) )
{
print "variable \$type is tied to $what";
}
[Note] It is a synonym to tied. See perltie.
untyp( $sref )
Takes the typ constrains from a variable (like untie).
untyp( $sref );
[Note] It is nearly a synonym to untie, but also updates the internal registry.
EXPORT
None per default.
FUNCTIONS
':all' loads qw(typ untyp istyp).
CONTACT
Sourceforge http://sf.net/projects/datatype is hosting a project dedicated to this module. And I enjoy receiving your comments/suggestion/reports also via http://rt.cpan.org or http://testers.cpan.org.
AUTHOR
Murat Uenalan, <muenalan@cpan.org>