cvs commit: perlfaq perlfaq8.pod

[email protected] (brian d foy) 5 May 2004 06:29:12 -0000
Newsgroups perl.cvs.perlfaq
Message-ID <[email protected]>
cvsuser     04/05/04 23:29:12

  Modified:    .        perlfaq8.pod
  Log:
  * How do I trap control characters/signals?
  	+ I got rid of all of the discussion about C signal
  	handlers and simply talked about Perl.
  	+ The old answer said you could set a variable in
  	a signal handler, which is false
  
  Revision  Changes    Path
  1.21      +24 -25    perlfaq/perlfaq8.pod
  
  Index: perlfaq8.pod
  ===================================================================
  RCS file: /cvs/public/perlfaq/perlfaq8.pod,v
  retrieving revision 1.20
  retrieving revision 1.21
  diff -u -w -r1.20 -r1.21
  --- perlfaq8.pod	7 Feb 2004 04:29:50 -0000	1.20
  +++ perlfaq8.pod	5 May 2004 06:29:12 -0000	1.21
  @@ -1,6 +1,6 @@
   =head1 NAME
   
  -perlfaq8 - System Interaction ($Revision: 1.20 $, $Date: 2004/02/07 04:29:50 $)
  +perlfaq8 - System Interaction ($Revision: 1.21 $, $Date: 2004/05/05 06:29:12 $)
   
   =head1 DESCRIPTION
   
  @@ -413,31 +413,30 @@
   Signals are documented in L<perlipc/"Signals"> and the
   section on ``Signals'' in the Camel.
   
  -Be warned that very few C libraries are re-entrant.  Therefore, if you
  -attempt to print() in a handler that got invoked during another stdio
  -operation your internal structures will likely be in an
  -inconsistent state, and your program will dump core.  You can
  -sometimes avoid this by using syswrite() instead of print().
  -
  -Unless you're exceedingly careful, the only safe things to do inside a
  -signal handler are (1) set a variable and (2) exit.  In the first case,
  -you should only set a variable in such a way that malloc() is not
  -called (eg, by setting a variable that already has a value).
  -
  -For example:
  -
  -    $Interrupted = 0;	# to ensure it has a value
  -    $SIG{INT} = sub {
  -        $Interrupted++;
  -	syswrite(STDERR, "ouch\n", 5);
  -    }
  +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"; 
  +
  +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
  +in %SIG.  This violated the rules of signal handling at that level
  +causing perl to dump core. Since version 5.8.0, perl looks at %SIG
  +*after* the signal has been caught, rather than while it is being caught.
  +Previous versions of this answer were incorrect.
   
  -However, because syscalls restart by default, you'll find that if
  -you're in a "slow" call, such as <FH>, read(), connect(), or
  -wait(), that the only way to terminate them is by "longjumping" out;
  -that is, by raising an exception.  See the time-out handler for a
  -blocking flock() in L<perlipc/"Signals"> or the section on ``Signals''
  -in the Camel book.
   
   =head2 How do I modify the shadow password file on a Unix system?