From: Tom Christiansen Date: Mon, 11 Jan 2010 10:04:08 +0000 (+0100) Subject: PATCH: perlfunc cleanup, part 1 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=80d38338d3280c3d336b87379c4da336e00e635b;p=p5sagit%2Fp5-mst-13.2.git PATCH: perlfunc cleanup, part 1 So I started tidying up the C and C documentation, but ended up diddling the file pretty much all over. There are some typo corrections, but most of this is just smoothing out infelicitous wording. I did add one brief example to C. I've made no warnings about delete and slices with negative indices; I'm hoping that may be fixed, as David has suggested be done. It's not a political edit, just a copy-edit. This is obviously only the first portion of the file, but the tweaks were getting extensive enough I thought I'd better send in a checkpoint. I'll finish it up over the next several days. --- diff --git a/pod/perlfunc.pod b/pod/perlfunc.pod index a40fbc3..40140ec 100644 --- a/pod/perlfunc.pod +++ b/pod/perlfunc.pod @@ -62,7 +62,7 @@ null list. Remember the following important rule: There is B that relates the behavior of an expression in list context to its behavior in scalar context, or vice versa. It might do two totally different things. -Each operator and function decides which sort of value it would be most +Each operator and function decides which sort of value would be most appropriate to return in scalar context. Some operators return the length of the list that would have been returned in list context. Some operators return the first value in the list. Some operators return the @@ -465,8 +465,8 @@ version of select() leaving the first three arguments undefined, or you might be able to use the C interface to access setitimer(2) if your system supports it. See L for details. -It is usually a mistake to intermix C and C calls. -(C may be internally implemented in your system with C) +It is usually a mistake to intermix C and C calls, because +C may be internally implemented on your system with C. If you want to use C to time out a system call you need to use an C/C pair. You can't rely on the alarm causing the system call to @@ -540,7 +540,7 @@ suitable for passing binary data. This includes turning off possible CRLF translation and marking it as bytes (as opposed to Unicode characters). Note that, despite what may be implied in I<"Programming Perl"> (the Camel) or elsewhere, C<:raw> is I simply the inverse of C<:crlf> --- other layers which would affect the binary nature of the stream are +--other layers that would affect the binary nature of the stream are I disabled. See L, L and the discussion about the PERLIO environment variable. @@ -635,8 +635,8 @@ X X X X =item caller Returns the context of the current subroutine call. In scalar context, -returns the caller's package name if there is a caller, that is, if -we're in a subroutine or C or C, and the undefined value +returns the caller's package name if there I a caller (that is, if +we're in a subroutine or C or C) and the undefined value otherwise. In list context, returns # 0 1 2 @@ -677,7 +677,7 @@ arguments with which the subroutine was invoked. Be aware that the optimizer might have optimized call frames away before C had a chance to get the information. That means that C -might not return information about the call frame you expect it do, for +might not return information about the call frame you expect it to, for C<< N > 1 >>. In particular, C<@DB::args> might have information from the previous time C was called. @@ -696,10 +696,10 @@ Changes the working directory to EXPR, if possible. If EXPR is omitted, changes to the directory specified by C<$ENV{HOME}>, if set; if not, changes to the directory specified by C<$ENV{LOGDIR}>. (Under VMS, the variable C<$ENV{SYS$LOGIN}> is also checked, and used if it is set.) If -neither is set, C does nothing. It returns true upon success, +neither is set, C does nothing. It returns true on success, false otherwise. See the example under C. -On systems that support fchdir, you might pass a file handle or +On systems that support fchdir, you may pass a file handle or directory handle as argument. On systems that don't support fchdir, passing handles produces a fatal error at run time. @@ -719,7 +719,7 @@ successfully changed. See also L, if all you have is a string. $mode = '0644'; chmod oct($mode), 'foo'; # this is better $mode = 0644; chmod $mode, 'foo'; # this is best -On systems that support fchmod, you might pass file handles among the +On systems that support fchmod, you may pass file handles among the files. On systems that don't support fchmod, passing file handles produces a fatal error at run time. The file handles must be passed as globs or references to be recognized. Barewords are considered @@ -813,7 +813,7 @@ successfully changed. $cnt = chown $uid, $gid, 'foo', 'bar'; chown $uid, $gid, @filenames; -On systems that support fchown, you might pass file handles among the +On systems that support fchown, you may pass file handles among the files. On systems that don't support fchown, passing file handles produces a fatal error at run time. The file handles must be passed as globs or references to be recognized. Barewords are considered @@ -851,7 +851,7 @@ For example, C is C<"A"> in either ASCII or Unicode, and chr(0x263a) is a Unicode smiley face. Negative values give the Unicode replacement character (chr(0xfffd)), -except under the L pragma, where low eight bits of the value +except under the L pragma, where the low eight bits of the value (truncated to an integer) are used. If NUMBER is omitted, uses C<$_>. @@ -895,15 +895,15 @@ If the file handle came from a piped open, C will additionally return false if one of the other system calls involved fails, or if the program exits with non-zero status. (If the only problem was that the program exited non-zero, C<$!> will be set to C<0>.) Closing a pipe -also waits for the process executing on the pipe to complete, in case you -want to look at the output of the pipe afterwards, and +also waits for the process executing on the pipe to exit, in case you +wish to look at the output of the pipe afterwards, and implicitly puts the exit status value of that command into C<$?> and C<${^CHILD_ERROR_NATIVE}>. -Prematurely closing the read end of a pipe (i.e. before the process -writing to it at the other end has closed it) will result in a -SIGPIPE being delivered to the writer. If the other end can't -handle that, be sure to read all the data before closing the pipe. +Closing the read end of a pipe before the process writing to it at the +other end is done writing results in the writer receiving a SIGPIPE. If +the other end can't handle that, be sure to read all the data before +closing the pipe. Example: @@ -928,8 +928,8 @@ system call. =item connect SOCKET,NAME X -Attempts to connect to a remote socket, just as the connect system call -does. Returns true if it succeeded, false otherwise. NAME should be a +Attempts to connect to a remote socket, just like connect(2). +Returns true if it succeeded, false otherwise. NAME should be a packed address of the appropriate type for the socket. See the examples in L. @@ -1081,7 +1081,7 @@ argument is I a filehandle, even though it looks like one). DBNAME is the name of the database (without the F<.dir> or F<.pag> extension if any). If the database does not exist, it is created with protection specified by MASK (as modified by the C). If your system supports -only the older DBM functions, you may perform only one C in your +only the older DBM functions, you may make only one C call in your program. In older versions of Perl, if your system had neither DBM nor ndbm, calling C produced a fatal error; it now falls back to sdbm(3). @@ -1134,9 +1134,9 @@ element to return happens to be C. You may also use C to check whether subroutine C<&func> has ever been defined. The return value is unaffected by any forward -declarations of C<&func>. Note that a subroutine which is not defined +declarations of C<&func>. A subroutine that is not defined may still be callable: its package may have an C method that -makes it spring into existence the first time that it is called -- see +makes it spring into existence the first time that it is called--see L. Use of C on aggregates (hashes and arrays) is deprecated. It @@ -1166,7 +1166,7 @@ defined values. For example, if you say "ab" =~ /a(.*)b/; -The pattern match succeeds, and C<$1> is defined, despite the fact that it +The pattern match succeeds and C<$1> is defined, although it matched "nothing". It didn't really fail to match anything. Rather, it matched something that happened to be zero characters long. This is all very above-board and honest. When a function returns an undefined value, @@ -1180,33 +1180,37 @@ See also L, L, L. =item delete EXPR X -Given an expression that specifies a hash element, array element, hash slice, -or array slice, deletes the specified element(s) from the hash or array. -In the case of an array, if the array elements happen to be at the end, -the size of the array will shrink to the highest element that tests -true for exists() (or 0 if no such element exists). +Given an expression that specifies an element or slice of an aggregate (a +hash or an array), deletes the specified elements from that aggregate so +that exists() on that element no longer returns true. Setting an aggregate +element to the undefined value does not remove its key, but deleting it +does; see L. + +Returns the value or values deleted in list context, or the last such +element in scalar context. The return list's length always matches that of +the argument list: deleting non-existent elements returns the undefined +value in their corresponding positions. + +Deleting array elements never changes indices of existing values; use +shift() or splice() for that. However, if all deleted elements fall at +the end of an array, the array's size shrinks to the position of the +highest element that still tests true for exists(), or to 0 if none do. + +Deleting from C<%ENV> modifies the environment. Deleting from a hash tied to +a DBM file deletes the entry from the DBM file. Deleting from a C hash +or array may not necessarily return anything; it depends on the implementation +of the C package's DELETE method, which may do whatever it pleases. -Returns a list with the same number of elements as the number of elements -for which deletion was attempted. Each element of that list consists of -either the value of the element deleted, or the undefined value. In scalar -context, this means that you get the value of the last element deleted (or -the undefined value if that element did not exist). +The C construct localizes the deletion to the current +block at run time. Until the block exits, elements locally deleted +temporarily no longer exist. See L. %hash = (foo => 11, bar => 22, baz => 33); $scalar = delete $hash{foo}; # $scalar is 11 $scalar = delete @hash{qw(foo bar)}; # $scalar is 22 @array = delete @hash{qw(foo bar baz)}; # @array is (undef,undef,33) -Deleting from C<%ENV> modifies the environment. Deleting from -a hash tied to a DBM file deletes the entry from the DBM file. Deleting -from a Cd hash or array may not necessarily return anything. - -Deleting an array element effectively returns that position of the array -to its initial, uninitialized state. Subsequently testing for the same -element with exists() will return false. Also, deleting array elements -in the middle of an array will not shift the index of the elements -after them down. Use splice() for that. See L. - The following (inefficiently) deletes all the values of %HASH and @ARRAY: foreach $key (keys %HASH) { @@ -1223,8 +1227,9 @@ And so do these: delete @ARRAY[0 .. $#ARRAY]; -But both of these are slower than just assigning the empty list -or undefining %HASH or @ARRAY: +But both are slower than assigning the empty list +or undefining %HASH or @ARRAY, which is the customary +way to empty out an aggregate: %HASH = (); # completely empty %HASH undef %HASH; # forget %HASH ever existed @@ -1232,9 +1237,8 @@ or undefining %HASH or @ARRAY: @ARRAY = (); # completely empty @ARRAY undef @ARRAY; # forget @ARRAY ever existed -Note that the EXPR can be arbitrarily complicated as long as the final -operation is a hash element, array element, hash slice, or array slice -lookup: +The EXPR can be arbitrarily complicated provided its +final operation is an element or slice of an aggregate: delete $ref->[$x][$y]{$key}; delete @{$ref->[$x][$y]}{$key1, $key2, @morekeys}; @@ -1242,10 +1246,6 @@ lookup: delete $ref->[$x][$y][$index]; delete @{$ref->[$x][$y]}[$index1, $index2, @moreindices]; -The C construct can also be used to localize the deletion -of array/hash elements to the current block. -See L. - =item die LIST X X X X X<$@> X @@ -1293,23 +1293,22 @@ This is useful for propagating exceptions: If the output is empty and C<$@> contains an object reference that has a C method, that method will be called with additional file and line number parameters. The return value replaces the value in -C<$@>. i.e. as if C<< $@ = eval { $@->PROPAGATE(__FILE__, __LINE__) }; >> +C<$@>. i.e., as if C<< $@ = eval { $@->PROPAGATE(__FILE__, __LINE__) }; >> were called. If C<$@> is empty then the string C<"Died"> is used. -die() can also be called with a reference argument. If this happens to be -trapped within an eval(), $@ contains the reference. This behavior permits -a more elaborate exception handling implementation using objects that -maintain arbitrary state about the nature of the exception. Such a scheme -is sometimes preferable to matching particular string values of $@ using -regular expressions. Because $@ is a global variable, and eval() may be -used within object implementations, care must be taken that analyzing the -error object doesn't replace the reference in the global variable. The -easiest solution is to make a local copy of the reference before doing -other manipulations. Here's an example: +You can also call C with a reference argument, and if this is trapped +within an C, C<$@> contains that reference. This permits more +elaborate exception handling using objects that maintain arbitrary state +about the exception. Such a scheme is sometimes preferable to matching +particular string values of C<$@> with regular expressions. Because C<$@> +is a global variable and C may be used within object implementations, +be careful that analyzing the error object doesn't replace the reference in +the global variable. It's easiest to make a local copy of the reference +before any manipulations. Here's an example: - use Scalar::Util 'blessed'; + use Scalar::Util "blessed"; eval { ... ; die Some::Module::Exception->new( FOO => "bar" ) }; if (my $ev_err = $@) { @@ -1321,8 +1320,8 @@ other manipulations. Here's an example: } } -Because perl will stringify uncaught exception messages before displaying -them, you may want to overload stringification operations on such custom +Because perl stringifies uncaught exception messages before display, +you'll probably want to overload stringification operations on exception objects. See L for details about that. You can arrange for a callback to be run just before the C @@ -1386,7 +1385,7 @@ returns undef and sets an error message in C<$@>. If the file is successfully compiled, C returns the value of the last expression evaluated. -Note that inclusion of library modules is better done with the +Inclusion of library modules is better done with the C and C operators, which also do automatic error checking and raise an exception if there's a problem. @@ -1421,7 +1420,7 @@ If C