home *** CD-ROM | disk | FTP | other *** search
/ BURKS 2 / BURKS_AUG97.ISO / SLAKWARE / D13 / PERL2.TGZ / perl2.tar / usr / lib / perl5 / pod / perldata.pod < prev    next >
Text File  |  1996-06-28  |  21KB  |  522 lines

  1. =head1 NAME
  2.  
  3. perldata - Perl data types
  4.  
  5. =head1 DESCRIPTION
  6.  
  7. =head2 Variable names
  8.  
  9. Perl has three data structures: scalars, arrays of scalars, and
  10. associative arrays of scalars, known as "hashes".  Normal arrays are
  11. indexed by number, starting with 0.  (Negative subscripts count from
  12. the end.)  Hash arrays are indexed by string.
  13.  
  14. Scalar values are always named with '$', even when referring to a scalar
  15. that is part of an array.  It works like the English word "the".  Thus
  16. we have:
  17.  
  18.     $days        # the simple scalar value "days"
  19.     $days[28]        # the 29th element of array @days
  20.     $days{'Feb'}    # the 'Feb' value from hash %days
  21.     $#days        # the last index of array @days
  22.  
  23. but entire arrays or array slices are denoted by '@', which works much like
  24. the word "these" or "those":
  25.  
  26.     @days        # ($days[0], $days[1],... $days[n])
  27.     @days[3,4,5]    # same as @days[3..5]
  28.     @days{'a','c'}    # same as ($days{'a'},$days{'c'})
  29.  
  30. and entire hashes are denoted by '%':
  31.  
  32.     %days        # (key1, val1, key2, val2 ...)
  33.  
  34. In addition, subroutines are named with an initial '&', though this is
  35. optional when it's otherwise unambiguous (just as "do" is often
  36. redundant in English).  Symbol table entries can be named with an
  37. initial '*', but you don't really care about that yet.
  38.  
  39. Every variable type has its own namespace.  You can, without fear of
  40. conflict, use the same name for a scalar variable, an array, or a hash
  41. (or, for that matter, a filehandle, a subroutine name, or a label).
  42. This means that $foo and @foo are two different variables.  It also
  43. means that C<$foo[1]> is a part of @foo, not a part of $foo.  This may
  44. seem a bit weird, but that's okay, because it is weird.
  45.  
  46. Since variable and array references always start with '$', '@', or '%',
  47. the "reserved" words aren't in fact reserved with respect to variable
  48. names.  (They ARE reserved with respect to labels and filehandles,
  49. however, which don't have an initial special character.  You can't have
  50. a filehandle named "log", for instance.  Hint: you could say
  51. C<open(LOG,'logfile')> rather than C<open(log,'logfile')>.  Using uppercase
  52. filehandles also improves readability and protects you from conflict
  53. with future reserved words.)  Case I<IS> significant--"FOO", "Foo" and
  54. "foo" are all different names.  Names that start with a letter or
  55. underscore may also contain digits and underscores.
  56.  
  57. It is possible to replace such an alphanumeric name with an expression
  58. that returns a reference to an object of that type.  For a description
  59. of this, see L<perlref>.
  60.  
  61. Names that start with a digit may only contain more digits.  Names
  62. which do not start with a letter, underscore,  or digit are limited to
  63. one character, e.g.  C<$%> or C<$$>.  (Most of these one character names
  64. have a predefined significance to Perl.  For instance, C<$$> is the
  65. current process id.)
  66.  
  67. =head2 Context
  68.  
  69. The interpretation of operations and values in Perl sometimes depends
  70. on the requirements of the context around the operation or value.
  71. There are two major contexts: scalar and list.  Certain operations
  72. return list values in contexts wanting a list, and scalar values
  73. otherwise.  (If this is true of an operation it will be mentioned in
  74. the documentation for that operation.)  In other words, Perl overloads
  75. certain operations based on whether the expected return value is
  76. singular or plural.  (Some words in English work this way, like "fish"
  77. and "sheep".)
  78.  
  79. In a reciprocal fashion, an operation provides either a scalar or a
  80. list context to each of its arguments.  For example, if you say
  81.  
  82.     int( <STDIN> )
  83.  
  84. the integer operation provides a scalar context for the <STDIN>
  85. operator, which responds by reading one line from STDIN and passing it
  86. back to the integer operation, which will then find the integer value
  87. of that line and return that.  If, on the other hand, you say
  88.  
  89.     sort( <STDIN> )
  90.  
  91. then the sort operation provides a list context for <STDIN>, which
  92. will proceed to read every line available up to the end of file, and
  93. pass that list of lines back to the sort routine, which will then
  94. sort those lines and return them as a list to whatever the context
  95. of the sort was.
  96.  
  97. Assignment is a little bit special in that it uses its left argument to
  98. determine the context for the right argument.  Assignment to a scalar
  99. evaluates the righthand side in a scalar context, while assignment to
  100. an array or array slice evaluates the righthand side in a list
  101. context.  Assignment to a list also evaluates the righthand side in a
  102. list context.
  103.  
  104. User defined subroutines may choose to care whether they are being
  105. called in a scalar or list context, but most subroutines do not
  106. need to care, because scalars are automatically interpolated into
  107. lists.  See L<perlfunc/wantarray>.
  108.  
  109. =head2 Scalar values
  110.  
  111. All data in Perl is a scalar or an array of scalars or a hash of scalars.
  112. Scalar variables may contain various kinds of singular data, such as
  113. numbers, strings, and references.  In general, conversion from one form to
  114. another is transparent.  (A scalar may not contain multiple values, but
  115. may contain a reference to an array or hash containing multiple values.)
  116. Because of the automatic conversion of scalars, operations and functions
  117. that return scalars don't need to care (and, in fact, can't care) whether
  118. the context is looking for a string or a number.
  119.  
  120. Scalars aren't necessarily one thing or another.  There's no place to
  121. declare a scalar variable to be of type "string", or of type "number", or
  122. type "filehandle", or anything else.  Perl is a contextually polymorphic
  123. language whose scalars can be strings, numbers, or references (which
  124. includes objects).  While strings and numbers are considered pretty
  125. much same thing for nearly all purposes, references are strongly-typed
  126. uncastable pointers with built-in reference-counting and destructor
  127. invocation.
  128.  
  129. A scalar value is interpreted as TRUE in the Boolean sense if it is not
  130. the null string or the number 0 (or its string equivalent, "0").  The
  131. Boolean context is just a special kind of scalar context.  
  132.  
  133. There are actually two varieties of null scalars: defined and
  134. undefined.  Undefined null scalars are returned when there is no real
  135. value for something, such as when there was an error, or at end of
  136. file, or when you refer to an uninitialized variable or element of an
  137. array.  An undefined null scalar may become defined the first time you
  138. use it as if it were defined, but prior to that you can use the
  139. defined() operator to determine whether the value is defined or not.
  140.  
  141. To find out whether a given string is a valid non-zero number, it's usually
  142. enough to test it against both numeric 0 and also lexical "0" (although
  143. this will cause B<-w> noises).  That's because strings that aren't
  144. numbers count as 0, just as the do in I<awk>:
  145.  
  146.     if ($str == 0 && $str ne "0")  {
  147.     warn "That doesn't look like a number";
  148.     } 
  149.  
  150. That's usually preferable because otherwise you won't treat IEEE notations
  151. like C<NaN> or C<Infinity> properly.  At other times you might prefer to
  152. use a regular expression to check whether data is numeric.  See L<perlre>
  153. for details on regular expressions.
  154.  
  155.     warn "has nondigits"    if     /\D/;
  156.     warn "not a whole number"   unless /^\d+$/;
  157.     warn "not an integer"       unless /^[+-]?\d+$/     
  158.     warn "not a decimal number" unless /^[+-]?\d+\.?\d*$/ 
  159.     warn "not a C float" 
  160.     unless /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/;
  161.  
  162. The length of an array is a scalar value.  You may find the length of
  163. array @days by evaluating C<$#days>, as in B<csh>.  (Actually, it's not
  164. the length of the array, it's the subscript of the last element, since
  165. there is (ordinarily) a 0th element.)  Assigning to C<$#days> changes the
  166. length of the array.  Shortening an array by this method destroys
  167. intervening values.  Lengthening an array that was previously shortened
  168. I<NO LONGER> recovers the values that were in those elements.  (It used to
  169. in Perl 4, but we had to break this make to make sure destructors were
  170. called when expected.)  You can also gain some measure of efficiency by
  171. preextending an array that is going to get big.  (You can also extend
  172. an array by assigning to an element that is off the end of the array.)
  173. You can truncate an array down to nothi