Types::Core - Core types defined as tests and literals (ease of use)
Version "0.1.2";
my @ref_types = (ARRAY CODE GLOB HASH IO REF SCALAR); my $ref = $_[0]; P "Error: expected %s", HASH unless HASH $ref;
Syntax symplifier for type checking.
Allows easy, non-quoted usage of types as literals, and allows the standard type names to be used as true/false check routines of references.
TYPE <Ref> - Check if Ref has underlying type, TYPE
TYPE <Ref
TYPE - Literal usage equal to itself
TYPE
printf "type = %s\n", HASH if HASH $var;
Same as:
printf "type = %s\n", 'HASH' if ref $var eq 'HASH';)
For the most basic functions listed in the Synopsis, they take either 0 or 1 arguments. If 1 parameter, then they test it to see if the ref is of the given type (blessed or not) & returns true or false.
ref
For no args, they return literals of themselves, allowing the named strings to be used as Literals w/o quotes.
EhV $hashref, FIELDNAME; # Exist[in]hash? Value : undef
If fieldname exists in the HASH pointed to by hashref, return the value, else returns undef.
typ REF; #return underlying type of REF
Just as c<ref> returns the name of the package or class of a reference, it had to start out with a reference to one of the basic types. That's the value returned by typ. Note: use of this function violates object integrity by "peeking under the hood" at how class is implemented.
typ
blessed REF; #is REF blessed or not?
Included for it's usefulness in type checking. Similar functionality as implemented in Scalar::Util (uses Scalar::Util if available, though it is not needed).
Scalar::Util
In order to prevent automatic creation of variables, when accessed or tested for undef, (autovivification), one must test for existence first, before attempting to read the value.
undef
This results in a 2 step process to retrive a value:
exists $name{$testname} ? $name{testname}:undef;
If you have multiple levels of hash tables say retrieving SSN's via {$lastname}{$firstname} in object member 'name2ssns' but don't know if the object member is valid or not, you could have nested code:
my $p=$this; if (exists $p->{name2ssns}) { $p=$p->{name2ssns}; if (exists $p->{$lastname}) { $p=$p->{$lastname}; if (exists $p->{$firstname}) { return $p->{$firstname}; } } } return undef;
Instead EhV saves 1 step. Instead of having to test then reference the value to return it, it returns the value if it exists, else it returns undef. Thus, the above could be written:
my $p=$this; return $p = EhV $p, name2ssns and $p = EhV $p, $lastname and EhV $p, $firstname;
This not only saves coding space & time, but allows faster comprehension of what is going on (presuming familiarity with EhV).
EhV
To install Types::Core, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Types::Core
CPAN shell
perl -MCPAN -e shell install Types::Core
For more information on module installation, please visit the detailed CPAN module installation guide.