Change the push/unshift warning (#9532) to talk about "no values"
[p5sagit/p5-mst-13.2.git] / pod / perldata.pod
index e3361e4..315f716 100644 (file)
@@ -129,7 +129,8 @@ assignment to an array or hash evaluates the righthand side in list
 context.  Assignment to a list (or slice, which is just a list
 anyway) also evaluates the righthand side in list context.
 
-When you use Perl's B<-w> command-line option, you may see warnings
+When you use the C<use warnings> pragma or Perl's B<-w> command-line 
+option, you may see warnings
 about useless uses of constants or functions in "void context".
 Void context just means the value has been discarded, such as a
 statement containing only C<"fred";> or C<getpwuid(0);>.  It still
@@ -208,9 +209,9 @@ with a regular expression (as documented in L<perlre>).
        unless /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/;
 
 The length of an array is a scalar value.  You may find the length
-of array @days by evaluating C<$#days>, as in B<csh>.  Technically
-speaking, this isn't the length of the array; it's the subscript
-of the last element, since there is ordinarily a 0th element.
+of array @days by evaluating C<$#days>, as in B<csh>.  However, this
+isn't the length of the array; it's the subscript of the last element,
+which is a different value since there is ordinarily a 0th element.
 Assigning to C<$#days> actually changes the length of the array.
 Shortening an array this way destroys intervening values.  Lengthening
 an array that was previously shortened does not recover values
@@ -258,7 +259,7 @@ of sixteen buckets has been touched, and presumably contains all
 10,000 of your items.  This isn't supposed to happen.
 
 You can preallocate space for a hash by assigning to the keys() function.
-This rounds up the allocated bucked to the next power of two:
+This rounds up the allocated buckets to the next power of two:
 
     keys(%users) = 1000;               # allocate 1024 buckets
 
@@ -274,7 +275,6 @@ integer formats:
     0xff                # hex
     0377                # octal
     0b011011            # binary
-    v102.111.111        # string (made of characters "f", "o", "o")
 
 String literals are usually delimited by either single or double
 quotes.  They work much like quotes in the standard Unix shells:
@@ -282,7 +282,7 @@ double-quoted string literals are subject to backslash and variable
 substitution; single-quoted strings are not (except for C<\'> and
 C<\\>).  The usual C-style backslash rules apply for making
 characters such as newline, tab, etc., as well as some more exotic
-forms.  See L<perlop/"Quote and Quotelike Operators"> for a list.
+forms.  See L<perlop/"Quote and Quote-like Operators"> for a list.
 
 Hexadecimal, octal, or binary, representations in string literals
 (e.g. '0xff') are not automatically converted to their integer
@@ -303,7 +303,8 @@ price is $Z<>100."
     print "The price is $Price.\n";    # interpreted
 
 As in some shells, you can enclose the variable name in braces to
-disambiguate it from following alphanumerics.  You must also do
+disambiguate it from following alphanumerics (and underscores).
+You must also do
 this when interpolating a variable into a string to separate the
 variable name from a following double-colon or an apostrophe, since
 these would be otherwise treated as a package separator:
@@ -331,7 +332,13 @@ readable interpolation form C<"\x{1}\x{14}\x{12c}\x{fa0}">.  This is useful
 for representing Unicode strings, and for comparing version "numbers"
 using the string comparison operators, C<cmp>, C<gt>, C<lt> etc.
 If there are two or more dots in the literal, the leading C<v> may be
-omitted.  Such literals are accepted by both C<require> and C<use> for
+omitted.
+
+    print v9786;              # prints UTF-8 encoded SMILEY, "\x{263a}"
+    print v102.111.111;       # prints "foo"
+    print 102.111.111;        # same
+
+Such literals are accepted by both C<require> and C<use> for
 doing a version check.  The C<$^V> special variable also contains the
 running Perl interpreter's version in this form.  See L<perlvar/$^V>.
 
@@ -366,7 +373,8 @@ A word that has no other interpretation in the grammar will
 be treated as if it were a quoted string.  These are known as
 "barewords".  As with filehandles and labels, a bareword that consists
 entirely of lowercase letters risks conflict with future reserved
-words, and if you use the B<-w> switch, Perl will warn you about any
+words, and if you use the C<use warnings> pragma or the B<-w> switch, 
+Perl will warn you about any
 such words.  Some people may wish to outlaw barewords entirely.  If you
 say
 
@@ -405,20 +413,20 @@ string may be either an identifier (a word), or some quoted text.  If
 quoted, the type of quotes you use determines the treatment of the
 text, just as in regular quoting.  An unquoted identifier works like
 double quotes.  There must be no space between the C<< << >> and
-the identifier.  (If you put a space it will be treated as a null
-identifier, which is valid, and matches the first empty line.)  The
-terminating string must appear by itself (unquoted and with no
-surrounding whitespace) on the terminating line.
+the identifier, unless the identifier is quoted.  (If you put a space it
+will be treated as a null identifier, which is valid, and matches the first
+empty line.)  The terminating string must appear by itself (unquoted and
+with no surrounding whitespace) on the terminating line.
 
        print <<EOF;
     The price is $Price.
     EOF
 
-       print <<"EOF";  # same as above
+       print << "EOF"; # same as above
     The price is $Price.
     EOF
 
-       print <<`EOC`;  # execute commands
+       print << `EOC`; # execute commands
     echo hi there
     echo lo there
     EOC
@@ -429,7 +437,7 @@ surrounding whitespace) on the terminating line.
     I said bar.
     bar
 
