From be539103222af61a863391d2f08292643e590572 Mon Sep 17 00:00:00 2001 From: Chris 'BinGOs' Williams Date: Sat, 22 Oct 2011 20:08:35 +0100 Subject: [PATCH] Update perlfaq to CPAN version 5.0150036 [DELTA] 5.0150036 Sat 22 Oct 2011 16:20:34 +0100 * Website moved from faq.perl.org -> learn.perl.org (ranguard) * Delete some questions/cleanup copy (ranguard) * Make perlfaq.pod shorter/cleaner (kablamo) * Many cleanups and corrections (shlomif) --- Porting/Maintainers.pl | 2 +- cpan/perlfaq/lib/perlfaq.pm | 2 +- cpan/perlfaq/lib/perlfaq.pod | 85 ++++++++++++++++++--------------------- cpan/perlfaq/lib/perlfaq2.pod | 10 ++--- cpan/perlfaq/lib/perlfaq3.pod | 25 +----------- cpan/perlfaq/lib/perlfaq5.pod | 26 ++++++------ cpan/perlfaq/lib/perlfaq6.pod | 48 ++++++++++++---------- cpan/perlfaq/lib/perlfaq7.pod | 58 +++++++++++++------------- cpan/perlfaq/lib/perlfaq8.pod | 72 ++++++++++++++++++--------------- cpan/perlfaq/lib/perlfaq9.pod | 33 +++++++-------- cpan/perlfaq/lib/perlglossary.pod | 12 +++--- pod/perldelta.pod | 4 ++ 12 files changed, 183 insertions(+), 194 deletions(-) diff --git a/Porting/Maintainers.pl b/Porting/Maintainers.pl index 7ae7bcf..8ad20eb 100755 --- a/Porting/Maintainers.pl +++ b/Porting/Maintainers.pl @@ -764,7 +764,7 @@ use File::Glob qw(:case); 'perlfaq' => { 'MAINTAINER' => 'perlfaq', - 'DISTRIBUTION' => 'LLAP/perlfaq-5.0150035.tar.gz', + 'DISTRIBUTION' => 'LLAP/perlfaq-5.0150036.tar.gz', 'FILES' => q[cpan/perlfaq], 'EXCLUDED' => [ qw(t/release-pod-syntax.t t/release-eol.t t/release-no-tabs.t) ], diff --git a/cpan/perlfaq/lib/perlfaq.pm b/cpan/perlfaq/lib/perlfaq.pm index 416a988..0b97525 100644 --- a/cpan/perlfaq/lib/perlfaq.pm +++ b/cpan/perlfaq/lib/perlfaq.pm @@ -1,6 +1,6 @@ package perlfaq; { - $perlfaq::VERSION = '5.0150035'; + $perlfaq::VERSION = '5.0150036'; } 0; # not is it supposed to be loaded diff --git a/cpan/perlfaq/lib/perlfaq.pod b/cpan/perlfaq/lib/perlfaq.pod index 095c436..ff269a3 100644 --- a/cpan/perlfaq/lib/perlfaq.pod +++ b/cpan/perlfaq/lib/perlfaq.pod @@ -8,65 +8,41 @@ The perlfaq comprises several documents that answer the most commonly asked questions about Perl and Perl programming. It's divided by topic into nine major sections outlined in this document. -=head2 Where to get the perlfaq +=head2 Where to find the perlfaq -The perlfaq comes with the standard Perl distribution, so if you have Perl -you should have the perlfaq. You should also have the C tool -that lets you read the L: +The perlfaq is an evolving document. Read the latest version at +L. It is also included in the standard Perl +distribution. - $ perldoc perlfaq - -or search the perlfaq question headings: +=head2 How to use the perlfaq - $ perldoc -q open +The C command line tool is part of the standard Perl distribution. To +read the perlfaq: -See L for more information. + $ perldoc perlfaq -Besides your local system, you can find the perlfaq on the web, including -at L. +To search the perlfaq question headings: -The perlfaq is an evolving document and you can read the latest version -at L. + $ perldoc -q open =head2 How to contribute to the perlfaq -Please review L, -if you do not find your suggestion please create an issue or pull request -against L. +Review L. If you don't find +your suggestion create an issue or pull request against +L. -Once approved they will then be merged into -L which is the latest live version that -drives L and will be distributed with the next -release of Perl 5. +Once approved, changes are merged into L, the +repository which drives L, and they are +distributed with the next Perl 5 release. =head2 What if my question isn't answered in the FAQ? -If you have a question that isn't in the FAQ and you would like help with -it, try the resources in L. - -=head1 CREDITS - -Tom Christiansen wrote the original perlfaq then expanded it with the -help of Nat Torkington. brian d foy substantialy edited and expanded -the perlfaq. perlfaq-workers and others have also supplied feedback, -patches and corrections over the years. - -=head1 AUTHOR AND COPYRIGHT - -Tom Christiansen wrote the original version of this document. -brian d foy C<< >> wrote this version. See the -individual perlfaq documents for additional copyright information. - -This document is available under the same terms as Perl itself. Code -examples in all the perlfaq documents are in the public domain. Use -them as you see fit (and at your own risk with no warranty from anyone). +Try the resources in L. -=head1 Table of Contents +=head1 TABLE OF CONTENTS =over 4 -=item perlfaq - this document - =item perlfaq1 - General Questions About Perl =item perlfaq2 - Obtaining and Learning about Perl @@ -87,7 +63,7 @@ them as you see fit (and at your own risk with no warranty from anyone). =back -=head1 The Questions +=head1 THE QUESTIONS =head2 L: General Questions About Perl @@ -1377,7 +1353,7 @@ How do I return the user's mail address? =item * -How do I send mail? +How do I send email? =item * @@ -1385,7 +1361,7 @@ How do I use MIME to make an attachment to a mail message? =item * -How do I read mail? +How do I read email? =item * @@ -1404,3 +1380,22 @@ How do I fetch/put an FTP file? How can I do RPC in Perl? =back + + +=head1 CREDITS + +Tom Christiansen wrote the original perlfaq then expanded it with the +help of Nat Torkington. brian d foy substantialy edited and expanded +the perlfaq. perlfaq-workers and others have also supplied feedback, +patches and corrections over the years. + +=head1 AUTHOR AND COPYRIGHT + +Tom Christiansen wrote the original version of this document. +brian d foy C<< >> wrote this version. See the +individual perlfaq documents for additional copyright information. + +This document is available under the same terms as Perl itself. Code +examples in all the perlfaq documents are in the public domain. Use +them as you see fit (and at your own risk with no warranty from anyone). + diff --git a/cpan/perlfaq/lib/perlfaq2.pod b/cpan/perlfaq/lib/perlfaq2.pod index 88fb1ea..c5186ea 100644 --- a/cpan/perlfaq/lib/perlfaq2.pod +++ b/cpan/perlfaq/lib/perlfaq2.pod @@ -8,9 +8,9 @@ This section of the FAQ answers questions about where to find source and documentation for Perl, support, and related matters. -=head2 What machines support perl? Where do I get it? +=head2 What machines support Perl? Where do I get it? -The standard release of perl (the one maintained by the perl +The standard release of Perl (the one maintained by the Perl development team) is distributed only in source code form. You can find the latest releases at L. @@ -29,7 +29,7 @@ of the particular platform that are not supported in the source release of perl) or negative (e.g. might be based upon a less current source release of perl). -=head2 How can I get a binary version of perl? +=head2 How can I get a binary version of Perl? See L @@ -44,7 +44,7 @@ Otherwise if you really do want to build Perl, you need to get a binary version of C for your system first. Use a search engine to find out how to do this for your operating system. -=head2 I copied the perl binary from one machine to another, but scripts don't work. +=head2 I copied the Perl binary from one machine to another, but scripts don't work. That's probably because you forgot libraries, or library paths differ. You really should build the whole distribution on the machine it will @@ -103,8 +103,6 @@ you should review as a good starting point. =item * L -=item * L - =back The complete Perl documentation is available with the Perl distribution. diff --git a/cpan/perlfaq/lib/perlfaq3.pod b/cpan/perlfaq/lib/perlfaq3.pod index a265acf..01502c8 100644 --- a/cpan/perlfaq/lib/perlfaq3.pod +++ b/cpan/perlfaq/lib/perlfaq3.pod @@ -157,26 +157,6 @@ You can also use L which is an interactive shell for Perl, commonly known as a REPL - Read, Evaluate, Print, Loop. It provides various handy features. - -=head2 Is there a Perl shell? - -The L (Perl sh) is currently at version 1.8. The Perl Shell is a shell -that combines the interactive nature of a Unix shell with the power of -Perl. The goal is a full-featured shell that behaves as expected for -normal shell activity and uses Perl syntax and functionality for -control-flow statements and other things. You can get L at -L . - -L is a similar project and provides a shell written in perl, -configured in perl and operated in perl. It is intended as a login shell -and development environment. It can be found at -L. - -The C module (distributed with Perl) makes Perl try commands -which aren't part of the Perl language as shell commands. C from -the source distribution is simplistic and uninteresting, but may still -be what you want. - =head2 How do I find which modules are installed on my system? From the command line, you can use the C command's C<-l> switch: @@ -303,8 +283,7 @@ similar to the L report: nytprofhtml -CPAN has several other profilers that you can invoke in the same -fashion. You might also be interested in using the L to +You might also be interested in using the L to measure and compare code snippets. You can read more about profiling in I, chapter 20, @@ -456,7 +435,7 @@ Visual Perl is a Visual Studio.NET plug-in from ActiveState. L -Zeus for Windows is another Win32 multi-language editor/IDE +Zeus for Window is another Win32 multi-language editor/IDE that comes with support for Perl. =back diff --git a/cpan/perlfaq/lib/perlfaq5.pod b/cpan/perlfaq/lib/perlfaq5.pod index 325b2cc..751ee46 100644 --- a/cpan/perlfaq/lib/perlfaq5.pod +++ b/cpan/perlfaq/lib/perlfaq5.pod @@ -845,7 +845,7 @@ C. People who installed tcsh as csh won't have this problem, but their users may be surprised by it. To get around this, either upgrade to Perl v5.6.0 or later, do the glob -yourself with readdir() and patterns, or use a module like File::KGlob, +yourself with readdir() and patterns, or use a module like L, one that doesn't use the shell to do globbing. =head2 Is there a leak/bug in glob()? @@ -1023,8 +1023,8 @@ simple as this works: However, if you have fixed sized records, then you might do something more like this: - $RECSIZE = 220; # size of record, in bytes - $recno = 37; # which record to update + my $RECSIZE = 220; # size of record, in bytes + my $recno = 37; # which record to update open my $fh, '+<', 'somewhere' or die "can't update somewhere: $!"; seek $fh, $recno * $RECSIZE, 0; read $fh, $record, $RECSIZE == $RECSIZE or die "can't read record $recno: $!"; @@ -1053,7 +1053,7 @@ Here's an example: my $write_secs = (stat($file))[9]; printf "file %s updated at %s\n", $file, - scalar localtime($write_secs); + scalar localtime($write_secs); If you prefer something more legible, use the File::stat module (part of the standard distribution in version 5.004 and later): @@ -1098,12 +1098,12 @@ the filesystems, not of utime(). X To connect one filehandle to several output filehandles, -you can use the IO::Tee or Tie::FileHandle::Multiplex modules. +you can use the L or L modules. If you only have to do this once, you can print individually to each filehandle. - for my $fh (FH1, FH2, FH3) { print $fh "whatever\n" } + for my $fh ($fh1, $fh2, $fh3) { print $fh "whatever\n" } =head2 How can I read in an entire file all at once? X X @@ -1325,21 +1325,21 @@ X X X X First try - seek(GWFILE, 0, 1); + seek($gw_fh, 0, 1); -The statement C doesn't change the current position, +The statement C doesn't change the current position, but it does clear the end-of-file condition on the handle, so that the -next C<< >> makes Perl try again to read something. +next C<< <$gw_fh> >> makes Perl try again to read something. If that doesn't work (it relies on features of your stdio implementation), then you need something more like this: for (;;) { - for ($curpos = tell(GWFILE); ; $curpos = tell(GWFILE)) { + for ($curpos = tell($gw_fh); <$gw_fh>; $curpos =tell($gw_fh)) { # search for some stuff and put it into files } # sleep for a while - seek(GWFILE, $curpos, 0); # seek to where we had been + seek($gw_fh, $curpos, 0); # seek to where we had been } If this still doesn't work, look into the C method @@ -1355,7 +1355,7 @@ If you check L, you'll see that several of the ways to call open() should do the trick. For example: open my $log, '>>', '/foo/logfile'; - open STDERR, '>&LOG'; + open STDERR, '>&', $log; Or even with a literal numeric descriptor: @@ -1392,7 +1392,7 @@ to, you may be able to do this: Or, just use the fdopen(3S) feature of C: { - open my( $fh ), "<&=$fd" or die "Cannot reopen fd=$fd: $!"; + open my $fh, "<&=$fd" or die "Cannot reopen fd=$fd: $!"; close $fh; } diff --git a/cpan/perlfaq/lib/perlfaq6.pod b/cpan/perlfaq/lib/perlfaq6.pod index e60d1df..4f6c562 100644 --- a/cpan/perlfaq/lib/perlfaq6.pod +++ b/cpan/perlfaq/lib/perlfaq6.pod @@ -95,8 +95,8 @@ got a multiline string in there. For example, this program detects duplicate words, even when they span line breaks (but not paragraph ones). For this example, we don't need C because we aren't using dot in a regular expression that we want -to cross line boundaries. Neither do we need C because we aren't -wanting caret or dollar to match at any point inside the record next +to cross line boundaries. Neither do we need C because we don't +want caret or dollar to match at any point inside the record next to newlines. But it's imperative that $/ be set to something other than the default, or else we won't actually ever have a multiline record read in. @@ -108,7 +108,7 @@ record read in. } } -Here's code that finds sentences that begin with "From " (which would +Here's some code that finds sentences that begin with "From " (which would be mangled by many mailers): $/ = ''; # read in whole paragraph, not just one line @@ -146,8 +146,8 @@ on matching balanced text. Here's another example of using C<..>: while (<>) { - $in_header = 1 .. /^$/; - $in_body = /^$/ .. eof; + my $in_header = 1 .. /^$/; + my $in_body = /^$/ .. eof; # now choose between them } continue { $. = 0 if eof; # fix $. @@ -163,7 +163,7 @@ If you just want to get work done, use a module and forget about the regular expressions. The L and L modules are good starts, although each namespace has other parsing modules specialized for certain tasks and different ways of doing it. Start at -CPAN Search ( L ) and wonder at all the work people +CPAN Search ( L ) and wonder at all the work people have done for you already! :) The problem with things such as XML is that they have balanced text @@ -201,7 +201,7 @@ X<$RS, regexes in> $/ has to be a string. You can use these examples if you really need to do this. -If you have File::Stream, this is easy. +If you have L, this is easy. use File::Stream; @@ -304,9 +304,9 @@ the case of the last character is used for the rest of the substitution. sub preserve_case($$) { my ($old, $new) = @_; - my ($state) = 0; # 0 = no change; 1 = lc; 2 = uc + my $state = 0; # 0 = no change; 1 = lc; 2 = uc my ($i, $oldlen, $newlen, $c) = (0, length($old), length($new)); - my ($len) = $oldlen < $newlen ? $oldlen : $newlen; + my $len = $oldlen < $newlen ? $oldlen : $newlen; for ($i = 0; $i < $len; $i++) { if ($c = substr($old, $i, 1), $c =~ /[\W\d_]/) { @@ -433,7 +433,7 @@ later, you should only see C report that for the first iteration. use re 'debug'; - $regex = 'Perl'; + my $regex = 'Perl'; foreach ( qw(Perl Java Ruby Python) ) { print STDERR "-" x 73, "\n"; print STDERR "Trying $_...\n"; @@ -640,7 +640,7 @@ versions of the same quantifiers, use (C, C<*?>, C<+?>, C<{}?>). An example: - $s1 = $s2 = "I am very very cold"; + my $s1 = my $s2 = "I am very very cold"; $s1 =~ s/ve.*y //; # I am cold $s2 =~ s/ve.*?y //; # I am very cold @@ -656,7 +656,7 @@ X Use the split function: while (<>) { - foreach $word ( split ) { + foreach my $word ( split ) { # do something with $word here } } @@ -680,24 +680,27 @@ pretend that by word you mean chunk of alphabetics, hyphens, or apostrophes, rather than the non-whitespace chunk idea of a word given in the previous question: + my (%seen); while (<>) { while ( /(\b[^\W_\d][\w'-]+\b)/g ) { # misses "`sheep'" $seen{$1}++; } } - while ( ($word, $count) = each %seen ) { + while ( my ($word, $count) = each %seen ) { print "$count $word\n"; } If you wanted to do the same thing for lines, you wouldn't need a regular expression: + my (%seen); + while (<>) { $seen{$_}++; } - while ( ($line, $count) = each %seen ) { + while ( my ($line, $count) = each %seen ) { print "$count $line"; } @@ -707,7 +710,7 @@ sort a hash (optionally by value instead of key)?". =head2 How can I do approximate matching? X X -See the module String::Approx available from CPAN. +See the module L available from CPAN. =head2 How do I efficiently match many regular expressions at once? X X @@ -772,8 +775,8 @@ though. } For more details on regular expression efficiency, see I by Jeffrey Friedl. He explains how regular -expressions engine work and why some patterns are surprisingly +Regular Expressions> by Jeffrey Friedl. He explains how the regular +expressions engine works and why some patterns are surprisingly inefficient. Once you understand how perl applies regular expressions, you can tune them for individual situations. @@ -1022,17 +1025,20 @@ Here are a few ways, all painful, to deal with it: Or like this: - @chars = $martian =~ m/([A-Z][A-Z]|[^A-Z])/g; - # above is conceptually similar to: @chars = $text =~ m/(.)/g; + my @chars = $martian =~ m/([A-Z][A-Z]|[^A-Z])/g; + # above is conceptually similar to: my @chars = $text =~ m/(.)/g; # - foreach $char (@chars) { + foreach my $char (@chars) { print "found GX!\n", last if $char eq 'GX'; } Or like this: while ($martian =~ m/\G([A-Z][A-Z]|.)/gs) { # \G probably unneeded - print "found GX!\n", last if $1 eq 'GX'; + if ($1 eq 'GX') { + print "found GX!\n"; + last; + } } Here's another, slightly less painful, way to do it from Benjamin diff --git a/cpan/perlfaq/lib/perlfaq7.pod b/cpan/perlfaq/lib/perlfaq7.pod index 96c621a..293834c 100644 --- a/cpan/perlfaq/lib/perlfaq7.pod +++ b/cpan/perlfaq/lib/perlfaq7.pod @@ -63,13 +63,13 @@ list. Good style (see L) says to put them in except for one-liners: if ($whoops) { exit 1 } - @nums = (1, 2, 3); + my @nums = (1, 2, 3); if ($whoops) { exit 1; } - @lines = ( + my @lines = ( "There Beren came from mountains cold", "And lost he wandered under leaves", ); @@ -92,12 +92,12 @@ you need: =head2 How do I temporarily block warnings? If you are running Perl 5.6.0 or better, the C pragma -allows fine control of what warning are produced. +allows fine control of what warnings are produced. See L for more details. { no warnings; # temporarily turn off warnings - $a = $b + $c; # I know these might be undef + $x = $y + $z; # I know these might be undef } Additionally, you can enable and disable categories of warnings. @@ -107,7 +107,7 @@ complete details, including the category names and hierarchy. { no warnings 'uninitialized'; - $a = $b + $c; + $x = $y + $z; } If you have an older version of Perl, the C<$^W> variable (documented @@ -115,7 +115,7 @@ in L) controls runtime warnings for a block: { local $^W = 0; # temporarily turn off warnings - $a = $b + $c; # I know these might be undef + $x = $y + $z; # I know these might be undef } Note that like all the punctuation variables, you cannot currently @@ -155,14 +155,14 @@ just such situations as the one above. Another operator with surprising precedence is exponentiation. It binds more tightly even than unary minus, making C<-2**2> produce a -negative not a positive four. It is also right-associating, meaning +negative four and not a positive one. It is also right-associating, meaning that C<2**3**2> is two raised to the ninth power, not eight squared. Although it has the same precedence as in C, Perl's C operator -produces an lvalue. This assigns $x to either $a or $b, depending +produces an lvalue. This assigns $x to either $if_true or $if_false, depending on the trueness of $maybe: - ($maybe ? $a : $b) = $x; + ($maybe ? $if_true : $if_false) = $x; =head2 How do I declare/create a structure? @@ -284,8 +284,8 @@ Here's a classic non-closure function-generating function: return sub { shift() + shift() }; } - $add_sub = add_function_generator(); - $sum = $add_sub->(4,5); # $sum is 9 now. + my $add_sub = add_function_generator(); + my $sum = $add_sub->(4,5); # $sum is 9 now. The anonymous subroutine returned by add_function_generator() isn't technically a closure because it refers to no lexicals outside its own @@ -303,11 +303,11 @@ value that the lexical had when the function was created. return sub { shift() + $addpiece }; } - $f1 = make_adder(20); - $f2 = make_adder(555); + my $f1 = make_adder(20); + my $f2 = make_adder(555); -Now C<&$f1($n)> is always 20 plus whatever $n you pass in, whereas -C<&$f2($n)> is always 555 plus whatever $n you pass in. The $addpiece +Now C<< $f1->($n) >> is always 20 plus whatever $n you pass in, whereas +C<< $f2->($n) >> is always 555 plus whatever $n you pass in. The $addpiece in the closure sticks around. Closures are often used for less esoteric purposes. For example, when @@ -356,9 +356,9 @@ variable's value this way, but now it's much harder. Take this code: print "Finally $f\n"; If you are experiencing variable suicide, that C in the subroutine -doesn't pick up a fresh copy of the C<$f> whose value is . The output -shows that inside the subroutine the value of C<$f> leaks through when it -shouldn't, as in this output: +doesn't pick up a fresh copy of the C<$f> whose value is C<'foo'>. The +output shows that inside the subroutine the value of C<$f> leaks through +when it shouldn't, as in this output: foobar foobarbar @@ -425,7 +425,7 @@ operator: sub compare($$) { my ($val1, $regex) = @_; my $retval = $val1 =~ /$regex/; - return $retval; + return $retval; } $match = compare("old McDonald", qr/d.*D/i); @@ -444,7 +444,7 @@ To pass an object method into a subroutine, you can do this: Or, you can use a closure to bundle up the object, its method call, and arguments: - my $whatnot = sub { $some_obj->obfuscate(@args) }; + my $whatnot = sub { $some_obj->obfuscate(@args) }; func($whatnot); sub func { my $code = shift; @@ -604,10 +604,10 @@ However, dynamic variables (aka global, local, or package variables) are effectively shallowly bound. Consider this just one more reason not to use them. See the answer to L<"What's a closure?">. -=head2 Why doesn't "my($foo) = EFILEE;" work right? +=head2 Why doesn't "my($foo) = E$fh;" work right? C and C give list context to the right hand side -of C<=>. The read operation, like so many of Perl's +of C<=>. The <$fh> read operation, like so many of Perl's functions and operators, can tell which context it was called in and behaves appropriately. In general, the scalar() function can help. This function does nothing to the data itself (contrary to popular myth) @@ -618,15 +618,15 @@ doesn't help you (such as with sort()). To enforce scalar context in this particular case, however, you need merely omit the parentheses: - local($foo) = ; # WRONG - local($foo) = scalar(); # ok - local $foo = ; # right + local($foo) = <$fh>; # WRONG + local($foo) = scalar(<$fh>); # ok + local $foo = <$fh>; # right You should probably be using lexical variables anyway, although the issue is the same here: - my($foo) = ; # WRONG - my $foo = ; # right + my($foo) = <$fh>; # WRONG + my $foo = <$fh>; # right =head2 How do I redefine a builtin function, operator, or method? @@ -861,7 +861,7 @@ You can safely replace C with C (although the converse is not true). The quick-and-dirty way to comment out more than one line of Perl is to surround those lines with Pod directives. You have to put these directives at the beginning of the line and somewhere where Perl -expects a new statement (so not in the middle of statements like the # +expects a new statement (so not in the middle of statements like the C<#> comments). You end the comment with C<=cut>, ending the Pod section: =pod @@ -954,7 +954,7 @@ symbolic references, you are just using the package's symbol-table hash use your own hash or a real reference instead. $USER_VARS{"fred"} = 23; - $varname = "fred"; + my $varname = "fred"; $USER_VARS{$varname}++; # not $$varname++ There we're using the %USER_VARS hash instead of symbolic references. diff --git a/cpan/perlfaq/lib/perlfaq8.pod b/cpan/perlfaq/lib/perlfaq8.pod index 7244725..552e00b 100644 --- a/cpan/perlfaq/lib/perlfaq8.pod +++ b/cpan/perlfaq/lib/perlfaq8.pod @@ -55,6 +55,9 @@ How you access/control keyboards, screens, and pointing devices =item Mouse Tk CPAN + Wx CPAN + Gtk2 CPAN + Qt4 kdebindings4 package =back @@ -115,10 +118,12 @@ to manipulate the POSIX termios structures. # HotKey.pm package HotKey; - @ISA = qw(Exporter); - @EXPORT = qw(cbreak cooked readkey); - use strict; + use warnings; + + use parent 'Exporter'; + our @EXPORT = qw(cbreak cooked readkey); + use POSIX qw(:termios_h); my ($term, $oterm, $echo, $noecho, $fd_stdin); @@ -164,7 +169,7 @@ not to block: ReadMode('cbreak'); - if (defined ($char = ReadKey(-1)) ) { + if (defined (my $char = ReadKey(-1)) ) { # input was waiting and it was $char } else { # no input was waiting @@ -194,8 +199,8 @@ method returns the string for the given capability: use Term::Cap; - $terminal = Term::Cap->Tgetent( { OSPEED => 9600 } ); - $clear_string = $terminal->Tputs('cl'); + my $terminal = Term::Cap->Tgetent( { OSPEED => 9600 } ); + my $clear_string = $terminal->Tputs('cl'); print $clear_screen; @@ -205,7 +210,7 @@ C method: Win32::Console; - $OUT = Win32::Console->new(STD_OUTPUT_HANDLE); + my $OUT = Win32::Console->new(STD_OUTPUT_HANDLE); my $clear_string = $OUT->Cls; print $clear_screen; @@ -214,7 +219,7 @@ If you have a command-line program that does the job, you can call it in backticks to capture whatever it outputs so you can use it later: - $clear_string = `clear`; + my $clear_string = `clear`; print $clear_string; @@ -225,18 +230,18 @@ you can use it to fetch the width and height in characters and in pixels: use Term::ReadKey; - ($wchar, $hchar, $wpixels, $hpixels) = GetTerminalSize(); + my ($wchar, $hchar, $wpixels, $hpixels) = GetTerminalSize(); This is more portable than the raw C, but not as illustrative: require 'sys/ioctl.ph'; die "no TIOCGWINSZ " unless defined &TIOCGWINSZ; - open(TTY, "+; on other systems, device names will doubtless differ. Several problem areas common to all device interaction are the following: @@ -311,19 +316,19 @@ and the C<$|> variable to control autoflushing (see L> and L, or L, "How do I flush/unbuffer an output filehandle? Why must I do this?"): - $oldh = select(DEV); + my $old_handle = select($dev_fh); $| = 1; - select($oldh); + select($old_handle); You'll also see code that does this without a temporary variable, as in - select((select(DEV), $| = 1)[0]); + select((select($deb_handle), $| = 1)[0]); Or if you don't mind pulling in a few thousand lines of code just because you're afraid of a little C<$|> variable: use IO::Handle; - DEV->autoflush(1); + $dev_fh->autoflush(1); As mentioned in the previous item, this still doesn't work when using socket I/O between Unix and Macintosh. You'll need to hard code your @@ -383,7 +388,7 @@ L, for example). There's not a single way to run code in the background so you don't have to wait for it to finish before your program moves on to other tasks. Process management depends on your particular operating system, -and many of the techniques are in L. +and many of the techniques are covered in L. Several CPAN modules may be able to help, including L or L, L, L, @@ -498,7 +503,7 @@ probably get away with setting an environment variable: $ENV{TZ} = "MST7MDT"; # Unixish $ENV{'SYS$TIMEZONE_DIFFERENTIAL'}="-5" # vms - system "trn comp.lang.perl.misc"; + system('trn', 'comp.lang.perl.misc'); =head2 How can I sleep() or alarm() for under a second? X X X X