X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=pod%2Fperlfunc.pod;h=2f9a85c8f6bb83e36a32497ea36ac04aab0b98b8;hb=98af1e142028dcf116f32636ea54f4c3e9494651;hp=0f4c4a8fd8c387925a9e2728ff2dd0e4a5ecf7dd;hpb=aeedbbed677eb39b533d78d495f79d82297f273f;p=p5sagit%2Fp5-mst-13.2.git diff --git a/pod/perlfunc.pod b/pod/perlfunc.pod index 0f4c4a8..2f9a85c 100644 --- a/pod/perlfunc.pod +++ b/pod/perlfunc.pod @@ -492,7 +492,8 @@ function, or use the familiar relation: sub tan { sin($_[0]) / cos($_[0]) } -Note that atan2(0, 0) is not well-defined. +The return value for C is implementation-defined; consult +your atan2(3) manpage for more information. =item bind SOCKET,NAME X @@ -1561,6 +1562,10 @@ determining whether a particular feature (such as C or C) is implemented. It is also Perl's exception trapping mechanism, where the die operator is used to raise exceptions. +If you want to trap errors when loading an XS module, some problems with +the binary interface (such as Perl version skew) may be fatal even with +C unless C<$ENV{PERL_DL_NONLAZY}> is set. See L. + If the code to be executed doesn't vary, you may use the eval-BLOCK form to trap run-time errors without incurring the penalty of recompiling each time. The error, if any, is still returned in C<$@>. @@ -1625,6 +1630,22 @@ normally you I like to use double quotes, except that in this particular situation, you can just use symbolic references instead, as in case 6. +The assignment to C<$@> occurs before restoration of localised variables, +which means a temporary is required if you want to mask some but not all +errors: + + # alter $@ on nefarious repugnancy only + { + my $e; + { + local $@; # protect existing $@ + eval { test_repugnancy() }; + # $@ =~ /nefarious/ and die $@; # DOES NOT WORK + $@ =~ /nefarious/ and $e = $@; + } + die $e if defined $e + } + C does I count as a loop, so the loop control statements C, C, or C cannot be used to leave or restart the block. @@ -1714,8 +1735,7 @@ X X Given an expression that specifies a hash element or array element, returns true if the specified element in the hash or array has ever -been initialized, even if the corresponding value is undefined. The -element is not autovivified if it doesn't exist. +been initialized, even if the corresponding value is undefined. print "Exists\n" if exists $hash{$key}; print "Defined\n" if defined $hash{$key}; @@ -1928,11 +1948,11 @@ Here's a mailbox appender for BSD systems. flock(MBOX,LOCK_UN); } - open(MBOX, ">>/usr/spool/mail/$ENV{'USER'}") + open(my $mbox, ">>", "/usr/spool/mail/$ENV{'USER'}") or die "Can't open mailbox: $!"; lock(); - print MBOX $msg,"\n\n"; + print $mbox $msg,"\n\n"; unlock(); On systems that support a real flock(), locks are inherited across fork() @@ -2327,8 +2347,14 @@ implementing the C<< <*.c> >> operator, but you can use it directly. If EXPR is omitted, C<$_> is used. The C<< <*.c> >> operator is discussed in more detail in L. +Note that C will split its arguments on whitespace, treating +each segment as separate pattern. As such, C would +match all files with a F<.c> or F<.h> extension. The expression +C would match all files in the current working directory. + Beginning with v5.6.0, this operator is implemented using the standard -C extension. See L for details. +C extension. See L for details, including +C which does not treat whitespace as a pattern separator. =item gmtime EXPR X X X @@ -2667,9 +2693,10 @@ X X =item length Returns the length in I of the value of EXPR. If EXPR is -omitted, returns length of C<$_>. Note that this cannot be used on -an entire array or hash to find out how many elements these have. -For that, use C and C respectively. +omitted, returns length of C<$_>. If EXPR is undefined, returns C. +Note that this cannot be used on an entire array or hash to find out how +many elements these have. For that, use C and C respectively. Note the I: if the EXPR is in Unicode, you will get the number of characters, not the number of bytes. To get the length @@ -2749,7 +2776,8 @@ Wednesday. C<$yday> is the day of the year, in the range C<0..364> C<$isdst> is true if the specified time occurs during Daylight Saving Time, false otherwise. -If EXPR is omitted, C uses the current time (C). +If EXPR is omitted, C uses the current time (as returned +by time(3)). In scalar context, C returns the ctime(3) value: @@ -3057,6 +3085,14 @@ X X X X Opens the file whose filename is given by EXPR, and associates it with FILEHANDLE. +Simple examples to open a file for reading: + + open(my $fh, '<', "input.txt") or die $!; + +and for writing: + + open(my $fh, '>', "output.txt") or die $!; + (The following is a comprehensive reference to open(): for a gentler introduction you may consider L.) @@ -3128,7 +3164,7 @@ You may use the three-argument form of open to specify IO "layers" that affect how the input and output are processed (see L and L for more details). For example - open(FH, "<:encoding(UTF-8)", "file") + open(my $fh, "<:encoding(UTF-8)", "file") will open the UTF-8 encoded file containing Unicode characters, see L. Note that if layers are specified in the @@ -3158,7 +3194,7 @@ working with an unopened filehandle is actually what you want to do. As a special case the 3-arg form with a read/write mode and the third argument being C: - open(TMP, "+>", undef) or die ... + open(my $tmp, "+>", undef) or die ... opens a filehandle to an anonymous temporary file. Also using "+<" works for symmetry, but you really should consider writing something @@ -3186,10 +3222,10 @@ Examples: open(LOG, '>>/usr/spool/news/twitlog'); # (log is reserved) # if the open fails, output is discarded - open(DBASE, '+<', 'dbase.mine') # open for update + open(my $dbase, '+<', 'dbase.mine') # open for update or die "Can't open 'dbase.mine' for update: $!"; - open(DBASE, '+new; open($handle, "myfile") or die "myfile: $!"; $first = <$handle> or return (); # Automatically closed here. @@ -3424,6 +3460,8 @@ scalar variable (or array or hash element), the variable is assigned a reference to a new anonymous dirhandle. DIRHANDLEs have their own namespace separate from FILEHANDLEs. +See example at C. + =item ord EXPR X X @@ -4050,12 +4088,6 @@ If the package name is null, the C
package as assumed. That is, C<$::sail> is equivalent to C<$main::sail> (as well as to C<$main'sail>, still seen in older code). -If NAMESPACE is omitted, then there is no current package, and all -identifiers must be fully qualified or lexicals. However, you are -strongly advised not to make use of this feature. Its use can cause -unexpected behaviour, even crashing some versions of Perl. It is -deprecated, and will be removed from a future release. - See L for more information about packages, modules, and classes. See L for other scoping issues. @@ -4188,13 +4220,15 @@ the completed C. =item qq/STRING/ -=item qr/STRING/ - =item qx/STRING/ =item qw/STRING/ -Generalized quotes. See L. +Generalized quotes. See L. + +=item qr/STRING/ + +Regexp-like quote. See L. =item quotemeta EXPR X X @@ -4275,9 +4309,9 @@ If you're planning to filetest the return values out of a C, you'd better prepend the directory in question. Otherwise, because we didn't C there, it would have been testing the wrong file. - opendir(DIR, $some_dir) || die "can't opendir $some_dir: $!"; - @dots = grep { /^\./ && -f "$some_dir/$_" } readdir(DIR); - closedir DIR; + opendir(my $dh, $some_dir) || die "can't opendir $some_dir: $!"; + @dots = grep { /^\./ && -f "$some_dir/$_" } readdir($dh); + closedir $dh; =item readline EXPR @@ -4693,13 +4727,16 @@ of LIST in the opposite order. In scalar context, concatenates the elements of LIST and returns a string value with all characters in the opposite order. - print reverse <>; # line tac, last line first + print join(", ", reverse "world", "Hello"); # Hello, world - undef $/; # for efficiency of <> - print scalar reverse <>; # character tac, last line tsrif + print scalar reverse "dlrow ,", "olleH"; # Hello, world Used without arguments in scalar context, reverse() reverses C<$_>. + $_ = "dlrow ,olleH"; + print reverse; # No output, list context + print scalar reverse; # Hello, world + This operator is also handy for inverting a hash, although there are some caveats. If a value is duplicated in the original hash, only one of those can be represented as a key in the inverted hash. Also, this has to @@ -5091,6 +5128,10 @@ It's also a more insistent form of close because it also disables the file descriptor in any forked copies in other processes. +Returns C<1> for success. In the case of error, returns C if +the first argument is not a valid filehandle, or returns C<0> and sets +C<$!> for any other failure. + =item sin EXPR X X X X @@ -5110,10 +5151,18 @@ X X =item sleep Causes the script to sleep for EXPR seconds, or forever if no EXPR. +Returns the number of seconds actually slept. + May be interrupted if the process receives a signal such as C. -Returns the number of seconds actually slept. You probably cannot -mix C and C calls, because C is often implemented -using C. + + eval { + local $SIG{ALARM} = sub { die "Alarm!\n" }; + sleep; + }; + die $@ unless $@ eq "Alarm!\n"; + +You probably cannot mix C and C calls, because C +is often implemented using C. On some older systems, it may sleep up to a full second less than what you requested, depending on how it counts seconds. Most modern systems @@ -5418,7 +5467,7 @@ a null pattern C, which is just one member of the set of patterns matching a null string) will split the value of EXPR into separate characters at each point it matches that way. For example: - print join(':', split(/ */, 'hi there')); + print join(':', split(/ */, 'hi there')), "\n"; produces the output 'h:i:t:h:e:r:e'. @@ -5427,7 +5476,7 @@ matches only the null string, and is not be confused with the regular use of C to mean "the last successful pattern match". So, for C, the following: - print join(':', split(//, 'hi there')); + print join(':', split(//, 'hi there')), "\n"; produces the output 'h:i: :t:h:e:r:e'. @@ -5442,8 +5491,8 @@ hand, are produced when there is a match at the end of the string (and when LIMIT is given and is not 0), regardless of the length of the match. For example: - print join(':', split(//, 'hi there!', -1)); - print join(':', split(/\W/, 'hi there!', -1)); + print join(':', split(//, 'hi there!', -1)), "\n"; + print join(':', split(/\W/, 'hi there!', -1)), "\n"; produce the output 'h:i: :t:h:e:r:e:!:' and 'hi:there:', respectively, both with an empty trailing field. @@ -5590,8 +5639,8 @@ to take the arguments out of order, e.g.: one or more of: - space prefix positive number with a space - + prefix positive number with a plus sign + space prefix non-negative number with a space + + prefix non-negative number with a plus sign - left-justify within the field 0 use zeros, not spaces, to right-justify # ensure the leading "0" for any octal, @@ -6863,22 +6912,16 @@ of perl older than the specified one. Specifying VERSION as a literal of the form v5.6.1 should generally be avoided, because it leads to misleading error messages under earlier -versions of Perl that do not support this syntax. The equivalent numeric -version should be used instead. - -Alternatively, you can use a numeric version C followed by a -v-string version like C, to avoid the unintuitive C. (older perl versions fail gracefully at the first C, -later perl versions understand the v-string syntax in the second). +versions of Perl (that is, prior to 5.6.0) that do not support this +syntax. The equivalent numeric version should be used instead. use v5.6.1; # compile time version check use 5.6.1; # ditto use 5.006_001; # ditto; preferred for backwards compatibility - use 5.006; use 5.6.1; # ditto, for compatibility and readability This is often useful if you need to check the current Perl version before -Cing library modules that have changed in incompatible ways from -older versions of Perl. (We try not to do this more than we have to.) +Cing library modules that won't work with older versions of Perl. +(We try not to do this more than we have to.) Also, if the specified perl version is greater than or equal to 5.9.5, C will also load the C pragma and enable all