This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
-Drv now turns on all regex debugging
[perl5.git] / pod / perldata.pod
index b456a81..d03fe25 100644 (file)
@@ -25,7 +25,7 @@ be a chain of identifiers, separated by C<::> (or by the slightly
 archaic C<'>); all but the last are interpreted as names of packages,
 to locate the namespace in which to look up the final identifier
 (see L<perlmod/Packages> for details).  For a more in-depth discussion
-on identifiers, see L<Identifier parsing>.  It's possible to
+on identifiers, see L</Identifier parsing>.  It's possible to
 substitute for a simple identifier, an expression that produces a reference
 to the value at runtime.   This is described in more detail below
 and in L<perlref>.
@@ -37,8 +37,8 @@ collide with one of your normal variables.  Strings that match
 parenthesized parts of a regular expression are saved under names
 containing only digits after the C<$> (see L<perlop> and L<perlre>).
 In addition, several special variables that provide windows into
-the inner working of Perl have names containing punctuation characters
-and control characters.  These are documented in L<perlvar>.
+the inner working of Perl have names containing punctuation characters.
+These are documented in L<perlvar>.
 X<variable, built-in>
 
 Scalar values are always named with '$', even when referring to a
@@ -99,11 +99,11 @@ that returns a reference to the appropriate type.  For a description
 of this, see L<perlref>.
 
 Names that start with a digit may contain only more digits.  Names
-that do not start with a letter, underscore, digit or a caret (i.e.
-a control character) are limited to one character, e.g.,  C<$%> or
+that do not start with a letter, underscore, digit or a caret are
+limited to one character, e.g.,  C<$%> or
 C<$$>.  (Most of these one character names have a predefined
 significance to Perl.  For instance, C<$$> is the current process
-id.)
+id.  And all such names are reserved for Perl's possible use.)
 
 =head2 Identifier parsing
 X<identifiers>
@@ -129,7 +129,7 @@ match C<\w> (this prevents some problematic cases); and Perl
 additionally accepts identfier names beginning with an underscore.
 
 If not under C<use utf8>, the source is treated as ASCII + 128 extra
-controls, and identifiers should match
+generic characters, and identifiers should match
 
     / (?aa) (?!\d) \w+ /x
 
@@ -184,53 +184,66 @@ Put together, a grammar to match a basic identifier becomes
 Meanwhile, special identifiers don't follow the above rules; For the most
 part, all of the identifiers in this category have a special meaning given
 by Perl.  Because they have special parsing rules, these generally can't be
-fully-qualified.  They come in four forms:
+fully-qualified.  They come in six forms (but don't use forms 5 and 6):
 
 =over
 
-=item *
+=item 1.
 
 A sigil, followed solely by digits matching C<\p{POSIX_Digit}>, like
 C<$0>, C<$1>, or C<$10000>.
 
-=item *
-
-A sigil, followed by either a caret and a single POSIX uppercase letter,
-like C<$^V> or C<$^W>, or a sigil followed by a literal non-space,
-non-C<NUL> control character matching the C<\p{POSIX_Cntrl}> property.
-Due to a historical oddity, if not running under C<use utf8>, the 128
-characters in the C<[0x80-0xff]> range are considered to be controls,
-and may also be used in length-one variables.  However, the use of
-non-graphical characters is deprecated as of v5.22, and support for them
-will be removed in a future version of perl.  ASCII space characters and
-C<NUL> already aren't allowed, so this means that a single-character
-variable name with that name being any other C0 control C<[0x01-0x1F]>,
-or C<DEL> will generate a deprecated warning.  Already, under C<"use
-utf8">, non-ASCII characters must match C<Perl_XIDS>.  As of v5.22, when
-not under C<"use utf8"> C1 controls C<[0x80-0x9F]>, NO BREAK SPACE, and
-SOFT HYPHEN (C<SHY>)) generate a deprecated warning.
-
-=item *
-
-Similar to the above, a sigil, followed by bareword text in brackets,
-where the first character is either a caret followed by an uppercase
-letter, like C<${^GLOBAL_PHASE}> or a non-C<NUL>, non-space literal
-control like C<${\7LOBAL_PHASE}>.  Like the above, when not under
-C<"use utf8">, the characters in C<[0x80-0xFF]> are considered controls, but as
-of v5.22, the use of any that are non-graphical are deprecated, and as
-of v5.20 the use of any ASCII-range literal control is deprecated.
-Support for these will be removed in a future version of perl.
-
-=item *
+=item 2.
 
 A sigil followed by a single character matching the C<\p{POSIX_Punct}>
 property, like C<$!> or C<%+>, except the character C<"{"> doesn't work.
 
+=item 3.
+
+A sigil, followed by a caret and any one of the characters
+C<[][A-Z^_?\]>, like C<$^V> or C<$^]>.
+
+=item 4.
+
+Similar to the above, a sigil, followed by bareword text in braces,
+where the first character is a caret.  The next character is any one of
+the characters C<[][A-Z^_?\]>, followed by ASCII word characters.  An
+example is C<${^GLOBAL_PHASE}>.
+
+=item 5.
+
+A sigil, followed by any single character in the range C<[\xA1-\xAC\xAE-\xFF]>
+when not under C<S<"use utf8">>.  (Under C<S<"use utf8">>, the normal
+identifier rules given earlier in this section apply.)  Use of
+non-graphic characters (the C1 controls, the NO-BREAK SPACE, and the
+SOFT HYPHEN) has been disallowed since v5.26.0.
+The use of the other characters is unwise, as these are all
+reserved to have special meaning to Perl, and none of them currently
+do have special meaning, though this could change without notice.
+
+Note that an implication of this form is that there are identifiers only
+legal under C<S<"use utf8">>, and vice-versa, for example the identifier
+C<$E<233>tat> is legal under C<S<"use utf8">>, but is otherwise
+considered to be the single character variable C<$E<233>> followed by
+the bareword C<"tat">, the combination of which is a syntax error.
+
+=item 6.
+
+This is a combination of the previous two forms.  It is valid only when
+not under S<C<"use utf8">> (normal identifier rules apply when under
+S<C<"use utf8">>).  The form is a sigil, followed by text in braces,
+where the first character is any one of the characters in the range
+C<[\x80-\xFF]> followed by ASCII word characters up to the trailing
+brace.
+
+The same caveats as the previous form apply:  The non-graphic
+characters are no longer allowed with S<"use utf8">, it is unwise
+to use this form at all, and utf8ness makes a big difference.
+
 =back
 
