Attribute::Types - Attributes that confer type on variables
This document describes version 0.10 of Attribute::Types, released May 10, 2001.
use Attribute::Types; my $count : INTEGER; # Can only store an integer my $date : INTEGER(1..31); # Can only store an int between 1..31 my $value : NUMBER; # Can only store a number my $score : NUMBER(0.1..9.9); # Can only store a num between 0.1..9.9 my @rain : NUMBER; # Elements can only store numbers my %vars : SCALAR; # Entries can only store scalar refs my %handler : CODE; # Entries can only store sub refs my $arr : ARRAY; # Can only store array ref my @hashes : HASH; # Elements can only store hash refs my $glob : GLOB; # Can only store a typeglob ref my $pattern : REGEX; # Can only store a qr'd regex my $ref2 : REF; # Can only store a meta-reference my $obj : Type(My::Class); # Can only store objects of (or # derived from) the specified class my $x : Type(/good|bad|ugly/); # Can only store strings matching # the specified regex sub odd { no warnings; $_[0]%2 } my $guarded : Type(&odd); # Can only store values for which # odd($value) returns true $date = 23; # okay $date = 32; # KABOOM! $rain[1] = 121.7; # okay $rain[1] = "lots"; # KABOOM! $x = 'very good'; # okay $x = 'excellent'; # KABOOM! package My::Class::Der; use base 'My::Class'; $obj = My::Class->new(); # okay $obj = My::Class::Der->new(); # okay $obj = Other::Class->new(); # KABOOM! $guarded = 1; # okay $guarded = 2; # KABOOM!
The Attribute::Types module provides 10 universally accessible attributes that can be used to create variables that accept assignments of only specific types of data.
The attributes are:
INTEGER
Indicates that the associated scalar, or the elements of the associated array, or the entries of the associated hash can only contain integer values (those values that are internally represented as actual numbers (or may be converted to actual numbers without generating a warning), and for which int($value)==$value).
int($value)==$value)
The attribute may also be specified with a range of integer values, indicating a further restriction on the values the associated variable can store. For example:
my $x1 : INTEGER(1..100); # Any int between 1 and 100 my $x2 : INTEGER(-10..10); # Any ine between -10 and 10 my $x3 : INTEGER(0..); # Any positive int my $x4 : INTEGER(..99); # Any int < 100 (including negatives)
NUMBER
Indicates that the associated scalar, or the elements of the associated array, or the entries of the associated hash can only contain values that are internally represented by (or silently convertible to) valid Perl numbers.
The attribute may also be specified with a range of numerical values, indicating a further restriction on the values the associated variable can store. For example:
my $x1 : NUMBER(1.0..100.0); # Any number between 1 and 100 my $x2 : NUMBER(-10..10); # Any number between -10 and 10 my $x3 : NUMBER(0..); # Any positive number my $x4 : NUMBER(..99.9); # Any number < 99.9 (incl. negatives)
SCALAR
Indicates that the associated scalar, or the elements of the associated array, or the entries of the associated hash can only contain references to scalars (i.e. only values for which ref($value) eq 'SCALAR').
ref($value) eq 'SCALAR'
ARRAY
Indicates that the associated variable can only contain references to arrays (i.e. only values for which ref($value) eq 'ARRAY').
ref($value) eq 'ARRAY'
HASH
Indicates that the associated variable can only contain references to hashes (i.e. only values for which ref($value) eq 'HASH').
ref($value) eq 'HASH'
CODE
Indicates that the associated variable can only contain references to subroutines (i.e. only values for which ref($value) eq 'CODE').
ref($value) eq 'CODE'
GLOB
Indicates that the associated variable can only contain references to typeglobs (i.e. only values for which ref($value) eq 'GLOB').
ref($value) eq 'GLOB'
REF
Indicates that the associated variable can only contain references to other references (i.e. only values for which ref($value) eq 'REF').
ref($value) eq 'REF'
REGEX
Indicates that the associated variable can only contain references to precompiled regular expressions (i.e. only values for which ref($value) eq 'Regexp').
ref($value) eq 'Regexp'
Type
Used to specify class-wise or generic storage constraints on a variable. There are three permitted syntaxes:
Type(Class::Name)
Indicates that the associated variable can only contain references to objects belonging to the specified class, or to one of its derived classes.
Type(/pattern/)
Indicates that the associated variable can only contain values that successfully match the specified pattern.
Type(&subname)
Indicates that the associated variable can only contain values for which the specified subroutine returns true when passed the value as its first argument.
Note that anonymous subroutines cannot be used in this context (they are run-time phenomena and types have to be set up at compile-time).
If the module is imported with a list of attribute names:
use Attribute::Types qw(INTEGER HASH);
then only those attributes can be used to specify types.
The type checking set up by this module is run-time type-checking. That is, the validity of an assignment is only checked when the assignment operation is actually performed, not when it is compiled.
Cannot assign value to type variable
The value being assigned is not consistent with the declared type of the variable.
Can't specify type attribute for CODE
Subroutines cannot be typed using the Attribute::Types attributes.
Invalid type specifier: Type(garbage)
The Type(...) attribute can only be specified with a class name, a pattern (in /.../), or a subroutine name.
Type(...)
This is all Nat Torkington's idea.
Damian Conway (damian@conway.org)
There are undoubtedly serious bugs lurking somewhere in this code :-) Bug reports and other feedback are most welcome.
Copyright (c) 2001, Damian Conway. All Rights Reserved. This module is free software. It may be used, redistributed and/or modified under the same terms as Perl itself.
To install Attribute::Types, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Attribute::Types
CPAN shell
perl -MCPAN -e shell install Attribute::Types
For more information on module installation, please visit the detailed CPAN module installation guide.