home *** CD-ROM | disk | FTP | other *** search
/ IRIX Base Documentation 2001 May / SGI IRIX Base Documentation 2001 May.iso / usr / share / catman / p_man / cat3 / perl5 / Class::Struct.z / Class::Struct
Encoding:
Text File  |  1998-10-30  |  12.3 KB  |  397 lines

  1.  
  2.  
  3.  
  4. CCCCllllaaaassssssss::::::::SSSSttttrrrruuuucccctttt((((3333))))                                              CCCCllllaaaassssssss::::::::SSSSttttrrrruuuucccctttt((((3333))))
  5.  
  6.  
  7.  
  8. NNNNAAAAMMMMEEEE
  9.      Class::Struct - declare struct-like datatypes as Perl classes
  10.  
  11. SSSSYYYYNNNNOOOOPPPPSSSSIIIISSSS
  12.          use Class::Struct;
  13.                  # declare struct, based on array:
  14.          struct( CLASS_NAME => [ ELEMENT_NAME => ELEMENT_TYPE, ... ]);
  15.                  # declare struct, based on hash:
  16.          struct( CLASS_NAME => { ELEMENT_NAME => ELEMENT_TYPE, ... });
  17.  
  18.          package CLASS_NAME;
  19.          use Class::Struct;
  20.                  # declare struct, based on array, implicit class name:
  21.          struct( ELEMENT_NAME => ELEMENT_TYPE, ... );
  22.  
  23.          package Myobj;
  24.          use Class::Struct;
  25.                  # declare struct with four types of elements:
  26.          struct( s => '$', a => '@', h => '%', c => 'My_Other_Class' );
  27.  
  28.          $obj = new Myobj;               # constructor
  29.  
  30.                                          # scalar type accessor:
  31.          $element_value = $obj->s;           # element value
  32.          $obj->s('new value');               # assign to element
  33.  
  34.                                          # array type accessor:
  35.          $ary_ref = $obj->a;                 # reference to whole array
  36.          $ary_element_value = $obj->a(2);    # array element value
  37.          $obj->a(2, 'new value');            # assign to array element
  38.  
  39.                                          # hash type accessor:
  40.          $hash_ref = $obj->h;                # reference to whole hash
  41.          $hash_element_value = $obj->h('x'); # hash element value
  42.          $obj->h('x', 'new value');        # assign to hash element
  43.  
  44.                                          # class type accessor:
  45.          $element_value = $obj->c;           # object reference
  46.          $obj->c->method(...);               # call method of object
  47.          $obj->c(new My_Other_Class);        # assign a new object
  48.  
  49.  
  50. DDDDEEEESSSSCCCCRRRRIIIIPPPPTTTTIIIIOOOONNNN
  51.      Class::Struct exports a single function, struct.  Given a list of element
  52.      names and types, and optionally a class name, struct creates a Perl 5
  53.      class that implements a "struct-like" data structure.
  54.  
  55.      The new class is given a constructor method, new, for creating struct
  56.      objects.
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.                                                                         PPPPaaaaggggeeee 1111
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70. CCCCllllaaaassssssss::::::::SSSSttttrrrruuuucccctttt((((3333))))                                              CCCCllllaaaassssssss::::::::SSSSttttrrrruuuucccctttt((((3333))))
  71.  
  72.  
  73.  
  74.      Each element in the struct data has an accessor method, which is used to
  75.      assign to the element and to fetch its value.  The default accessor can
  76.      be overridden by declaring a sub of the same name in the package.  (See
  77.      Example 2.)
  78.  
  79.      Each element's type can be scalar, array, hash, or class.
  80.  
  81.      TTTThhhheeee ssssttttrrrruuuucccctttt(((()))) function
  82.  
  83.      The struct function has three forms of parameter-list.
  84.  
  85.          struct( CLASS_NAME => [ ELEMENT_LIST ]);
  86.          struct( CLASS_NAME => { ELEMENT_LIST });
  87.          struct( ELEMENT_LIST );
  88.  
  89.      The first and second forms explicitly identify the name of the class
  90.      being created.  The third form assumes the current package name as the
  91.      class name.
  92.  
  93.      An object of a class created by the first and third forms is based on an
  94.      array, whereas an object of a class created by the second form is based
  95.      on a hash. The array-based forms will be somewhat faster and smaller; the
  96.      hash-based forms are more flexible.
  97.  
  98.      The class created by struct must not be a subclass of another class other
  99.      than UNIVERSAL.
  100.  
  101.      A function named new must not be explicitly defined in a class created by
  102.      struct.
  103.  
  104.      The _E_L_E_M_E_N_T__L_I_S_T has the form
  105.  
  106.          NAME => TYPE, ...
  107.  
  108.      Each name-type pair declares one element of the struct. Each element name
  109.      will be defined as an accessor method unless a method by that name is
  110.      explicitly defined; in the latter case, a warning is issued if the
  111.      warning flag (----wwww) is set.
  112.  
  113.      EEEElllleeeemmmmeeeennnntttt TTTTyyyyppppeeeessss aaaannnndddd AAAAcccccccceeeessssssssoooorrrr MMMMeeeetttthhhhooooddddssss
  114.  
  115.      The four element types -- scalar, array, hash, and class -- are
  116.      represented by strings -- '$', '@', '%', and a class name -- optionally
  117.      preceded by a '*'.
  118.  
  119.      The accessor method provided by struct for an element depends on the
  120.      declared type of the element.
  121.  
  122.      Scalar ('$' or '*$')
  123.           The element is a scalar, and is initialized to undef.
  124.  
  125.           The accessor's argument, if any, is assigned to the element.
  126.  
  127.  
  128.  
  129.                                                                         PPPPaaaaggggeeee 2222
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136. CCCCllllaaaassssssss::::::::SSSSttttrrrruuuucccctttt((((3333))))                                              CCCCllllaaaassssssss::::::::SSSSttttrrrruuuucccctttt((((3333))))
  137.  
  138.  
  139.  
  140.           If the element type is '$', the value of the element (after
  141.           assignment) is returned. If the element type is '*$', a reference to
  142.           the element is returned.
  143.  
  144.      Array ('@' or '*@')
  145.           The element is an array, initialized to ().
  146.  
  147.           With no argument, the accessor returns a reference to the element's
  148.           whole array.
  149.  
  150.           With one or two arguments, the first argument is an index specifying
  151.           one element of the array; the second argument, if present, is
  152.           assigned to the array element.  If the element type is '@', the
  153.           accessor returns the array element value.  If the element type is
  154.           '*@', a reference to the array element is returned.
  155.  
  156.      Hash ('%' or '*%')
  157.           The element is a hash, initialized to ().
  158.  
  159.           With no argument, the accessor returns a reference to the element's
  160.           whole hash.
  161.  
  162.           With one or two arguments, the first argument is a key specifying
  163.           one element of the hash; the second argument, if present, is
  164.           assigned to the hash element.  If the element type is '%', the
  165.           accessor returns the hash element value.  If the element type is
  166.           '*%', a reference to the hash element is returned.
  167.  
  168.      Class ('Class_Name' or '*Class_Name')
  169.           The element's value must be a reference blessed to the named class
  170.           or to one of its subclasses. The element is initialized to the
  171.           result of calling the new constructor of the named class.
  172.  
  173.           The accessor's argument, if any, is assigned to the element. The
  174.           accessor will croak if this is not an appropriate object reference.
  175.  
  176.           If the element type does not start with a '*', the accessor returns
  177.           the element value (after assignment). If the element type starts
  178.           with a '*', a reference to the element itself is returned.
  179.  
  180. EEEEXXXXAAAAMMMMPPPPLLLLEEEESSSS
  181.      Example 1
  182.           Giving a struct element a class type that is also a struct is how
  183.           structs are nested.  Here, timeval represents a time (seconds and
  184.           microseconds), and rusage has two elements, each of which is of type
  185.           timeval.
  186.  
  187.               use Class::Struct;
  188.  
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.                                                                         PPPPaaaaggggeeee 3333
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202. CCCCllllaaaassssssss::::::::SSSSttttrrrruuuucccctttt((((3333))))                                              CCCCllllaaaassssssss::::::::SSSSttttrrrruuuucccctttt((((3333))))
  203.  
  204.  
  205.  
  206.               struct( rusage => {
  207.                   ru_utime => timeval,  # seconds
  208.                   ru_stime => timeval,  # microseconds
  209.               });
  210.  
  211.               struct( timeval => [
  212.                   tv_secs  => '$',
  213.                   tv_usecs => '$',
  214.               ]);
  215.  
  216.                   # create an object:
  217.               my $t = new rusage;
  218.                   # $t->ru_utime and $t->ru_stime are objects of type timeval.
  219.  
  220.                   # set $t->ru_utime to 100.0 sec and $t->ru_stime to 5.0 sec.
  221.               $t->ru_utime->tv_secs(100);
  222.               $t->ru_utime->tv_usecs(0);
  223.               $t->ru_stime->tv_secs(5);
  224.               $t->ru_stime->tv_usecs(0);
  225.  
  226.  
  227.      Example 2
  228.           An accessor function can be redefined in order to provide additional
  229.           checking of values, etc.  Here, we want the count element always to
  230.           be nonnegative, so we redefine the count accessor accordingly.
  231.  
  232.               package MyObj;
  233.               use Class::Struct;
  234.  
  235.                           # declare the struct
  236.               struct ( 'MyObj', { count => '$', stuff => '%' } );
  237.  
  238.                           # override the default accessor method for 'count'
  239.               sub count {
  240.                   my $self = shift;
  241.                   if ( @_ ) {
  242.                       die 'count must be nonnegative' if $_[0] < 0;
  243.                       $self->{'count'} = shift;
  244.                       warn "Too many args to count" if @_;
  245.                   }
  246.                   return $self->{'count'};
  247.               }
  248.  
  249.               package main;
  250.               $x = new MyObj;
  251.               print "\$x->count(5) = ", $x->count(5), "\n";
  252.                                       # prints '$x->count(5) = 5'
  253.  
  254.               print "\$x->count = ", $x->count, "\n";
  255.                                       # prints '$x->count = 5'
  256.  
  257.  
  258.  
  259.  
  260.  
  261.                                                                         PPPPaaaaggggeeee 4444
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268. CCCCllllaaaassssssss::::::::SSSSttttrrrruuuucccctttt((((3333))))                                              CCCCllllaaaassssssss::::::::SSSSttttrrrruuuucccctttt((((3333))))
  269.  
  270.  
  271.  
  272.               print "\$x->count(-5) = ", $x->count(-5), "\n";
  273.                                       # dies due to negative argument!
  274.  
  275.  
  276. AAAAuuuutttthhhhoooorrrr aaaannnndddd MMMMooooddddiiiiffffiiiiccccaaaattttiiiioooonnnn HHHHiiiissssttttoooorrrryyyy
  277.      Renamed to Class::Struct and modified by Jim Miner, 1997-04-02.
  278.  
  279.          members() function removed.
  280.          Documentation corrected and extended.
  281.          Use of struct() in a subclass prohibited.
  282.          User definition of accessor allowed.
  283.          Treatment of '*' in element types corrected.
  284.          Treatment of classes as element types corrected.
  285.          Class name to struct() made optional.
  286.          Diagnostic checks added.
  287.  
  288.      Originally Class::Template by Dean Roehrich.
  289.  
  290.          # Template.pm   --- struct/member template builder
  291.          #   12mar95
  292.          #   Dean Roehrich
  293.          #
  294.          # changes/bugs fixed since 28nov94 version:
  295.          #  - podified
  296.          # changes/bugs fixed since 21nov94 version:
  297.          #  - Fixed examples.
  298.          # changes/bugs fixed since 02sep94 version:
  299.          #  - Moved to Class::Template.
  300.          # changes/bugs fixed since 20feb94 version:
  301.          #  - Updated to be a more proper module.
  302.          #  - Added "use strict".
  303.          #  - Bug in build_methods, was using @var when @$var needed.
  304.          #  - Now using my() rather than local().
  305.          #
  306.          # Uses perl5 classes to create nested data types.
  307.          # This is offered as one implementation of Tom Christiansen's "structs.pl"
  308.          # idea.
  309.  
  310.  
  311.  
  312.  
  313.  
  314.  
  315.  
  316.  
  317.  
  318.  
  319.  
  320.  
  321.  
  322.  
  323.  
  324.  
  325.  
  326.  
  327.                                                                         PPPPaaaaggggeeee 5555
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334. CCCCllllaaaassssssss::::::::SSSSttttrrrruuuucccctttt((((3333))))                                              CCCCllllaaaassssssss::::::::SSSSttttrrrruuuucccctttt((((3333))))
  335.  
  336.  
  337.  
  338.  
  339.  
  340.  
  341.  
  342.  
  343.  
  344.  
  345.  
  346.  
  347.  
  348.  
  349.  
  350.  
  351.  
  352.  
  353.  
  354.  
  355.  
  356.  
  357.  
  358.  
  359.  
  360.  
  361.  
  362.  
  363.  
  364.  
  365.  
  366.  
  367.  
  368.  
  369.  
  370.  
  371.  
  372.  
  373.  
  374.  
  375.  
  376.  
  377.  
  378.  
  379.  
  380.  
  381.  
  382.  
  383.  
  384.  
  385.  
  386.  
  387.  
  388.  
  389.  
  390.                                                                         PPPPaaaaggggeeee 6666
  391.  
  392.  
  393.  
  394.  
  395.  
  396.  
  397.