-Note that as of Perl 5.20, literal control characters in variable names
-are deprecated; and as of Perl 5.22, any other non-graphic characters
-are also deprecated.
+Prior to Perl v5.24, non-graphical ASCII control characters were also
+allowed in some situations; this had been deprecated since v5.20.
 
 =head2 Context
 X<context> X<scalar context> X<list context>
@@ -307,12 +320,17 @@ are considered pretty much the same thing for nearly all purposes,
 references are strongly-typed, uncastable pointers with builtin
 reference-counting and destructor invocation.
 
+X<truth> X<falsehood> X<true> X<false> X<!> X<not> X<negation> X<0>
+X<boolean> X<bool>
 A scalar value is interpreted as FALSE in the Boolean sense
 if it is undefined, the null string or the number 0 (or its
 string equivalent, "0"), and TRUE if it is anything else.  The
 Boolean context is just a special kind of scalar context where no 
 conversion to a string or a number is ever performed.
-X<boolean> X<bool> X<true> X<false> X<truth>
+Negation of a true value by C<!> or C<not> returns a special false value.
+When evaluated as a string it is treated as C<"">, but as a number, it
+is treated as 0.  Most Perl operators
+that return true or false behave this way.
 
 There are actually two varieties of null strings (sometimes referred
 to as "empty" strings), a defined one and an undefined one.  The
@@ -386,18 +404,25 @@ leave nothing to doubt:
 
     $element_count = scalar(@whatever);
 
-If you evaluate a hash in scalar context, it returns false if the
-hash is empty.  If there are any key/value pairs, it returns true;
-more precisely, the value returned is a string consisting of the
+If you evaluate a hash in scalar context, it returns a false value if
+the hash is empty.  If there are any key/value pairs, it returns a
+true value.  A more precise definition is version dependent.
+
+Prior to Perl 5.25 the value returned was a string consisting of the
 number of used buckets and the number of allocated buckets, separated
 by a slash.  This is pretty much useful only to find out whether
 Perl's internal hashing algorithm is performing poorly on your data
 set.  For example, you stick 10,000 things in a hash, but evaluating
 %HASH in scalar context reveals C<"1/16">, which means only one out
 of sixteen buckets has been touched, and presumably contains all
-10,000 of your items.  This isn't supposed to happen.  If a tied hash
-is evaluated in scalar context, the C<SCALAR> method is called (with a
-fallback to C<FIRSTKEY>).
+10,000 of your items.  This isn't supposed to happen.
+
+As of Perl 5.25 the return was changed to be the count of keys in the
+hash. If you need access to the old behavior you can use
+C<Hash::Util::bucket_ratio()> instead.
+
+If a tied hash is evaluated in scalar context, the C<SCALAR> method is
+called (with a fallback to C<FIRSTKEY>).
 X<hash, scalar context> X<hash, bucket> X<bucket>
 
 You can preallocate space for a hash by assigning to the keys() function.
