1*0Sstevel@tonic-gate=head1 NAME 2*0Sstevel@tonic-gate 3*0Sstevel@tonic-gateperldata - Perl data types 4*0Sstevel@tonic-gate 5*0Sstevel@tonic-gate=head1 DESCRIPTION 6*0Sstevel@tonic-gate 7*0Sstevel@tonic-gate=head2 Variable names 8*0Sstevel@tonic-gate 9*0Sstevel@tonic-gatePerl has three built-in data types: scalars, arrays of scalars, and 10*0Sstevel@tonic-gateassociative arrays of scalars, known as "hashes". A scalar is a 11*0Sstevel@tonic-gatesingle string (of any size, limited only by the available memory), 12*0Sstevel@tonic-gatenumber, or a reference to something (which will be discussed 13*0Sstevel@tonic-gatein L<perlref>). Normal arrays are ordered lists of scalars indexed 14*0Sstevel@tonic-gateby number, starting with 0. Hashes are unordered collections of scalar 15*0Sstevel@tonic-gatevalues indexed by their associated string key. 16*0Sstevel@tonic-gate 17*0Sstevel@tonic-gateValues are usually referred to by name, or through a named reference. 18*0Sstevel@tonic-gateThe first character of the name tells you to what sort of data 19*0Sstevel@tonic-gatestructure it refers. The rest of the name tells you the particular 20*0Sstevel@tonic-gatevalue to which it refers. Usually this name is a single I<identifier>, 21*0Sstevel@tonic-gatethat is, a string beginning with a letter or underscore, and 22*0Sstevel@tonic-gatecontaining letters, underscores, and digits. In some cases, it may 23*0Sstevel@tonic-gatebe a chain of identifiers, separated by C<::> (or by the slightly 24*0Sstevel@tonic-gatearchaic C<'>); all but the last are interpreted as names of packages, 25*0Sstevel@tonic-gateto locate the namespace in which to look up the final identifier 26*0Sstevel@tonic-gate(see L<perlmod/Packages> for details). It's possible to substitute 27*0Sstevel@tonic-gatefor a simple identifier, an expression that produces a reference 28*0Sstevel@tonic-gateto the value at runtime. This is described in more detail below 29*0Sstevel@tonic-gateand in L<perlref>. 30*0Sstevel@tonic-gate 31*0Sstevel@tonic-gatePerl also has its own built-in variables whose names don't follow 32*0Sstevel@tonic-gatethese rules. They have strange names so they don't accidentally 33*0Sstevel@tonic-gatecollide with one of your normal variables. Strings that match 34*0Sstevel@tonic-gateparenthesized parts of a regular expression are saved under names 35*0Sstevel@tonic-gatecontaining only digits after the C<$> (see L<perlop> and L<perlre>). 36*0Sstevel@tonic-gateIn addition, several special variables that provide windows into 37*0Sstevel@tonic-gatethe inner working of Perl have names containing punctuation characters 38*0Sstevel@tonic-gateand control characters. These are documented in L<perlvar>. 39*0Sstevel@tonic-gate 40*0Sstevel@tonic-gateScalar values are always named with '$', even when referring to a 41*0Sstevel@tonic-gatescalar that is part of an array or a hash. The '$' symbol works 42*0Sstevel@tonic-gatesemantically like the English word "the" in that it indicates a 43*0Sstevel@tonic-gatesingle value is expected. 44*0Sstevel@tonic-gate 45*0Sstevel@tonic-gate $days # the simple scalar value "days" 46*0Sstevel@tonic-gate $days[28] # the 29th element of array @days 47*0Sstevel@tonic-gate $days{'Feb'} # the 'Feb' value from hash %days 48*0Sstevel@tonic-gate $#days # the last index of array @days 49*0Sstevel@tonic-gate 50*0Sstevel@tonic-gateEntire arrays (and slices of arrays and hashes) are denoted by '@', 51*0Sstevel@tonic-gatewhich works much like the word "these" or "those" does in English, 52*0Sstevel@tonic-gatein that it indicates multiple values are expected. 53*0Sstevel@tonic-gate 54*0Sstevel@tonic-gate @days # ($days[0], $days[1],... $days[n]) 55*0Sstevel@tonic-gate @days[3,4,5] # same as ($days[3],$days[4],$days[5]) 56*0Sstevel@tonic-gate @days{'a','c'} # same as ($days{'a'},$days{'c'}) 57*0Sstevel@tonic-gate 58*0Sstevel@tonic-gateEntire hashes are denoted by '%': 59*0Sstevel@tonic-gate 60*0Sstevel@tonic-gate %days # (key1, val1, key2, val2 ...) 61*0Sstevel@tonic-gate 62*0Sstevel@tonic-gateIn addition, subroutines are named with an initial '&', though this 63*0Sstevel@tonic-gateis optional when unambiguous, just as the word "do" is often redundant 64*0Sstevel@tonic-gatein English. Symbol table entries can be named with an initial '*', 65*0Sstevel@tonic-gatebut you don't really care about that yet (if ever :-). 66*0Sstevel@tonic-gate 67*0Sstevel@tonic-gateEvery variable type has its own namespace, as do several 68*0Sstevel@tonic-gatenon-variable identifiers. This means that you can, without fear 69*0Sstevel@tonic-gateof conflict, use the same name for a scalar variable, an array, or 70*0Sstevel@tonic-gatea hash--or, for that matter, for a filehandle, a directory handle, a 71*0Sstevel@tonic-gatesubroutine name, a format name, or a label. This means that $foo 72*0Sstevel@tonic-gateand @foo are two different variables. It also means that C<$foo[1]> 73*0Sstevel@tonic-gateis a part of @foo, not a part of $foo. This may seem a bit weird, 74*0Sstevel@tonic-gatebut that's okay, because it is weird. 75*0Sstevel@tonic-gate 76*0Sstevel@tonic-gateBecause variable references always start with '$', '@', or '%', the 77*0Sstevel@tonic-gate"reserved" words aren't in fact reserved with respect to variable 78*0Sstevel@tonic-gatenames. They I<are> reserved with respect to labels and filehandles, 79*0Sstevel@tonic-gatehowever, which don't have an initial special character. You can't 80*0Sstevel@tonic-gatehave a filehandle named "log", for instance. Hint: you could say 81*0Sstevel@tonic-gateC<open(LOG,'logfile')> rather than C<open(log,'logfile')>. Using 82*0Sstevel@tonic-gateuppercase filehandles also improves readability and protects you 83*0Sstevel@tonic-gatefrom conflict with future reserved words. Case I<is> significant--"FOO", 84*0Sstevel@tonic-gate"Foo", and "foo" are all different names. Names that start with a 85*0Sstevel@tonic-gateletter or underscore may also contain digits and underscores. 86*0Sstevel@tonic-gate 87*0Sstevel@tonic-gateIt is possible to replace such an alphanumeric name with an expression 88*0Sstevel@tonic-gatethat returns a reference to the appropriate type. For a description 89*0Sstevel@tonic-gateof this, see L<perlref>. 90*0Sstevel@tonic-gate 91*0Sstevel@tonic-gateNames that start with a digit may contain only more digits. Names 92*0Sstevel@tonic-gatethat do not start with a letter, underscore, digit or a caret (i.e. 93*0Sstevel@tonic-gatea control character) are limited to one character, e.g., C<$%> or 94*0Sstevel@tonic-gateC<$$>. (Most of these one character names have a predefined 95*0Sstevel@tonic-gatesignificance to Perl. For instance, C<$$> is the current process 96*0Sstevel@tonic-gateid.) 97*0Sstevel@tonic-gate 98*0Sstevel@tonic-gate=head2 Context 99*0Sstevel@tonic-gate 100*0Sstevel@tonic-gateThe interpretation of operations and values in Perl sometimes depends 101*0Sstevel@tonic-gateon the requirements of the context around the operation or value. 102*0Sstevel@tonic-gateThere are two major contexts: list and scalar. Certain operations 103*0Sstevel@tonic-gatereturn list values in contexts wanting a list, and scalar values 104*0Sstevel@tonic-gateotherwise. If this is true of an operation it will be mentioned in 105*0Sstevel@tonic-gatethe documentation for that operation. In other words, Perl overloads 106*0Sstevel@tonic-gatecertain operations based on whether the expected return value is 107*0Sstevel@tonic-gatesingular or plural. Some words in English work this way, like "fish" 108*0Sstevel@tonic-gateand "sheep". 109*0Sstevel@tonic-gate 110*0Sstevel@tonic-gateIn a reciprocal fashion, an operation provides either a scalar or a 111*0Sstevel@tonic-gatelist context to each of its arguments. For example, if you say 112*0Sstevel@tonic-gate 113*0Sstevel@tonic-gate int( <STDIN> ) 114*0Sstevel@tonic-gate 115*0Sstevel@tonic-gatethe integer operation provides scalar context for the <> 116*0Sstevel@tonic-gateoperator, which responds by reading one line from STDIN and passing it 117*0Sstevel@tonic-gateback to the integer operation, which will then find the integer value 118*0Sstevel@tonic-gateof that line and return that. If, on the other hand, you say 119*0Sstevel@tonic-gate 120*0Sstevel@tonic-gate sort( <STDIN> ) 121*0Sstevel@tonic-gate 122*0Sstevel@tonic-gatethen the sort operation provides list context for <>, which 123*0Sstevel@tonic-gatewill proceed to read every line available up to the end of file, and 124*0Sstevel@tonic-gatepass that list of lines back to the sort routine, which will then 125*0Sstevel@tonic-gatesort those lines and return them as a list to whatever the context 126*0Sstevel@tonic-gateof the sort was. 127*0Sstevel@tonic-gate 128*0Sstevel@tonic-gateAssignment is a little bit special in that it uses its left argument 129*0Sstevel@tonic-gateto determine the context for the right argument. Assignment to a 130*0Sstevel@tonic-gatescalar evaluates the right-hand side in scalar context, while 131*0Sstevel@tonic-gateassignment to an array or hash evaluates the righthand side in list 132*0Sstevel@tonic-gatecontext. Assignment to a list (or slice, which is just a list 133*0Sstevel@tonic-gateanyway) also evaluates the righthand side in list context. 134*0Sstevel@tonic-gate 135*0Sstevel@tonic-gateWhen you use the C<use warnings> pragma or Perl's B<-w> command-line 136*0Sstevel@tonic-gateoption, you may see warnings 137*0Sstevel@tonic-gateabout useless uses of constants or functions in "void context". 138*0Sstevel@tonic-gateVoid context just means the value has been discarded, such as a 139*0Sstevel@tonic-gatestatement containing only C<"fred";> or C<getpwuid(0);>. It still 140*0Sstevel@tonic-gatecounts as scalar context for functions that care whether or not 141*0Sstevel@tonic-gatethey're being called in list context. 142*0Sstevel@tonic-gate 143*0Sstevel@tonic-gateUser-defined subroutines may choose to care whether they are being 144*0Sstevel@tonic-gatecalled in a void, scalar, or list context. Most subroutines do not 145*0Sstevel@tonic-gateneed to bother, though. That's because both scalars and lists are 146*0Sstevel@tonic-gateautomatically interpolated into lists. See L<perlfunc/wantarray> 147*0Sstevel@tonic-gatefor how you would dynamically discern your function's calling 148*0Sstevel@tonic-gatecontext. 149*0Sstevel@tonic-gate 150*0Sstevel@tonic-gate=head2 Scalar values 151*0Sstevel@tonic-gate 152*0Sstevel@tonic-gateAll data in Perl is a scalar, an array of scalars, or a hash of 153*0Sstevel@tonic-gatescalars. A scalar may contain one single value in any of three 154*0Sstevel@tonic-gatedifferent flavors: a number, a string, or a reference. In general, 155*0Sstevel@tonic-gateconversion from one form to another is transparent. Although a 156*0Sstevel@tonic-gatescalar may not directly hold multiple values, it may contain a 157*0Sstevel@tonic-gatereference to an array or hash which in turn contains multiple values. 158*0Sstevel@tonic-gate 159*0Sstevel@tonic-gateScalars aren't necessarily one thing or another. There's no place 160*0Sstevel@tonic-gateto declare a scalar variable to be of type "string", type "number", 161*0Sstevel@tonic-gatetype "reference", or anything else. Because of the automatic 162*0Sstevel@tonic-gateconversion of scalars, operations that return scalars don't need 163*0Sstevel@tonic-gateto care (and in fact, cannot care) whether their caller is looking 164*0Sstevel@tonic-gatefor a string, a number, or a reference. Perl is a contextually 165*0Sstevel@tonic-gatepolymorphic language whose scalars can be strings, numbers, or 166*0Sstevel@tonic-gatereferences (which includes objects). Although strings and numbers 167*0Sstevel@tonic-gateare considered pretty much the same thing for nearly all purposes, 168*0Sstevel@tonic-gatereferences are strongly-typed, uncastable pointers with builtin 169*0Sstevel@tonic-gatereference-counting and destructor invocation. 170*0Sstevel@tonic-gate 171*0Sstevel@tonic-gateA scalar value is interpreted as TRUE in the Boolean sense if it is not 172*0Sstevel@tonic-gatethe null string or the number 0 (or its string equivalent, "0"). The 173*0Sstevel@tonic-gateBoolean context is just a special kind of scalar context where no 174*0Sstevel@tonic-gateconversion to a string or a number is ever performed. 175*0Sstevel@tonic-gate 176*0Sstevel@tonic-gateThere are actually two varieties of null strings (sometimes referred 177*0Sstevel@tonic-gateto as "empty" strings), a defined one and an undefined one. The 178*0Sstevel@tonic-gatedefined version is just a string of length zero, such as C<"">. 179*0Sstevel@tonic-gateThe undefined version is the value that indicates that there is 180*0Sstevel@tonic-gateno real value for something, such as when there was an error, or 181*0Sstevel@tonic-gateat end of file, or when you refer to an uninitialized variable or 182*0Sstevel@tonic-gateelement of an array or hash. Although in early versions of Perl, 183*0Sstevel@tonic-gatean undefined scalar could become defined when first used in a 184*0Sstevel@tonic-gateplace expecting a defined value, this no longer happens except for 185*0Sstevel@tonic-gaterare cases of autovivification as explained in L<perlref>. You can 186*0Sstevel@tonic-gateuse the defined() operator to determine whether a scalar value is 187*0Sstevel@tonic-gatedefined (this has no meaning on arrays or hashes), and the undef() 188*0Sstevel@tonic-gateoperator to produce an undefined value. 189*0Sstevel@tonic-gate 190*0Sstevel@tonic-gateTo find out whether a given string is a valid non-zero number, it's 191*0Sstevel@tonic-gatesometimes enough to test it against both numeric 0 and also lexical 192*0Sstevel@tonic-gate"0" (although this will cause noises if warnings are on). That's 193*0Sstevel@tonic-gatebecause strings that aren't numbers count as 0, just as they do in B<awk>: 194*0Sstevel@tonic-gate 195*0Sstevel@tonic-gate if ($str == 0 && $str ne "0") { 196*0Sstevel@tonic-gate warn "That doesn't look like a number"; 197*0Sstevel@tonic-gate } 198*0Sstevel@tonic-gate 199*0Sstevel@tonic-gateThat method may be best because otherwise you won't treat IEEE 200*0Sstevel@tonic-gatenotations like C<NaN> or C<Infinity> properly. At other times, you 201*0Sstevel@tonic-gatemight prefer to determine whether string data can be used numerically 202*0Sstevel@tonic-gateby calling the POSIX::strtod() function or by inspecting your string 203*0Sstevel@tonic-gatewith a regular expression (as documented in L<perlre>). 204*0Sstevel@tonic-gate 205*0Sstevel@tonic-gate warn "has nondigits" if /\D/; 206*0Sstevel@tonic-gate warn "not a natural number" unless /^\d+$/; # rejects -3 207*0Sstevel@tonic-gate warn "not an integer" unless /^-?\d+$/; # rejects +3 208*0Sstevel@tonic-gate warn "not an integer" unless /^[+-]?\d+$/; 209*0Sstevel@tonic-gate warn "not a decimal number" unless /^-?\d+\.?\d*$/; # rejects .2 210*0Sstevel@tonic-gate warn "not a decimal number" unless /^-?(?:\d+(?:\.\d*)?|\.\d+)$/; 211*0Sstevel@tonic-gate warn "not a C float" 212*0Sstevel@tonic-gate unless /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/; 213*0Sstevel@tonic-gate 214*0Sstevel@tonic-gateThe length of an array is a scalar value. You may find the length 215*0Sstevel@tonic-gateof array @days by evaluating C<$#days>, as in B<csh>. However, this 216*0Sstevel@tonic-gateisn't the length of the array; it's the subscript of the last element, 217*0Sstevel@tonic-gatewhich is a different value since there is ordinarily a 0th element. 218*0Sstevel@tonic-gateAssigning to C<$#days> actually changes the length of the array. 219*0Sstevel@tonic-gateShortening an array this way destroys intervening values. Lengthening 220*0Sstevel@tonic-gatean array that was previously shortened does not recover values 221*0Sstevel@tonic-gatethat were in those elements. (It used to do so in Perl 4, but we 222*0Sstevel@tonic-gatehad to break this to make sure destructors were called when expected.) 223*0Sstevel@tonic-gate 224*0Sstevel@tonic-gateYou can also gain some minuscule measure of efficiency by pre-extending 225*0Sstevel@tonic-gatean array that is going to get big. You can also extend an array 226*0Sstevel@tonic-gateby assigning to an element that is off the end of the array. You 227*0Sstevel@tonic-gatecan truncate an array down to nothing by assigning the null list 228*0Sstevel@tonic-gate() to it. The following are equivalent: 229*0Sstevel@tonic-gate 230*0Sstevel@tonic-gate @whatever = (); 231*0Sstevel@tonic-gate $#whatever = -1; 232*0Sstevel@tonic-gate 233*0Sstevel@tonic-gateIf you evaluate an array in scalar context, it returns the length 234*0Sstevel@tonic-gateof the array. (Note that this is not true of lists, which return 235*0Sstevel@tonic-gatethe last value, like the C comma operator, nor of built-in functions, 236*0Sstevel@tonic-gatewhich return whatever they feel like returning.) The following is 237*0Sstevel@tonic-gatealways true: 238*0Sstevel@tonic-gate 239*0Sstevel@tonic-gate scalar(@whatever) == $#whatever - $[ + 1; 240*0Sstevel@tonic-gate 241*0Sstevel@tonic-gateVersion 5 of Perl changed the semantics of C<$[>: files that don't set 242*0Sstevel@tonic-gatethe value of C<$[> no longer need to worry about whether another 243*0Sstevel@tonic-gatefile changed its value. (In other words, use of C<$[> is deprecated.) 244*0Sstevel@tonic-gateSo in general you can assume that 245*0Sstevel@tonic-gate 246*0Sstevel@tonic-gate scalar(@whatever) == $#whatever + 1; 247*0Sstevel@tonic-gate 248*0Sstevel@tonic-gateSome programmers choose to use an explicit conversion so as to 249*0Sstevel@tonic-gateleave nothing to doubt: 250*0Sstevel@tonic-gate 251*0Sstevel@tonic-gate $element_count = scalar(@whatever); 252*0Sstevel@tonic-gate 253*0Sstevel@tonic-gateIf you evaluate a hash in scalar context, it returns false if the 254*0Sstevel@tonic-gatehash is empty. If there are any key/value pairs, it returns true; 255*0Sstevel@tonic-gatemore precisely, the value returned is a string consisting of the 256*0Sstevel@tonic-gatenumber of used buckets and the number of allocated buckets, separated 257*0Sstevel@tonic-gateby a slash. This is pretty much useful only to find out whether 258*0Sstevel@tonic-gatePerl's internal hashing algorithm is performing poorly on your data 259*0Sstevel@tonic-gateset. For example, you stick 10,000 things in a hash, but evaluating 260*0Sstevel@tonic-gate%HASH in scalar context reveals C<"1/16">, which means only one out 261*0Sstevel@tonic-gateof sixteen buckets has been touched, and presumably contains all 262*0Sstevel@tonic-gate10,000 of your items. This isn't supposed to happen. 263*0Sstevel@tonic-gate 264*0Sstevel@tonic-gateYou can preallocate space for a hash by assigning to the keys() function. 265*0Sstevel@tonic-gateThis rounds up the allocated buckets to the next power of two: 266*0Sstevel@tonic-gate 267*0Sstevel@tonic-gate keys(%users) = 1000; # allocate 1024 buckets 268*0Sstevel@tonic-gate 269*0Sstevel@tonic-gate=head2 Scalar value constructors 270*0Sstevel@tonic-gate 271*0Sstevel@tonic-gateNumeric literals are specified in any of the following floating point or 272*0Sstevel@tonic-gateinteger formats: 273*0Sstevel@tonic-gate 274*0Sstevel@tonic-gate 12345 275*0Sstevel@tonic-gate 12345.67 276*0Sstevel@tonic-gate .23E-10 # a very small number 277*0Sstevel@tonic-gate 3.14_15_92 # a very important number 278*0Sstevel@tonic-gate 4_294_967_296 # underscore for legibility 279*0Sstevel@tonic-gate 0xff # hex 280*0Sstevel@tonic-gate 0xdead_beef # more hex 281*0Sstevel@tonic-gate 0377 # octal 282*0Sstevel@tonic-gate 0b011011 # binary 283*0Sstevel@tonic-gate 284*0Sstevel@tonic-gateYou are allowed to use underscores (underbars) in numeric literals 285*0Sstevel@tonic-gatebetween digits for legibility. You could, for example, group binary 286*0Sstevel@tonic-gatedigits by threes (as for a Unix-style mode argument such as 0b110_100_100) 287*0Sstevel@tonic-gateor by fours (to represent nibbles, as in 0b1010_0110) or in other groups. 288*0Sstevel@tonic-gate 289*0Sstevel@tonic-gateString literals are usually delimited by either single or double 290*0Sstevel@tonic-gatequotes. They work much like quotes in the standard Unix shells: 291*0Sstevel@tonic-gatedouble-quoted string literals are subject to backslash and variable 292*0Sstevel@tonic-gatesubstitution; single-quoted strings are not (except for C<\'> and 293*0Sstevel@tonic-gateC<\\>). The usual C-style backslash rules apply for making 294*0Sstevel@tonic-gatecharacters such as newline, tab, etc., as well as some more exotic 295*0Sstevel@tonic-gateforms. See L<perlop/"Quote and Quote-like Operators"> for a list. 296*0Sstevel@tonic-gate 297*0Sstevel@tonic-gateHexadecimal, octal, or binary, representations in string literals 298*0Sstevel@tonic-gate(e.g. '0xff') are not automatically converted to their integer 299*0Sstevel@tonic-gaterepresentation. The hex() and oct() functions make these conversions 300*0Sstevel@tonic-gatefor you. See L<perlfunc/hex> and L<perlfunc/oct> for more details. 301*0Sstevel@tonic-gate 302*0Sstevel@tonic-gateYou can also embed newlines directly in your strings, i.e., they can end 303*0Sstevel@tonic-gateon a different line than they begin. This is nice, but if you forget 304*0Sstevel@tonic-gateyour trailing quote, the error will not be reported until Perl finds 305*0Sstevel@tonic-gateanother line containing the quote character, which may be much further 306*0Sstevel@tonic-gateon in the script. Variable substitution inside strings is limited to 307*0Sstevel@tonic-gatescalar variables, arrays, and array or hash slices. (In other words, 308*0Sstevel@tonic-gatenames beginning with $ or @, followed by an optional bracketed 309*0Sstevel@tonic-gateexpression as a subscript.) The following code segment prints out "The 310*0Sstevel@tonic-gateprice is $Z<>100." 311*0Sstevel@tonic-gate 312*0Sstevel@tonic-gate $Price = '$100'; # not interpolated 313*0Sstevel@tonic-gate print "The price is $Price.\n"; # interpolated 314*0Sstevel@tonic-gate 315*0Sstevel@tonic-gateThere is no double interpolation in Perl, so the C<$100> is left as is. 316*0Sstevel@tonic-gate 317*0Sstevel@tonic-gateAs in some shells, you can enclose the variable name in braces to 318*0Sstevel@tonic-gatedisambiguate it from following alphanumerics (and underscores). 319*0Sstevel@tonic-gateYou must also do 320*0Sstevel@tonic-gatethis when interpolating a variable into a string to separate the 321*0Sstevel@tonic-gatevariable name from a following double-colon or an apostrophe, since 322*0Sstevel@tonic-gatethese would be otherwise treated as a package separator: 323*0Sstevel@tonic-gate 324*0Sstevel@tonic-gate $who = "Larry"; 325*0Sstevel@tonic-gate print PASSWD "${who}::0:0:Superuser:/:/bin/perl\n"; 326*0Sstevel@tonic-gate print "We use ${who}speak when ${who}'s here.\n"; 327*0Sstevel@tonic-gate 328*0Sstevel@tonic-gateWithout the braces, Perl would have looked for a $whospeak, a 329*0Sstevel@tonic-gateC<$who::0>, and a C<$who's> variable. The last two would be the 330*0Sstevel@tonic-gate$0 and the $s variables in the (presumably) non-existent package 331*0Sstevel@tonic-gateC<who>. 332*0Sstevel@tonic-gate 333*0Sstevel@tonic-gateIn fact, an identifier within such curlies is forced to be a string, 334*0Sstevel@tonic-gateas is any simple identifier within a hash subscript. Neither need 335*0Sstevel@tonic-gatequoting. Our earlier example, C<$days{'Feb'}> can be written as 336*0Sstevel@tonic-gateC<$days{Feb}> and the quotes will be assumed automatically. But 337*0Sstevel@tonic-gateanything more complicated in the subscript will be interpreted as 338*0Sstevel@tonic-gatean expression. 339*0Sstevel@tonic-gate 340*0Sstevel@tonic-gate=head3 Version Strings 341*0Sstevel@tonic-gate 342*0Sstevel@tonic-gateB<Note:> Version Strings (v-strings) have been deprecated. They will 343*0Sstevel@tonic-gatenot be available after Perl 5.8. The marginal benefits of v-strings 344*0Sstevel@tonic-gatewere greatly outweighed by the potential for Surprise and Confusion. 345*0Sstevel@tonic-gate 346*0Sstevel@tonic-gateA literal of the form C<v1.20.300.4000> is parsed as a string composed 347*0Sstevel@tonic-gateof characters with the specified ordinals. This form, known as 348*0Sstevel@tonic-gatev-strings, provides an alternative, more readable way to construct 349*0Sstevel@tonic-gatestrings, rather than use the somewhat less readable interpolation form 350*0Sstevel@tonic-gateC<"\x{1}\x{14}\x{12c}\x{fa0}">. This is useful for representing 351*0Sstevel@tonic-gateUnicode strings, and for comparing version "numbers" using the string 352*0Sstevel@tonic-gatecomparison operators, C<cmp>, C<gt>, C<lt> etc. If there are two or 353*0Sstevel@tonic-gatemore dots in the literal, the leading C<v> may be omitted. 354*0Sstevel@tonic-gate 355*0Sstevel@tonic-gate print v9786; # prints UTF-8 encoded SMILEY, "\x{263a}" 356*0Sstevel@tonic-gate print v102.111.111; # prints "foo" 357*0Sstevel@tonic-gate print 102.111.111; # same 358*0Sstevel@tonic-gate 359*0Sstevel@tonic-gateSuch literals are accepted by both C<require> and C<use> for 360*0Sstevel@tonic-gatedoing a version check. The C<$^V> special variable also contains the 361*0Sstevel@tonic-gaterunning Perl interpreter's version in this form. See L<perlvar/$^V>. 362*0Sstevel@tonic-gateNote that using the v-strings for IPv4 addresses is not portable unless 363*0Sstevel@tonic-gateyou also use the inet_aton()/inet_ntoa() routines of the Socket package. 364*0Sstevel@tonic-gate 365*0Sstevel@tonic-gateNote that since Perl 5.8.1 the single-number v-strings (like C<v65>) 366*0Sstevel@tonic-gateare not v-strings before the C<< => >> operator (which is usually used 367*0Sstevel@tonic-gateto separate a hash key from a hash value), instead they are interpreted 368*0Sstevel@tonic-gateas literal strings ('v65'). They were v-strings from Perl 5.6.0 to 369*0Sstevel@tonic-gatePerl 5.8.0, but that caused more confusion and breakage than good. 370*0Sstevel@tonic-gateMulti-number v-strings like C<v65.66> and C<65.66.67> continue to 371*0Sstevel@tonic-gatebe v-strings always. 372*0Sstevel@tonic-gate 373*0Sstevel@tonic-gate=head3 Special Literals 374*0Sstevel@tonic-gate 375*0Sstevel@tonic-gateThe special literals __FILE__, __LINE__, and __PACKAGE__ 376*0Sstevel@tonic-gaterepresent the current filename, line number, and package name at that 377*0Sstevel@tonic-gatepoint in your program. They may be used only as separate tokens; they 378*0Sstevel@tonic-gatewill not be interpolated into strings. If there is no current package 379*0Sstevel@tonic-gate(due to an empty C<package;> directive), __PACKAGE__ is the undefined 380*0Sstevel@tonic-gatevalue. 381*0Sstevel@tonic-gate 382*0Sstevel@tonic-gateThe two control characters ^D and ^Z, and the tokens __END__ and __DATA__ 383*0Sstevel@tonic-gatemay be used to indicate the logical end of the script before the actual 384*0Sstevel@tonic-gateend of file. Any following text is ignored. 385*0Sstevel@tonic-gate 386*0Sstevel@tonic-gateText after __DATA__ but may be read via the filehandle C<PACKNAME::DATA>, 387*0Sstevel@tonic-gatewhere C<PACKNAME> is the package that was current when the __DATA__ 388*0Sstevel@tonic-gatetoken was encountered. The filehandle is left open pointing to the 389*0Sstevel@tonic-gatecontents after __DATA__. It is the program's responsibility to 390*0Sstevel@tonic-gateC<close DATA> when it is done reading from it. For compatibility with 391*0Sstevel@tonic-gateolder scripts written before __DATA__ was introduced, __END__ behaves 392*0Sstevel@tonic-gatelike __DATA__ in the toplevel script (but not in files loaded with 393*0Sstevel@tonic-gateC<require> or C<do>) and leaves the remaining contents of the 394*0Sstevel@tonic-gatefile accessible via C<main::DATA>. 395*0Sstevel@tonic-gate 396*0Sstevel@tonic-gateSee L<SelfLoader> for more description of __DATA__, and 397*0Sstevel@tonic-gatean example of its use. Note that you cannot read from the DATA 398*0Sstevel@tonic-gatefilehandle in a BEGIN block: the BEGIN block is executed as soon 399*0Sstevel@tonic-gateas it is seen (during compilation), at which point the corresponding 400*0Sstevel@tonic-gate__DATA__ (or __END__) token has not yet been seen. 401*0Sstevel@tonic-gate 402*0Sstevel@tonic-gate=head3 Barewords 403*0Sstevel@tonic-gate 404*0Sstevel@tonic-gateA word that has no other interpretation in the grammar will 405*0Sstevel@tonic-gatebe treated as if it were a quoted string. These are known as 406*0Sstevel@tonic-gate"barewords". As with filehandles and labels, a bareword that consists 407*0Sstevel@tonic-gateentirely of lowercase letters risks conflict with future reserved 408*0Sstevel@tonic-gatewords, and if you use the C<use warnings> pragma or the B<-w> switch, 409*0Sstevel@tonic-gatePerl will warn you about any 410*0Sstevel@tonic-gatesuch words. Some people may wish to outlaw barewords entirely. If you 411*0Sstevel@tonic-gatesay 412*0Sstevel@tonic-gate 413*0Sstevel@tonic-gate use strict 'subs'; 414*0Sstevel@tonic-gate 415*0Sstevel@tonic-gatethen any bareword that would NOT be interpreted as a subroutine call 416*0Sstevel@tonic-gateproduces a compile-time error instead. The restriction lasts to the 417*0Sstevel@tonic-gateend of the enclosing block. An inner block may countermand this 418*0Sstevel@tonic-gateby saying C<no strict 'subs'>. 419*0Sstevel@tonic-gate 420*0Sstevel@tonic-gate=head3 Array Joining Delimiter 421*0Sstevel@tonic-gate 422*0Sstevel@tonic-gateArrays and slices are interpolated into double-quoted strings 423*0Sstevel@tonic-gateby joining the elements with the delimiter specified in the C<$"> 424*0Sstevel@tonic-gatevariable (C<$LIST_SEPARATOR> if "use English;" is specified), 425*0Sstevel@tonic-gatespace by default. The following are equivalent: 426*0Sstevel@tonic-gate 427*0Sstevel@tonic-gate $temp = join($", @ARGV); 428*0Sstevel@tonic-gate system "echo $temp"; 429*0Sstevel@tonic-gate 430*0Sstevel@tonic-gate system "echo @ARGV"; 431*0Sstevel@tonic-gate 432*0Sstevel@tonic-gateWithin search patterns (which also undergo double-quotish substitution) 433*0Sstevel@tonic-gatethere is an unfortunate ambiguity: Is C</$foo[bar]/> to be interpreted as 434*0Sstevel@tonic-gateC</${foo}[bar]/> (where C<[bar]> is a character class for the regular 435*0Sstevel@tonic-gateexpression) or as C</${foo[bar]}/> (where C<[bar]> is the subscript to array 436*0Sstevel@tonic-gate@foo)? If @foo doesn't otherwise exist, then it's obviously a 437*0Sstevel@tonic-gatecharacter class. If @foo exists, Perl takes a good guess about C<[bar]>, 438*0Sstevel@tonic-gateand is almost always right. If it does guess wrong, or if you're just 439*0Sstevel@tonic-gateplain paranoid, you can force the correct interpretation with curly 440*0Sstevel@tonic-gatebraces as above. 441*0Sstevel@tonic-gate 442*0Sstevel@tonic-gateIf you're looking for the information on how to use here-documents, 443*0Sstevel@tonic-gatewhich used to be here, that's been moved to 444*0Sstevel@tonic-gateL<perlop/Quote and Quote-like Operators>. 445*0Sstevel@tonic-gate 446*0Sstevel@tonic-gate=head2 List value constructors 447*0Sstevel@tonic-gate 448*0Sstevel@tonic-gateList values are denoted by separating individual values by commas 449*0Sstevel@tonic-gate(and enclosing the list in parentheses where precedence requires it): 450*0Sstevel@tonic-gate 451*0Sstevel@tonic-gate (LIST) 452*0Sstevel@tonic-gate 453*0Sstevel@tonic-gateIn a context not requiring a list value, the value of what appears 454*0Sstevel@tonic-gateto be a list literal is simply the value of the final element, as 455*0Sstevel@tonic-gatewith the C comma operator. For example, 456*0Sstevel@tonic-gate 457*0Sstevel@tonic-gate @foo = ('cc', '-E', $bar); 458*0Sstevel@tonic-gate 459*0Sstevel@tonic-gateassigns the entire list value to array @foo, but 460*0Sstevel@tonic-gate 461*0Sstevel@tonic-gate $foo = ('cc', '-E', $bar); 462*0Sstevel@tonic-gate 463*0Sstevel@tonic-gateassigns the value of variable $bar to the scalar variable $foo. 464*0Sstevel@tonic-gateNote that the value of an actual array in scalar context is the 465*0Sstevel@tonic-gatelength of the array; the following assigns the value 3 to $foo: 466*0Sstevel@tonic-gate 467*0Sstevel@tonic-gate @foo = ('cc', '-E', $bar); 468*0Sstevel@tonic-gate $foo = @foo; # $foo gets 3 469*0Sstevel@tonic-gate 470*0Sstevel@tonic-gateYou may have an optional comma before the closing parenthesis of a 471*0Sstevel@tonic-gatelist literal, so that you can say: 472*0Sstevel@tonic-gate 473*0Sstevel@tonic-gate @foo = ( 474*0Sstevel@tonic-gate 1, 475*0Sstevel@tonic-gate 2, 476*0Sstevel@tonic-gate 3, 477*0Sstevel@tonic-gate ); 478*0Sstevel@tonic-gate 479*0Sstevel@tonic-gateTo use a here-document to assign an array, one line per element, 480*0Sstevel@tonic-gateyou might use an approach like this: 481*0Sstevel@tonic-gate 482*0Sstevel@tonic-gate @sauces = <<End_Lines =~ m/(\S.*\S)/g; 483*0Sstevel@tonic-gate normal tomato 484*0Sstevel@tonic-gate spicy tomato 485*0Sstevel@tonic-gate green chile 486*0Sstevel@tonic-gate pesto 487*0Sstevel@tonic-gate white wine 488*0Sstevel@tonic-gate End_Lines 489*0Sstevel@tonic-gate 490*0Sstevel@tonic-gateLISTs do automatic interpolation of sublists. That is, when a LIST is 491*0Sstevel@tonic-gateevaluated, each element of the list is evaluated in list context, and 492*0Sstevel@tonic-gatethe resulting list value is interpolated into LIST just as if each 493*0Sstevel@tonic-gateindividual element were a member of LIST. Thus arrays and hashes lose their 494*0Sstevel@tonic-gateidentity in a LIST--the list 495*0Sstevel@tonic-gate 496*0Sstevel@tonic-gate (@foo,@bar,&SomeSub,%glarch) 497*0Sstevel@tonic-gate 498*0Sstevel@tonic-gatecontains all the elements of @foo followed by all the elements of @bar, 499*0Sstevel@tonic-gatefollowed by all the elements returned by the subroutine named SomeSub 500*0Sstevel@tonic-gatecalled in list context, followed by the key/value pairs of %glarch. 501*0Sstevel@tonic-gateTo make a list reference that does I<NOT> interpolate, see L<perlref>. 502*0Sstevel@tonic-gate 503*0Sstevel@tonic-gateThe null list is represented by (). Interpolating it in a list 504*0Sstevel@tonic-gatehas no effect. Thus ((),(),()) is equivalent to (). Similarly, 505*0Sstevel@tonic-gateinterpolating an array with no elements is the same as if no 506*0Sstevel@tonic-gatearray had been interpolated at that point. 507*0Sstevel@tonic-gate 508*0Sstevel@tonic-gateThis interpolation combines with the facts that the opening 509*0Sstevel@tonic-gateand closing parentheses are optional (except when necessary for 510*0Sstevel@tonic-gateprecedence) and lists may end with an optional comma to mean that 511*0Sstevel@tonic-gatemultiple commas within lists are legal syntax. The list C<1,,3> is a 512*0Sstevel@tonic-gateconcatenation of two lists, C<1,> and C<3>, the first of which ends 513*0Sstevel@tonic-gatewith that optional comma. C<1,,3> is C<(1,),(3)> is C<1,3> (And 514*0Sstevel@tonic-gatesimilarly for C<1,,,3> is C<(1,),(,),3> is C<1,3> and so on.) Not that 515*0Sstevel@tonic-gatewe'd advise you to use this obfuscation. 516*0Sstevel@tonic-gate 517*0Sstevel@tonic-gateA list value may also be subscripted like a normal array. You must 518*0Sstevel@tonic-gateput the list in parentheses to avoid ambiguity. For example: 519*0Sstevel@tonic-gate 520*0Sstevel@tonic-gate # Stat returns list value. 521*0Sstevel@tonic-gate $time = (stat($file))[8]; 522*0Sstevel@tonic-gate 523*0Sstevel@tonic-gate # SYNTAX ERROR HERE. 524*0Sstevel@tonic-gate $time = stat($file)[8]; # OOPS, FORGOT PARENTHESES 525*0Sstevel@tonic-gate 526*0Sstevel@tonic-gate # Find a hex digit. 527*0Sstevel@tonic-gate $hexdigit = ('a','b','c','d','e','f')[$digit-10]; 528*0Sstevel@tonic-gate 529*0Sstevel@tonic-gate # A "reverse comma operator". 530*0Sstevel@tonic-gate return (pop(@foo),pop(@foo))[0]; 531*0Sstevel@tonic-gate 532*0Sstevel@tonic-gateLists may be assigned to only when each element of the list 533*0Sstevel@tonic-gateis itself legal to assign to: 534*0Sstevel@tonic-gate 535*0Sstevel@tonic-gate ($a, $b, $c) = (1, 2, 3); 536*0Sstevel@tonic-gate 537*0Sstevel@tonic-gate ($map{'red'}, $map{'blue'}, $map{'green'}) = (0x00f, 0x0f0, 0xf00); 538*0Sstevel@tonic-gate 539*0Sstevel@tonic-gateAn exception to this is that you may assign to C<undef> in a list. 540*0Sstevel@tonic-gateThis is useful for throwing away some of the return values of a 541*0Sstevel@tonic-gatefunction: 542*0Sstevel@tonic-gate 543*0Sstevel@tonic-gate ($dev, $ino, undef, undef, $uid, $gid) = stat($file); 544*0Sstevel@tonic-gate 545*0Sstevel@tonic-gateList assignment in scalar context returns the number of elements 546*0Sstevel@tonic-gateproduced by the expression on the right side of the assignment: 547*0Sstevel@tonic-gate 548*0Sstevel@tonic-gate $x = (($foo,$bar) = (3,2,1)); # set $x to 3, not 2 549*0Sstevel@tonic-gate $x = (($foo,$bar) = f()); # set $x to f()'s return count 550*0Sstevel@tonic-gate 551*0Sstevel@tonic-gateThis is handy when you want to do a list assignment in a Boolean 552*0Sstevel@tonic-gatecontext, because most list functions return a null list when finished, 553*0Sstevel@tonic-gatewhich when assigned produces a 0, which is interpreted as FALSE. 554*0Sstevel@tonic-gate 555*0Sstevel@tonic-gateIt's also the source of a useful idiom for executing a function or 556*0Sstevel@tonic-gateperforming an operation in list context and then counting the number of 557*0Sstevel@tonic-gatereturn values, by assigning to an empty list and then using that 558*0Sstevel@tonic-gateassignment in scalar context. For example, this code: 559*0Sstevel@tonic-gate 560*0Sstevel@tonic-gate $count = () = $string =~ /\d+/g; 561*0Sstevel@tonic-gate 562*0Sstevel@tonic-gatewill place into $count the number of digit groups found in $string. 563*0Sstevel@tonic-gateThis happens because the pattern match is in list context (since it 564*0Sstevel@tonic-gateis being assigned to the empty list), and will therefore return a list 565*0Sstevel@tonic-gateof all matching parts of the string. The list assignment in scalar 566*0Sstevel@tonic-gatecontext will translate that into the number of elements (here, the 567*0Sstevel@tonic-gatenumber of times the pattern matched) and assign that to $count. Note 568*0Sstevel@tonic-gatethat simply using 569*0Sstevel@tonic-gate 570*0Sstevel@tonic-gate $count = $string =~ /\d+/g; 571*0Sstevel@tonic-gate 572*0Sstevel@tonic-gatewould not have worked, since a pattern match in scalar context will 573*0Sstevel@tonic-gateonly return true or false, rather than a count of matches. 574*0Sstevel@tonic-gate 575*0Sstevel@tonic-gateThe final element of a list assignment may be an array or a hash: 576*0Sstevel@tonic-gate 577*0Sstevel@tonic-gate ($a, $b, @rest) = split; 578*0Sstevel@tonic-gate my($a, $b, %rest) = @_; 579*0Sstevel@tonic-gate 580*0Sstevel@tonic-gateYou can actually put an array or hash anywhere in the list, but the first one 581*0Sstevel@tonic-gatein the list will soak up all the values, and anything after it will become 582*0Sstevel@tonic-gateundefined. This may be useful in a my() or local(). 583*0Sstevel@tonic-gate 584*0Sstevel@tonic-gateA hash can be initialized using a literal list holding pairs of 585*0Sstevel@tonic-gateitems to be interpreted as a key and a value: 586*0Sstevel@tonic-gate 587*0Sstevel@tonic-gate # same as map assignment above 588*0Sstevel@tonic-gate %map = ('red',0x00f,'blue',0x0f0,'green',0xf00); 589*0Sstevel@tonic-gate 590*0Sstevel@tonic-gateWhile literal lists and named arrays are often interchangeable, that's 591*0Sstevel@tonic-gatenot the case for hashes. Just because you can subscript a list value like 592*0Sstevel@tonic-gatea normal array does not mean that you can subscript a list value as a 593*0Sstevel@tonic-gatehash. Likewise, hashes included as parts of other lists (including 594*0Sstevel@tonic-gateparameters lists and return lists from functions) always flatten out into 595*0Sstevel@tonic-gatekey/value pairs. That's why it's good to use references sometimes. 596*0Sstevel@tonic-gate 597*0Sstevel@tonic-gateIt is often more readable to use the C<< => >> operator between key/value 598*0Sstevel@tonic-gatepairs. The C<< => >> operator is mostly just a more visually distinctive 599*0Sstevel@tonic-gatesynonym for a comma, but it also arranges for its left-hand operand to be 600*0Sstevel@tonic-gateinterpreted as a string -- if it's a bareword that would be a legal simple 601*0Sstevel@tonic-gateidentifier (C<< => >> doesn't quote compound identifiers, that contain 602*0Sstevel@tonic-gatedouble colons). This makes it nice for initializing hashes: 603*0Sstevel@tonic-gate 604*0Sstevel@tonic-gate %map = ( 605*0Sstevel@tonic-gate red => 0x00f, 606*0Sstevel@tonic-gate blue => 0x0f0, 607*0Sstevel@tonic-gate green => 0xf00, 608*0Sstevel@tonic-gate ); 609*0Sstevel@tonic-gate 610*0Sstevel@tonic-gateor for initializing hash references to be used as records: 611*0Sstevel@tonic-gate 612*0Sstevel@tonic-gate $rec = { 613*0Sstevel@tonic-gate witch => 'Mable the Merciless', 614*0Sstevel@tonic-gate cat => 'Fluffy the Ferocious', 615*0Sstevel@tonic-gate date => '10/31/1776', 616*0Sstevel@tonic-gate }; 617*0Sstevel@tonic-gate 618*0Sstevel@tonic-gateor for using call-by-named-parameter to complicated functions: 619*0Sstevel@tonic-gate 620*0Sstevel@tonic-gate $field = $query->radio_group( 621*0Sstevel@tonic-gate name => 'group_name', 622*0Sstevel@tonic-gate values => ['eenie','meenie','minie'], 623*0Sstevel@tonic-gate default => 'meenie', 624*0Sstevel@tonic-gate linebreak => 'true', 625*0Sstevel@tonic-gate labels => \%labels 626*0Sstevel@tonic-gate ); 627*0Sstevel@tonic-gate 628*0Sstevel@tonic-gateNote that just because a hash is initialized in that order doesn't 629*0Sstevel@tonic-gatemean that it comes out in that order. See L<perlfunc/sort> for examples 630*0Sstevel@tonic-gateof how to arrange for an output ordering. 631*0Sstevel@tonic-gate 632*0Sstevel@tonic-gate=head2 Subscripts 633*0Sstevel@tonic-gate 634*0Sstevel@tonic-gateAn array is subscripted by specifying a dollary sign (C<$>), then the 635*0Sstevel@tonic-gatename of the array (without the leading C<@>), then the subscript inside 636*0Sstevel@tonic-gatesquare brackets. For example: 637*0Sstevel@tonic-gate 638*0Sstevel@tonic-gate @myarray = (5, 50, 500, 5000); 639*0Sstevel@tonic-gate print "Element Number 2 is", $myarray[2], "\n"; 640*0Sstevel@tonic-gate 641*0Sstevel@tonic-gateThe array indices start with 0. A negative subscript retrieves its 642*0Sstevel@tonic-gatevalue from the end. In our example, C<$myarray[-1]> would have been 643*0Sstevel@tonic-gate5000, and C<$myarray[-2]> would have been 500. 644*0Sstevel@tonic-gate 645*0Sstevel@tonic-gateHash subscripts are similar, only instead of square brackets curly brackets 646*0Sstevel@tonic-gateare used. For example: 647*0Sstevel@tonic-gate 648*0Sstevel@tonic-gate %scientists = 649*0Sstevel@tonic-gate ( 650*0Sstevel@tonic-gate "Newton" => "Isaac", 651*0Sstevel@tonic-gate "Einstein" => "Albert", 652*0Sstevel@tonic-gate "Darwin" => "Charles", 653*0Sstevel@tonic-gate "Feynman" => "Richard", 654*0Sstevel@tonic-gate ); 655*0Sstevel@tonic-gate 656*0Sstevel@tonic-gate print "Darwin's First Name is ", $scientists{"Darwin"}, "\n"; 657*0Sstevel@tonic-gate 658*0Sstevel@tonic-gate=head2 Slices 659*0Sstevel@tonic-gate 660*0Sstevel@tonic-gateA common way to access an array or a hash is one scalar element at a 661*0Sstevel@tonic-gatetime. You can also subscript a list to get a single element from it. 662*0Sstevel@tonic-gate 663*0Sstevel@tonic-gate $whoami = $ENV{"USER"}; # one element from the hash 664*0Sstevel@tonic-gate $parent = $ISA[0]; # one element from the array 665*0Sstevel@tonic-gate $dir = (getpwnam("daemon"))[7]; # likewise, but with list 666*0Sstevel@tonic-gate 667*0Sstevel@tonic-gateA slice accesses several elements of a list, an array, or a hash 668*0Sstevel@tonic-gatesimultaneously using a list of subscripts. It's more convenient 669*0Sstevel@tonic-gatethan writing out the individual elements as a list of separate 670*0Sstevel@tonic-gatescalar values. 671*0Sstevel@tonic-gate 672*0Sstevel@tonic-gate ($him, $her) = @folks[0,-1]; # array slice 673*0Sstevel@tonic-gate @them = @folks[0 .. 3]; # array slice 674*0Sstevel@tonic-gate ($who, $home) = @ENV{"USER", "HOME"}; # hash slice 675*0Sstevel@tonic-gate ($uid, $dir) = (getpwnam("daemon"))[2,7]; # list slice 676*0Sstevel@tonic-gate 677*0Sstevel@tonic-gateSince you can assign to a list of variables, you can also assign to 678*0Sstevel@tonic-gatean array or hash slice. 679*0Sstevel@tonic-gate 680*0Sstevel@tonic-gate @days[3..5] = qw/Wed Thu Fri/; 681*0Sstevel@tonic-gate @colors{'red','blue','green'} 682*0Sstevel@tonic-gate = (0xff0000, 0x0000ff, 0x00ff00); 683*0Sstevel@tonic-gate @folks[0, -1] = @folks[-1, 0]; 684*0Sstevel@tonic-gate 685*0Sstevel@tonic-gateThe previous assignments are exactly equivalent to 686*0Sstevel@tonic-gate 687*0Sstevel@tonic-gate ($days[3], $days[4], $days[5]) = qw/Wed Thu Fri/; 688*0Sstevel@tonic-gate ($colors{'red'}, $colors{'blue'}, $colors{'green'}) 689*0Sstevel@tonic-gate = (0xff0000, 0x0000ff, 0x00ff00); 690*0Sstevel@tonic-gate ($folks[0], $folks[-1]) = ($folks[-1], $folks[0]); 691*0Sstevel@tonic-gate 692*0Sstevel@tonic-gateSince changing a slice changes the original array or hash that it's 693*0Sstevel@tonic-gateslicing, a C<foreach> construct will alter some--or even all--of the 694*0Sstevel@tonic-gatevalues of the array or hash. 695*0Sstevel@tonic-gate 696*0Sstevel@tonic-gate foreach (@array[ 4 .. 10 ]) { s/peter/paul/ } 697*0Sstevel@tonic-gate 698*0Sstevel@tonic-gate foreach (@hash{qw[key1 key2]}) { 699*0Sstevel@tonic-gate s/^\s+//; # trim leading whitespace 700*0Sstevel@tonic-gate s/\s+$//; # trim trailing whitespace 701*0Sstevel@tonic-gate s/(\w+)/\u\L$1/g; # "titlecase" words 702*0Sstevel@tonic-gate } 703*0Sstevel@tonic-gate 704*0Sstevel@tonic-gateA slice of an empty list is still an empty list. Thus: 705*0Sstevel@tonic-gate 706*0Sstevel@tonic-gate @a = ()[1,0]; # @a has no elements 707*0Sstevel@tonic-gate @b = (@a)[0,1]; # @b has no elements 708*0Sstevel@tonic-gate @c = (0,1)[2,3]; # @c has no elements 709*0Sstevel@tonic-gate 710*0Sstevel@tonic-gateBut: 711*0Sstevel@tonic-gate 712*0Sstevel@tonic-gate @a = (1)[1,0]; # @a has two elements 713*0Sstevel@tonic-gate @b = (1,undef)[1,0,2]; # @b has three elements 714*0Sstevel@tonic-gate 715*0Sstevel@tonic-gateThis makes it easy to write loops that terminate when a null list 716*0Sstevel@tonic-gateis returned: 717*0Sstevel@tonic-gate 718*0Sstevel@tonic-gate while ( ($home, $user) = (getpwent)[7,0]) { 719*0Sstevel@tonic-gate printf "%-8s %s\n", $user, $home; 720*0Sstevel@tonic-gate } 721*0Sstevel@tonic-gate 722*0Sstevel@tonic-gateAs noted earlier in this document, the scalar sense of list assignment 723*0Sstevel@tonic-gateis the number of elements on the right-hand side of the assignment. 724*0Sstevel@tonic-gateThe null list contains no elements, so when the password file is 725*0Sstevel@tonic-gateexhausted, the result is 0, not 2. 726*0Sstevel@tonic-gate 727*0Sstevel@tonic-gateIf you're confused about why you use an '@' there on a hash slice 728*0Sstevel@tonic-gateinstead of a '%', think of it like this. The type of bracket (square 729*0Sstevel@tonic-gateor curly) governs whether it's an array or a hash being looked at. 730*0Sstevel@tonic-gateOn the other hand, the leading symbol ('$' or '@') on the array or 731*0Sstevel@tonic-gatehash indicates whether you are getting back a singular value (a 732*0Sstevel@tonic-gatescalar) or a plural one (a list). 733*0Sstevel@tonic-gate 734*0Sstevel@tonic-gate=head2 Typeglobs and Filehandles 735*0Sstevel@tonic-gate 736*0Sstevel@tonic-gatePerl uses an internal type called a I<typeglob> to hold an entire 737*0Sstevel@tonic-gatesymbol table entry. The type prefix of a typeglob is a C<*>, because 738*0Sstevel@tonic-gateit represents all types. This used to be the preferred way to 739*0Sstevel@tonic-gatepass arrays and hashes by reference into a function, but now that 740*0Sstevel@tonic-gatewe have real references, this is seldom needed. 741*0Sstevel@tonic-gate 742*0Sstevel@tonic-gateThe main use of typeglobs in modern Perl is create symbol table aliases. 743*0Sstevel@tonic-gateThis assignment: 744*0Sstevel@tonic-gate 745*0Sstevel@tonic-gate *this = *that; 746*0Sstevel@tonic-gate 747*0Sstevel@tonic-gatemakes $this an alias for $that, @this an alias for @that, %this an alias 748*0Sstevel@tonic-gatefor %that, &this an alias for &that, etc. Much safer is to use a reference. 749*0Sstevel@tonic-gateThis: 750*0Sstevel@tonic-gate 751*0Sstevel@tonic-gate local *Here::blue = \$There::green; 752*0Sstevel@tonic-gate 753*0Sstevel@tonic-gatetemporarily makes $Here::blue an alias for $There::green, but doesn't 754*0Sstevel@tonic-gatemake @Here::blue an alias for @There::green, or %Here::blue an alias for 755*0Sstevel@tonic-gate%There::green, etc. See L<perlmod/"Symbol Tables"> for more examples 756*0Sstevel@tonic-gateof this. Strange though this may seem, this is the basis for the whole 757*0Sstevel@tonic-gatemodule import/export system. 758*0Sstevel@tonic-gate 759*0Sstevel@tonic-gateAnother use for typeglobs is to pass filehandles into a function or 760*0Sstevel@tonic-gateto create new filehandles. If you need to use a typeglob to save away 761*0Sstevel@tonic-gatea filehandle, do it this way: 762*0Sstevel@tonic-gate 763*0Sstevel@tonic-gate $fh = *STDOUT; 764*0Sstevel@tonic-gate 765*0Sstevel@tonic-gateor perhaps as a real reference, like this: 766*0Sstevel@tonic-gate 767*0Sstevel@tonic-gate $fh = \*STDOUT; 768*0Sstevel@tonic-gate 769*0Sstevel@tonic-gateSee L<perlsub> for examples of using these as indirect filehandles 770*0Sstevel@tonic-gatein functions. 771*0Sstevel@tonic-gate 772*0Sstevel@tonic-gateTypeglobs are also a way to create a local filehandle using the local() 773*0Sstevel@tonic-gateoperator. These last until their block is exited, but may be passed back. 774*0Sstevel@tonic-gateFor example: 775*0Sstevel@tonic-gate 776*0Sstevel@tonic-gate sub newopen { 777*0Sstevel@tonic-gate my $path = shift; 778*0Sstevel@tonic-gate local *FH; # not my! 779*0Sstevel@tonic-gate open (FH, $path) or return undef; 780*0Sstevel@tonic-gate return *FH; 781*0Sstevel@tonic-gate } 782*0Sstevel@tonic-gate $fh = newopen('/etc/passwd'); 783*0Sstevel@tonic-gate 784*0Sstevel@tonic-gateNow that we have the C<*foo{THING}> notation, typeglobs aren't used as much 785*0Sstevel@tonic-gatefor filehandle manipulations, although they're still needed to pass brand 786*0Sstevel@tonic-gatenew file and directory handles into or out of functions. That's because 787*0Sstevel@tonic-gateC<*HANDLE{IO}> only works if HANDLE has already been used as a handle. 788*0Sstevel@tonic-gateIn other words, C<*FH> must be used to create new symbol table entries; 789*0Sstevel@tonic-gateC<*foo{THING}> cannot. When in doubt, use C<*FH>. 790*0Sstevel@tonic-gate 791*0Sstevel@tonic-gateAll functions that are capable of creating filehandles (open(), 792*0Sstevel@tonic-gateopendir(), pipe(), socketpair(), sysopen(), socket(), and accept()) 793*0Sstevel@tonic-gateautomatically create an anonymous filehandle if the handle passed to 794*0Sstevel@tonic-gatethem is an uninitialized scalar variable. This allows the constructs 795*0Sstevel@tonic-gatesuch as C<open(my $fh, ...)> and C<open(local $fh,...)> to be used to 796*0Sstevel@tonic-gatecreate filehandles that will conveniently be closed automatically when 797*0Sstevel@tonic-gatethe scope ends, provided there are no other references to them. This 798*0Sstevel@tonic-gatelargely eliminates the need for typeglobs when opening filehandles 799*0Sstevel@tonic-gatethat must be passed around, as in the following example: 800*0Sstevel@tonic-gate 801*0Sstevel@tonic-gate sub myopen { 802*0Sstevel@tonic-gate open my $fh, "@_" 803*0Sstevel@tonic-gate or die "Can't open '@_': $!"; 804*0Sstevel@tonic-gate return $fh; 805*0Sstevel@tonic-gate } 806*0Sstevel@tonic-gate 807*0Sstevel@tonic-gate { 808*0Sstevel@tonic-gate my $f = myopen("</etc/motd"); 809*0Sstevel@tonic-gate print <$f>; 810*0Sstevel@tonic-gate # $f implicitly closed here 811*0Sstevel@tonic-gate } 812*0Sstevel@tonic-gate 813*0Sstevel@tonic-gateNote that if an initialized scalar variable is used instead the 814*0Sstevel@tonic-gateresult is different: C<my $fh='zzz'; open($fh, ...)> is equivalent 815*0Sstevel@tonic-gateto C<open( *{'zzz'}, ...)>. 816*0Sstevel@tonic-gateC<use strict 'refs'> forbids such practice. 817*0Sstevel@tonic-gate 818*0Sstevel@tonic-gateAnother way to create anonymous filehandles is with the Symbol 819*0Sstevel@tonic-gatemodule or with the IO::Handle module and its ilk. These modules 820*0Sstevel@tonic-gatehave the advantage of not hiding different types of the same name 821*0Sstevel@tonic-gateduring the local(). See the bottom of L<perlfunc/open()> for an 822*0Sstevel@tonic-gateexample. 823*0Sstevel@tonic-gate 824*0Sstevel@tonic-gate=head1 SEE ALSO 825*0Sstevel@tonic-gate 826*0Sstevel@tonic-gateSee L<perlvar> for a description of Perl's built-in variables and 827*0Sstevel@tonic-gatea discussion of legal variable names. See L<perlref>, L<perlsub>, 828*0Sstevel@tonic-gateand L<perlmod/"Symbol Tables"> for more discussion on typeglobs and 829*0Sstevel@tonic-gatethe C<*foo{THING}> syntax. 830