X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=pod%2Fperlfaq8.pod;h=d5c63daa29788fae93da90d53e5c967636af071a;hb=3b910f1cdd4f8ae1cc44d538e0b3182f132b4bf7;hp=8152d49c7e3bc19902d1f51581eb92cb1c9e5881;hpb=7678ccedef3d2583c849cbd8e5a13ba36925ac4c;p=p5sagit%2Fp5-mst-13.2.git diff --git a/pod/perlfaq8.pod b/pod/perlfaq8.pod index 8152d49..d5c63da 100644 --- a/pod/perlfaq8.pod +++ b/pod/perlfaq8.pod @@ -1,6 +1,6 @@ =head1 NAME -perlfaq8 - System Interaction ($Revision: 1.23 $, $Date: 2005/01/03 18:43:37 $) +perlfaq8 - System Interaction ($Revision: 3606 $) =head1 DESCRIPTION @@ -35,25 +35,26 @@ How you access/control keyboards, screens, and pointing devices =item Keyboard - Term::Cap Standard perl distribution - Term::ReadKey CPAN - Term::ReadLine::Gnu CPAN - Term::ReadLine::Perl CPAN - Term::Screen CPAN + Term::Cap Standard perl distribution + Term::ReadKey CPAN + Term::ReadLine::Gnu CPAN + Term::ReadLine::Perl CPAN + Term::Screen CPAN =item Screen - Term::Cap Standard perl distribution - Curses CPAN - Term::ANSIColor CPAN + Term::Cap Standard perl distribution + Curses CPAN + Term::ANSIColor CPAN =item Mouse - Tk CPAN + Tk CPAN =back -Some of these specific cases are shown below. +Some of these specific cases are shown as examples in other answers +in this section of the perlfaq. =head2 How do I print something out in color? @@ -276,8 +277,8 @@ next. If you expect characters to get to your device when you print() them, you'll want to autoflush that filehandle. You can use select() 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?''): +and L, or L, "How do I flush/unbuffer an +output filehandle? Why must I do this?"): $oldh = select(DEV); $| = 1; @@ -411,24 +412,24 @@ You don't actually "trap" a control character. Instead, that character generates a signal which is sent to your terminal's currently foregrounded process group, which you then trap in your process. Signals are documented in L and the -section on ``Signals'' in the Camel. +section on "Signals" in the Camel. -You can set the values of the %SIG hash to be the functions you want +You can set the values of the %SIG hash to be the functions you want to handle the signal. After perl catches the signal, it looks in %SIG for a key with the same name as the signal, then calls the subroutine value for that key. # as an anonymous subroutine - + $SIG{INT} = sub { syswrite(STDERR, "ouch\n", 5 ) }; - + # or a reference to a function - + $SIG{INT} = \&ouch; - + # or the name of the function as a string - - $SIG{INT} = "ouch"; + + $SIG{INT} = "ouch"; Perl versions before 5.8 had in its C source code signal handlers which would catch the signal and possibly run a Perl function that you had set @@ -527,7 +528,7 @@ though, so if you use END blocks you should also use Perl's exception-handling mechanism is its eval() operator. You can use eval() as setjmp and die() as longjmp. For details of this, see the section on signals, especially the time-out handler for a blocking -flock() in L or the section on ``Signals'' in +flock() in L or the section on "Signals" in the Camel Book. If exception handling is all you're interested in, try the @@ -665,8 +666,8 @@ files: use IPC::Open3; use Symbol qw(gensym); use IO::File; - local *CATCHOUT = IO::File->new_tempfile; - local *CATCHERR = IO::File->new_tempfile; + local *CATCHOUT = IO::File->new_tmpfile; + local *CATCHERR = IO::File->new_tmpfile; my $pid = open3(gensym, ">&CATCHOUT", ">&CATCHERR", "cmd"); waitpid($pid, 0); seek $_, 0, 0 for \*CATCHOUT, \*CATCHERR; @@ -679,7 +680,7 @@ should work just as well, without deadlocking: use IPC::Open3; use Symbol qw(gensym); use IO::File; - local *CATCHERR = IO::File->new_tempfile; + local *CATCHERR = IO::File->new_tmpfile; my $pid = open3(gensym, \*CATCHOUT, ">&CATCHERR", "cmd"); while( ) {} waitpid($pid, 0); @@ -1004,20 +1005,26 @@ On POSIX systems, you can test whether your own process group matches the current process group of your controlling terminal as follows: use POSIX qw/getpgrp tcgetpgrp/; - open(TTY, "/dev/tty") or die $!; - $tpgrp = tcgetpgrp(fileno(*TTY)); - $pgrp = getpgrp(); - if ($tpgrp == $pgrp) { - print "foreground\n"; + + # Some POSIX systems, such as Linux, can be + # without a /dev/tty at boot time. + if (!open(TTY, "/dev/tty")) { + print "no tty\n"; } else { - print "background\n"; + $tpgrp = tcgetpgrp(fileno(*TTY)); + $pgrp = getpgrp(); + if ($tpgrp == $pgrp) { + print "foreground\n"; + } else { + print "background\n"; + } } =head2 How do I timeout a slow event? Use the alarm() function, probably in conjunction with a signal handler, as documented in L and the section on -``Signals'' in the Camel. You may instead use the more flexible +"Signals" in the Camel. You may instead use the more flexible Sys::AlarmCall module available from CPAN. The alarm() function is not implemented on all versions of Windows. @@ -1095,12 +1102,12 @@ A quick and dirty fix involves a little bit of code, but this may be all you need to figure out the problem. #!/usr/bin/perl -w - + BEGIN { $SIG{__WARN__} = sub{ print STDERR "Perl: ", @_; }; $SIG{__DIE__} = sub{ print STDERR "Perl: ", @_; exit 1}; } - + $a = 1 + undef; $x / 0; __END__ @@ -1177,8 +1184,8 @@ just need to replace step 3 (B) with B and you will get a new F binary with your extension linked in. See L for more details on building extensions. -See also the next question, ``What's the difference between require -and use?''. +See also the next question, "What's the difference between require +and use?". =head2 What's the difference between require and use? @@ -1250,9 +1257,17 @@ It's a perl4-style file defining values for system networking constants. Sometimes it is built using h2ph when Perl is installed, but other times it is not. Modern programs C instead. +=head1 REVISION + +Revision: $Revision: 3606 $ + +Date: $Date: 2006-03-06 12:05:47 +0100 (lun, 06 mar 2006) $ + +See L for source control details and availability. + =head1 AUTHOR AND COPYRIGHT -Copyright (c) 1997-2005 Tom Christiansen, Nathan Torkington, and +Copyright (c) 1997-2006 Tom Christiansen, Nathan Torkington, and other authors as noted. All rights reserved. This documentation is free; you can redistribute it and/or modify it