@@ -502,6 +527,37 @@ anything more complicated in the subscript will be interpreted as an
 expression.  This means for example that C<$version{2.0}++> is
 equivalent to C<$version{2}++>, not to C<$version{'2.0'}++>.
 
+=head3 Special floating point: infinity (Inf) and not-a-number (NaN)
+
+Floating point values include the special values C<Inf> and C<NaN>,
+for infinity and not-a-number.  The infinity can be also negative.
+
+The infinity is the result of certain math operations that overflow
+the floating point range, like 9**9**9.  The not-a-number is the
+result when the result is undefined or unrepresentable.  Though note
+that you cannot get C<NaN> from some common "undefined" or
+"out-of-range" operations like dividing by zero, or square root of
+a negative number, since Perl generates fatal errors for those.
+
+The infinity and not-a-number have their own special arithmetic rules.
+The general rule is that they are "contagious": C<Inf> plus one is
+C<Inf>, and C<NaN> plus one is C<NaN>.  Where things get interesting
+is when you combine infinities and not-a-numbers: C<Inf> minus C<Inf>
+and C<Inf> divided by C<Inf> are C<NaN> (while C<Inf> plus C<Inf> is
+C<Inf> and C<Inf> times C<Inf> is C<Inf>).  C<NaN> is also curious
+in that it does not equal any number, I<including> itself:
+C<NaN> != C<NaN>.
+
+Perl doesn't understand C<Inf> and C<NaN> as numeric literals, but
+you can have them as strings, and Perl will convert them as needed:
+"Inf" + 1.  (You can, however, import them from the POSIX extension;
+C<use POSIX qw(Inf NaN);> and then use them as literals.)
+
+Note that on input (string to number) Perl accepts C<Inf> and C<NaN>
+in many forms.   Case is ignored, and the Win32-specific forms like
+C<1.#INF> are understood, but on output the values are normalized to
+C<Inf> and C<NaN>.
+
 =head3 Version Strings
 X<version string> X<vstring> X<v-string>
 
@@ -531,57 +587,6 @@ Perl 5.8.0, but that caused more confusion and breakage than good.
 Multi-number v-strings like C<v65.66> and C<65.66.67> continue to
 be v-strings always.
 
-=head3 Special floating point
-
-Floating point values include the special values C<Inf> and C<NaN>,
-for infinity and not-a-number.  The infinity can be also negative.
-
-The infinity is the result of certain math operations that overflow
-the floating point range, like 9**9**9.  The not-a-number is the
-result when the result is undefined or unrepresentable.  Though note
-that you cannot get C<NaN> from some common "undefined" or
-"out-of-range" operations like dividing by zero, or square root of
-a negative number, since Perl generates fatal errors for those.
-
-The infinity and not-a-number have their own special arithmetic rules.
-The general rule is that they are "contagious": C<Inf> plus one is
-C<Inf>, and C<NaN> plus one is C<NaN>.  Where things get interesting
-is when you combine infinities and not-a-numbers: C<Inf> minus C<Inf>
-and C<Inf> divided by C<INf> are C<NaN> (while C<Inf> plus C<Inf> is
-C<Inf> and C<Inf> times C<Inf> is C<Inf>).
-
-Perl doesn't understand C<Inf> and C<NaN> as numeric literals, but you
-can have them as strings, and Perl will convert them as needed: "Inf" + 1.
-(If you want to have them as kind of literals, you can import them from
-the POSIX extension.)
-
-Note that on input (string to number) Perl accepts C<Inf> and C<NaN>
-in many forms.   Case is ignored, and the Win32-specific forms like
-C<1.#INF> are understood, but on output the values are normalized to
-C<Inf> and C<NaN>.
-
-The C<NaN> has two special features of its own.  Firstly, it comes in
-two flavors, quiet and signaling.  What this means is depends on the
-platform.  Secondly, it may have "payload" of a number of bits.  The
-number of bits available again depends on the platform.  (Though for
-the most common floating point format, 64-bit IEEE 754, there is
-room for 51 bits.)
-
-You can generate a NaN with payload with e.g. "nan(0x123)" or "nans(456)".
-Note that you have to be aware of the binary representation of the payload:
-because of how negative numbers are stored, they often cannot be used as
-the payload.
-
-The payload (and quiet/signaling flag) is propagated on straight
-copies, but on operations (like addition) the result (which payload
-bits end up where) again depends on the platform.
-
-The default stringification of not-a-numbers will just show the C<NaN>
-but you can use C<printf %#g> to see the payload: the C<#> is the key.
-The payload will be shown as hexadecimal integer if possible on
-the platform (floating point values may have more bits than integers),
-if not, as a string of hexadecimal bytes.
-
 =head3 Special Literals
 X<special literal> X<__END__> X<__DATA__> X<END> X<DATA>
 X<end> X<data> X<^D> X<^Z>
