Perl Cd Bookshelf [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Perl Cd Bookshelf [Electronic resources] - نسخه متنی

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید



16.16. Temporarily Overriding a Signal Handler


16.16.1. Problem



You
want to install a signal handler only for a particular subroutine.
For instance, your subroutine catches SIGINT, and
you don't want to disturb SIGINT handling outside
the subroutine.

16.16.2. Solution


Use local to
temporarily override a signal's behavior:

# the signal handler
sub ding {
$SIG{INT} = \&ding;
warn "\aEnter your name!\n";
}
# prompt for name, overriding SIGINT
sub get_name {
local $SIG{INT} = \&ding;
my $name;
print "Kindly Stranger, please enter your name: ";
chomp( $name = <> );
return $name;
}

16.16.3. Discussion


You must use local rather than
my to save away one value out of
%SIG. The change remains in effect throughout the
execution of that block, including in anything called from it. In
this case, that's the get_name subroutine. If the
signal is delivered while another function that your function calls
is running, your signal handler is triggered—unless the called
subroutine installs its own signal handler. The previous value of the
hash is automatically restored when the block exits. This is one of
the (few) places where dynamic scoping is more convenient than
confusing.

16.16.4. See Also


Recipe 10.13; Recipe 16.15; Recipe 16.18



16.15. Installing a Signal Handler16.17. Writing a Signal Handler




Copyright © 2003 O'Reilly & Associates. All rights reserved.

/ 875