GSP
Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Support
Contact Us
Online Help
Handbooks
Domain Status
Man Pages

FAQ
Virtual Servers
Pricing
Billing
Technical

Network
Facilities
Connectivity
Topology Map

Miscellaneous
Server Agreement
Year 2038
Credits
 

USA Flag

 

 

Man Pages


Manual Reference Pages  -  TYPES::CORE (3)

.ds Aq ’

NAME

Types::Core - Core types defined as tests and literals (ease of use)

CONTENTS

VERSION

Version 0.1.8;

SYNOPSIS



  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.

USAGE

<B>B>TYPE <Ref<B>B>> - Check if Ref has underlying type, TYPE

<B>B>TYPE<B>B> - Literal usage equal to itself

Example



  printf "type = %s\n", HASH if HASH $var;



Same as:



  printf "type = %s\n", HASH if ref $var eq HASH;)



DESCRIPTION

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). If false, undef is returned, of true, the ref, itself is returned.

For no args, they return literals of themselves, allowing the named strings to be used as Literals w/o quotes.

More Examples

Initialization



  our %field_types = (Paths{type => ARRAY, ...});



Flow Routing



    ...
    return statAR_2_Ino_t($path,$arg)    if $ref_arg eq ARRAY;
    return stat_t_2_Ino_t($path, $arg)   if $ref_arg eq stat_t }
  else { _path_2_Ino_t($path) }



Data Verification



  sub Type_check($;$) { ...
    if (ARRAY $cfp) {
      for (@$cfp) {
        die P "Field %s does not exist", $_ unless exists $v->{$_};
        my $cls_ftpp = $class."::field_types";
        if (HASH $cls_ftpp) {
          if ($cls_ftpp->{type} eq ARRAY) {  ...



Param Checking



  sub popable (+) {
    my $ar=$_[0];
    ARRAY $ar or die P "popable only works with arrays, got %s", ref $ar;



Return Value Checks and Dereference Protection



  my $Inos = $mp->get_sorted_Ino_t_Array;
  return undef unless ARRAY $Inos and @$Inos >= 2;



Helper/Useful shorthand Functions



     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.



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

EhV Example

In order to prevent automatic creation of variables when accessed or tested for undef, (i.e. autovivification), one must test for existence first, before attempting to read or test the ’defined’-ness of the value.

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

Compatibility Note with Perl 5.12.5 and earlier

In order for earlier perls to parse things correctly parentheses may be needed if unrelated arguments follow the <B>typeB> tests.

Search for    or go to Top of page |  Section 3 |  Main Index


perl v5.20.3 TYPES::CORE (3) 2015-07-05

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with manServer 1.07.