@@ -614,6 +619,17 @@ introduced, __END__ behaves like __DATA__ in the top level script (but
 not in files loaded with C<require> or C<do>) and leaves the remaining
 contents of the file accessible via C<main::DATA>.
 
+The C<DATA> file handle by default has whatever PerlIO layers were
+in place when Perl read the file to parse the source.  Normally that
+means that the file is being read bytewise, as if it were encoded in
+Latin-1, but there are two major ways for it to be otherwise.  Firstly,
+if the C<__END__>/C<__DATA__> token is in the scope of a C<use utf8>
+pragma then the C<DATA> handle will be in UTF-8 mode.  And secondly,
+if the source is being read from perl's standard input then the C<DATA>
+file handle is actually aliased to the C<STDIN> file handle, and may
+be in UTF-8 mode because of the C<PERL_UNICODE> environment variable or
+perl's command-line switches.
+
 See L<SelfLoader> for more description of __DATA__, and
 an example of its use.  Note that you cannot read from the DATA
 filehandle in a BEGIN block: the BEGIN block is executed as soon
@@ -773,6 +789,53 @@ As of Perl 5.22, you can also use C<(undef)x2> instead of C<undef, undef>.
 (You can also do C<($x) x 2>, which is less useful, because it assigns to
 the same variable twice, clobbering the first value assigned.)
 
+When you assign a list of scalars to an array, all previous values in that
+array are wiped out and the number of elements in the array will now be equal to
+the number of elements in the right-hand list -- the list from which
+assignment was made.  The array will automatically resize itself to precisely
+accommodate each element in the right-hand list.
+
+    use warnings;
+    my (@xyz, $x, $y, $z);
+
+    @xyz = (1, 2, 3);
+    print "@xyz\n";                             # 1 2 3
+
+    @xyz = ('al', 'be', 'ga', 'de');
+    print "@xyz\n";                             # al be ga de
+
+    @xyz = (101, 102);
+    print "@xyz\n";                             # 101 102
+
+When, however, you assign a list of scalars to another list of scalars, the
+results differ according to whether the left-hand list -- the list being
+assigned to -- has the same, more or fewer elements than the right-hand list.
+
+    ($x, $y, $z) = (1, 2, 3);
+    print "$x $y $z\n";                         # 1 2 3
+
+    ($x, $y, $z) = ('al', 'be', 'ga', 'de');
+    print "$x $y $z\n";                         # al be ga
+
+    ($x, $y, $z) = (101, 102);
+    print "$x $y $z\n";                         # 101 102
+    # Use of uninitialized value $z in concatenation (.)
+    # or string at [program] line [line number].
+
+If the number of scalars in the left-hand list is less than that in the
+right-hand list, the "extra" scalars in the right-hand list will simply not be
+assigned.
+
+If the number of scalars in the left-hand list is greater than that in the
+left-hand list, the "missing" scalars will become undefined.
+
+    ($x, $y, $z) = (101, 102);
+    for my $el ($x, $y, $z) {
+        (defined $el) ? print "$el " : print "<undef>";
+    }
+    print "\n";
+                                                # 101 102 <undef>
+
 List assignment in scalar context returns the number of elements
 produced by the expression on the right side of the assignment:
 
@@ -1014,8 +1077,11 @@ returning a list of key/value pairs rather than just values:
     %h = (blonk => 2, foo => 3, squink => 5, bar => 8);
     %subset = %h{'foo', 'bar'}; # key/value hash slice
     # %subset is now (foo => 3, bar => 8)
+    %removed = delete %h{'foo', 'bar'};
+    # %removed is now (foo => 3, bar => 8)
+    # %h is now (blonk => 2, squink => 5)
 
-However, the result of such a slice cannot be localized, deleted or used
+However, the result of such a slice cannot be localized or used
 in assignment.  These are otherwise very much consistent with hash slices
 using the @ symbol.
 
@@ -1028,6 +1094,12 @@ of index/value pairs:
     @a = "a".."z";
     @list = %a[3,4,6];
     # @list is now (3, "d", 4, "e", 6, "g")
+    @removed = delete %a[3,4,6]
+    # @removed is now (3, "d", 4, "e", 6, "g")
+    # @list[3,4,6] are now undef
+
+Note that calling L<C<delete>|perlfunc/delete EXPR> on array values is
+strongly discouraged.
 
 =head2 Typeglobs and Filehandles
 X<typeglob> X<filehandle> X<*>