.rn '' }`
-''' $Header: perl.man.1,v 3.0 89/10/18 15:21:29 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
+'''
''' Revision 3.0 89/10/18 15:21:29 lwall
''' 3.0 baseline
'''
.I perl
locates the script, it parses the lines and ignores them because
the variable $running_under_some_shell is never true.
+A better construct than $* would be ${1+"$@"}, which handles embedded spaces
+and such in the filenames, but doesn't work if the script is being interpreted
+by csh.
+In order to start up sh rather than csh, some systems may have to replace the
+#! line with a line containing just
+a colon, which will be politely ignored by perl.
.TP 5
.B \-u
causes
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.
.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,
.fi
Note that you can put curly brackets around the identifier to delimit it
from following alphanumerics.
+Also note that a single quoted string must be separated from a preceding
+word by a space, since single quote is a valid character in an identifier
+(see Packages).
.PP
Array values are interpolated into double-quoted strings by joining all the
elements of the array with the delimiter specified in the $" variable,
system "echo @ARGV";
.fi
-Within search patterns (which also undergo double-quoteish substitution)
+Within search patterns (which also undergo double-quotish substitution)
there is a bad ambiguity: Is /$foo[bar]/ to be
interpreted as /${foo}[bar]/ (where [bar] is a character class for the
regular expression) or as /${foo[bar]}/ (where [bar] is the subscript to
.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,
.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
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
.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
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
.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;
}
.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;}
.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.)