Variable::Strongly::Typed 1.0.0 released

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!


    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
    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.


    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
    my %hash_of_floats  :TYPE('float');     # Each value must be a

    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

    $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 = 3.14159;      # no problem
    $hash_of_floats  = 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!

Site Timeline