Attribute::Types - Attributes that confer type on variables |
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
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
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
ref($value) eq 'SCALAR'
).
ARRAY
ref($value) eq 'ARRAY'
).
HASH
ref($value) eq 'HASH'
).
CODE
ref($value) eq 'CODE'
).
GLOB
ref($value) eq 'GLOB'
).
REF
ref($value) eq 'REF'
).
REGEX
ref($value) eq 'Regexp'
).
Type
Type(Class::Name)
Type(/pattern/)
Type(&subname)
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
Can't specify type attribute for CODE
Invalid type specifier: Type(garbage)
Type(...)
attribute can only be specified with a class name, a
pattern (in /.../), or a subroutine name.
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.
Attribute::Types - Attributes that confer type on variables |