perl 3.0 patch #22 patch #19, continued
[p5sagit/p5-mst-13.2.git] / perl.man.1
index f61350b..69f373f 100644 (file)
@@ -1,7 +1,22 @@
 .rn '' }`
-''' $Header: perl.man.1,v 3.0.1.1 89/11/11 04:41:22 lwall Locked $
+''' $Header: perl_man.1,v 3.0.1.5 90/03/27 16:14:37 lwall Locked $
 ''' 
 ''' $Log:      perl.man.1,v $
+''' Revision 3.0.1.5  90/03/27  16:14:37  lwall
+''' patch16: .. now works using magical string increment
+''' 
+''' Revision 3.0.1.4  90/03/12  16:44:33  lwall
+''' patch13: (LIST,) now legal
+''' patch13: improved LIST documentation
+''' patch13: example of if-elsif switch was wrong  
+''' 
+''' Revision 3.0.1.3  90/02/28  17:54:32  lwall
+''' patch9: @array in scalar context now returns length of array
+''' patch9: in manual, example of open and ?: was backwards
+''' 
+''' Revision 3.0.1.2  89/11/17  15:30:03  lwall
+''' patch5: fixed some manual typos and indent problems
+''' 
 ''' Revision 3.0.1.1  89/11/11  04:41:22  lwall
 ''' patch2: explained about sh and ${1+"$@"}
 ''' patch2: documented that space must separate word and '' string
@@ -413,7 +428,7 @@ scalar variables and values are interpreted as strings or numbers
 as appropriate to the context.
 A scalar is interpreted as TRUE in the boolean sense if it is not the null
 string or 0.
-Booleans returned by operators are 1 for true and \'0\' or \'\' (the null
+Booleans returned by operators are 1 for true and 0 or \'\' (the null
 string) for false.
 .PP
 There are actually two varieties of null string: defined and undefined.
@@ -478,9 +493,20 @@ The following are exactly equivalent
 
 .fi
 .PP
+If you evaluate an array in a scalar context, it returns the length of
+the array.
+The following is always true:
+.nf
+
+       @whatever == $#whatever \- $[ + 1;
+
+.fi
+.PP
 Multi-dimensional arrays are not directly supported, but see the discussion
 of the $; variable later for a means of emulating multiple subscripts with
 an associative array.
+You could also write a subroutine to turn multiple subscripts into a single
+subscript.
 .PP
 Every data type has its own namespace.
 You can, without fear of conflict, use the same name for a scalar variable,
@@ -612,7 +638,12 @@ bar
 
 .fi
 Array literals are denoted by separating individual values by commas, and
-enclosing the list in parentheses.
+enclosing the list in parentheses:
+.nf
+
+       (LIST)
+
+.fi
 In a context not requiring an array value, the value of the array literal
 is the value of the final element, as in the C comma operator.
 For example,
@@ -627,6 +658,46 @@ assigns the entire array value to array foo, but
 
 .fi
 assigns the value of variable bar to variable foo.
+Note that the value of an actual array in a scalar context is the length
+of the array; the following assigns to $foo the value 3:
+.nf
+
+.ne 2
+    @foo = (\'cc\', \'\-E\', $bar);
+    $foo = @foo;               # $foo gets 3
+
+.fi
+You may have an optional comma before the closing parenthesis of an
+array literal, so that you can say:
+.nf
+
+    @foo = (
+       1,
+       2,
+       3,
+    );
+
+.fi
+When a LIST is evaluated, each element of the list is evaluated in
+an array context, and the resulting array value is interpolated into LIST
+just as if each individual element were a member of LIST.  Thus arrays
+lose their identity in a LIST\*(--the list
+
+       (@foo,@bar,&SomeSub)
+
+contains all the elements of @foo followed by all the elements of @bar,
+followed by all the elements returned by the subroutine named SomeSub.
+.PP
+A list value may also be subscripted like a normal array.
+Examples:
+.nf
+
+       $time = (stat($file))[8];       # stat returns array value
+       $digit = ('a','b','c','d','e','f')[$digit-10];
+       return (pop(@foo),pop(@foo))[0];
+
+.fi
+.PP
 Array lists may be assigned to if and only if each element of the list
 is an lvalue:
 .nf
@@ -681,7 +752,7 @@ Evaluating a filehandle in angle brackets yields the next line
 from that file (newline included, so it's never false until EOF, at
 which time an undefined value is returned).
 Ordinarily you must assign that value to a variable,
-but there is one situation where in which an automatic assignment happens.
+but there is one situation where an automatic assignment happens.
 If (and only if) the input symbol is the only thing inside the conditional of a
 .I while
 loop, the value is
@@ -831,7 +902,7 @@ The only things that need to be declared in
 .I perl
 are report formats and subroutines.
 See the sections below for more information on those declarations.
-All uninitialized objects user-created objects are assumed to
+All uninitialized user-created objects are assumed to
 start with a null or 0 value until they
 are defined by some explicit operation such as assignment.
 The sequence of commands is executed just once, unlike in
@@ -893,7 +964,7 @@ The following all do the same thing:
        if (!open(foo)) { die "Can't open $foo: $!"; }
        die "Can't open $foo: $!" unless open(foo);
        open(foo) || die "Can't open $foo: $!"; # foo or bust!
-       open(foo) ? die "Can't open $foo: $!" : \'hi mom\';
+       open(foo) ? \'hi mom\' : die "Can't open $foo: $!";
                                # a bit exotic, that last one
 
 .fi
@@ -1031,9 +1102,9 @@ In addition to the above, you could write
 
 .ne 6
        foo: {
-               $abc = 1, last foo      if /^abc/;
-               $def = 1, last foo      if /^def/;
-               $xyz = 1, last foo      if /^xyz/;
+               $abc = 1, last foo  if /^abc/;
+               $def = 1, last foo  if /^def/;
+               $xyz = 1, last foo  if /^xyz/;
                $nothing = 1;
        }
 
@@ -1061,11 +1132,11 @@ or even
 
 .ne 8
        if (/^abc/)
-               { $abc = 1; last foo; }
+               { $abc = 1; }
        elsif (/^def/)
-               { $def = 1; last foo; }
+               { $def = 1; }
        elsif (/^xyz/)
-               { $xyz = 1; last foo; }
+               { $xyz = 1; }
        else
                {$nothing = 1;}
 
@@ -1382,3 +1453,22 @@ as a string, preserving each character within its range, with carry:
 
 .fi
 The autodecrement is not magical.
+.PP
+The range operator (in an array context) makes use of the magical
+autoincrement algorithm if the minimum and maximum are strings.
+You can say
+
+       @alphabet = (\'A\' .. \'Z\');
+
+to get all the letters of the alphabet, or
+
+       $hexdigit = (0 .. 9, \'a\' .. \'f\')[$num & 15];
+
+to get a hexadecimal digit, or
+
+       @z2 = (\'01\' .. \'31\');  print @z2[$mday];
+
+to get dates with leading zeros.
+(If the final value specified is not in the sequence that the magical increment
+would produce, the sequence goes until the next value would be longer than
+the final value specified.)