Data::Type::Tied - bind variables to datatypes
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; };
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.
typ()
valid()
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.
typ
untyp()
@ref_variables
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 ties them to Data::Type::Tied. So don't use tie directly, otherwise the other functions are confused and wont work.
tie
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.
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.
None per default.
':all' loads qw(typ untyp istyp).
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.
Murat Uenalan, <muenalan@cpan.org>
To install Data::Type, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Data::Type
CPAN shell
perl -MCPAN -e shell install Data::Type
For more information on module installation, please visit the detailed CPAN module installation guide.