Variable::Strongly::Typed 1.0.0 released
am 02.09.2005 18:28:42 von zzoDESCRIPTION
This modules allow you to strongly type your variables. Also known
as the 'no fun' module - it can greatly enhance you code's quality
and robustness.
By enforcing types on some (or all) of your variables you will
eliminate
a large class of careless (& not so careless) errors.
This could also aid an editor or code-browsing tools to verify code
correctness without having to execute the script.
SYNOPSIS
use Variable::Strongly::Typed;
my $int :TYPE('int'); # must have an 'int' value
my $float :TYPE('float'); # must have a 'float' value
my $string :TYPE('string'); # must not be a reference
my $file :TYPE('IO::File'); # must be an IO::File
my @array_of_ints :TYPE('int'); # Each slot must contain an
int
my %hash_of_floats :TYPE('float'); # Each value must be a
float
my @array_of_rgb :TYPE(\&red_green_blue); # my enumerated type
# ... and later ...
$int = 23; # All is well
$int = 'howdy!'; # This line will croak with a good error
message
$float = 3.23; # All is well, nothing to see here
$float = new XML::Parser; # croak!
$array_of_ints[23] = 44; # Groovy
$array_of_ints[12] = 'yah '; # croak!
$hash_of_floats{pi} = 3.14159; # no problem
$hash_of_floats{e} = new IO::File; # croak!
# Return 1 if this val is RED, BLUE, or GREEN
# 0 otherwise
sub red_green_blue {
local $_ = shift;
/\A RED \z/xms || /\A BLUE \z/xms || /\A GREEN \z/xms;
}
$array_of_my_very_own_types[23] = 99; # croak!
$array_of_my_very_own_types[2] = 'BLUE'; # OK!
Available at a CPAN near you!
Mark