-       myfunc(<<"THIS", 23, <<'THAT');
+       myfunc(<< "THIS", 23, <<'THAT');
     Here's a line
     or two.
     THIS
@@ -454,6 +462,39 @@ from each line manually:
        down from the door where it began.
     FINIS
 
+If you use a here-doc within a delimited construct, such as in C<s///eg>,
+the quoted material must come on the lines following the final delimiter.
+So instead of
+
+    s/this/<<E . 'that'
+    the other
+    E
+     . 'more '/eg;
+
+you have to write
+
+    s/this/<<E . 'that' 
+     . 'more '/eg; 
+    the other 
+    E 
+
+If the terminating identifier is on the last line of the program, you
+must be sure there is a newline after it; otherwise, Perl will give the
+warning B<Can't find string terminator "END" anywhere before EOF...>.
+
+Additionally, the quoting rules for the identifier are not related to
+Perl's quoting rules -- C<q()>, C<qq()>, and the like are not supported
+in place of C<''> and C<"">, and the only interpolation is for backslashing
+the quoting character:
+
+    print << "abc\"def";
+    testing...
+    abc"def
+
+Finally, quoted strings cannot span multiple lines.  The general rule is
+that the identifier must be a string literal.  Stick with that, and you
+should be safe.
+
 =head2 List value constructors
 
 List values are denoted by separating individual values by commas
@@ -516,6 +557,15 @@ has no effect.  Thus ((),(),()) is equivalent to ().  Similarly,
 interpolating an array with no elements is the same as if no
 array had been interpolated at that point.
 
+This interpolation combines with the facts that the opening
+and closing parentheses are optional (except necessary for
+precedence) and lists may end with an optional comma to mean that
+multiple commas within lists are legal syntax. The list C<1,,3> is a
+concatenation of two lists, C<1,> and C<3>, the first of which ends
+with that optional comma.  C<1,,3> is C<(1,),(3)> is C<1,3> (And
+similarly for C<1,,,3> is C<(1,),(,),3> is C<1,3> and so on.)  Not that
+we'd advise you to use this obfuscation.
+
 A list value may also be subscripted like a normal array.  You must
 put the list in parentheses to avoid ambiguity.  For example:
 
@@ -743,6 +793,28 @@ C<*HANDLE{IO}> only works if HANDLE has already been used as a handle.
 In other words, C<*FH> must be used to create new symbol table entries;
 C<*foo{THING}> cannot.  When in doubt, use C<*FH>.
 
+All functions that are capable of creating filehandles (open(),
+opendir(), pipe(), socketpair(), sysopen(), socket(), and accept())
+automatically create an anonymous filehandle if the handle passed to
+them is an uninitialized scalar variable. This allows the constructs
+such as C<open(my $fh, ...)> and C<open(local $fh,...)> to be used to
+create filehandles that will conveniently be closed automatically when
+the scope ends, provided there are no other references to them. This
+largely eliminates the need for typeglobs when opening filehandles
+that must be passed around, as in the following example:
+
+    sub myopen {
+        open my $fh, "@_"
+            or die "Can't open '@_': $!";
+       return $fh;
+    }
+
+    {
+        my $f = myopen("</etc/motd");
+       print <$f>;
+       # $f implicitly closed here
+    }
+
 Another way to create anonymous filehandles is with the Symbol
 module or with the IO::Handle module and its ilk.  These modules
 have the advantage of not hiding different types of the same name