test contents of variable using alarm()

------_=_NextPart_001_01CB4042.789BA502
Content-Type: text/plain;
charset="us-ascii"
Content-Transfer-Encoding: quoted-printable

Hi All,



I want to use timers to check if certain variables are set and if not
send some data back to a client...

Been searching for this a while now, but all I can find on alarm are
examples on timing out commands....



So something like:



sub timeout {

$timeout =3D 5;

eval {

local $SIG{ALRM} =3D sub { die "alarm\n" };

alarm $timeout;

unless $status_ehlo =3D=3D "1";

alarm 0;

};

if ($ [at] ) {

print $client "Timed Out";

die unless $ [at] eq "alarm\n";

} else {

Print $client "Hi!";

}



Thanks!



-

Marco van Kammen
Springer Science+Business Media
System Manager & Postmaster

-

van Godewijckstraat 30 | 3311 GX
Office Number: 05E21
Dordrecht | The Netherlands

-

tel

+31(78)6576446

fax

+31(78)6576302

-

www.springeronline.com <http://www.springeronline.com>
www.springer.com <http://www.springer.com/>

-






------_=_NextPart_001_01CB4042.789BA502--
Marco.vanKammen [ Fr, 20 August 2010 10:34 ] [ ID #2046409 ]

Re: test contents of variable using alarm()

On Fri, Aug 20, 2010 at 04:34, Kammen van, Marco, Springer SBM NL
<Marco.vanKammen [at] springer.com> wrote:
> Hi All,
>
> I want to use timers to check if certain variables are set and if not
> send some data back to a client...
>
> Been searching for this a while now, but all I can find on alarm are
> examples on timing out commands....
snip

All the [alarm][0] function does is send the [ALRM][0] signal to the
current process after X seconds. It is often used to turn a blocking
function into a non-blocking function (i.e. a timeout), but any code
can be put into the signal handler. Here is some code that does
something different with it:

#!/usr/bin/perl

use strict;
use warnings;

use Time::HiRes qw/gettimeofday/;

sub increment_speed {
my $wait = shift;
my $count = 1;
my $continue = 1;
local $SIG{ALRM} = sub { $continue = 0 };
#run this loop for roughly $wait seconds
alarm $wait;
while ($continue) {
$count++;
}
return $count;
}

my $start = gettimeofday;
my $count = increment_speed(5);
my $end = gettimeofday;
my $elapsed = $end - $start;
my $average = $count/$elapsed;

print "$average ($count/$elapsed) average increments per second\n";

[0]: http://perldoc.perl.org/functions/alarm.html
[1]: http://en.wikipedia.org/wiki/SIGALRM

--
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
chas.owens [ Fr, 20 August 2010 16:07 ] [ ID #2046411 ]

RE: test contents of variable using alarm()

LS0tLU9yaWdpbmFsIE1lc3NhZ2UtLS0tLQ0KRnJvbTogQ2hhcy4gT3dlbnMg W21haWx0bzpjaGFz
Lm93ZW5zQGdtYWlsLmNvbV0gDQpTZW50OiBGcmlkYXksIEF1Z3VzdCAyMCwg MjAxMCA0OjA3IFBN
DQpUbzogS2FtbWVuIHZhbiwgTWFyY28sIFNwcmluZ2VyIFNCTSBOTA0KQ2M6 IGJlZ2lubmVyc0Bw
ZXJsLm9yZw0KU3ViamVjdDogUmU6IHRlc3QgY29udGVudHMgb2YgdmFyaWFi bGUgdXNpbmcgYWxh
cm0oKQ0KDQpPbiBGcmksIEF1ZyAyMCwgMjAxMCBhdCAwNDozNCwgS2FtbWVu IHZhbiwgTWFyY28s
IFNwcmluZ2VyIFNCTSBOTA0KPE1hcmNvLnZhbkthbW1lbkBzcHJpbmdlci5j b20+IHdyb3RlOg0K
PiBIaSBBbGwsDQo+DQo+IEkgd2FudCB0byB1c2UgdGltZXJzIHRvIGNoZWNr IGlmIGNlcnRhaW4g
dmFyaWFibGVzIGFyZSBzZXQgYW5kIGlmIG5vdA0KPiBzZW5kIHNvbWUgZGF0 YSBiYWNrIHRvIGEg
Y2xpZW50Li4uDQo+DQo+IEJlZW4gc2VhcmNoaW5nIGZvciB0aGlzIGEgd2hp bGUgbm93LCBidXQg
YWxsIEkgY2FuIGZpbmQgb24gYWxhcm0gYXJlDQo+IGV4YW1wbGVzIG9uIHRp bWluZyBvdXQgY29t
bWFuZHMuLi4uDQpzbmlwDQoNCj4+QWxsIHRoZSBbYWxhcm1dWzBdIGZ1bmN0 aW9uIGRvZXMgaXMg
c2VuZCB0aGUgW0FMUk1dWzBdIHNpZ25hbCB0byB0aGUNCj4+Y3VycmVudCBw cm9jZXNzIGFmdGVy
IFggc2Vjb25kcy4gIEl0IGlzIG9mdGVuIHVzZWQgdG8gdHVybiBhIGJsb2Nr aW5nDQo+PmZ1bmN0
aW9uIGludG8gYSBub24tYmxvY2tpbmcgZnVuY3Rpb24gKGkuZS4gYSB0aW1l b3V0KSwgYnV0IGFu
eSBjb2RlDQo+PmNhbiBiZSBwdXQgaW50byB0aGUgc2lnbmFsIGhhbmRsZXIu ICBIZXJlIGlzIHNv
bWUgY29kZSB0aGF0IGRvZXMNCj4+c29tZXRoaW5nIGRpZmZlcmVudCB3aXRo IGl0Og0KDQpIaSBD
aGFzLA0KDQpJJ20gbG9va2luZyBmb3Igc29tZXRoaW5nIGxpa2UgdGhpcywg SSdtIG5vdCBzdXJl
IGlmIGFsYXJtKCkgaXMgbWFkZSBmb3Igc3VjaCBjaGVja3MgYnV0IEknbSB3 b25kZXJpbmcgaG93
IHRvIGRvIHRoaXMuLi4NCg0KDQpUaW1lciBmb3IgMzAgc2Vjb25kcw0KSWYg KCRkYXRhIGVxICJz
b21ldGhpbmciKSB7DQogIFByaW50ICJPS1xuIjsNCn0gZWxzZSB7DQogIEFm dGVyIDMwIHNlY29u
ZHMNCiAgUHJpbnQgIk5vIGRhdGEgcmVjZWl2ZWQsIHRyeSBhZ2FpbiBsYXRl clxuIjsNCn0NCg0K
TWFyY28hIA0K
Marco.vanKammen [ Mi, 25 August 2010 08:39 ] [ ID #2046684 ]

Re: test contents of variable using alarm()

On Wed, Aug 25, 2010 at 02:39, Kammen van, Marco, Springer SBM NL
<Marco.vanKammen [at] springer.com> wrote:
> ----Original Message-----
> From: Chas. Owens [mailto:chas.owens [at] gmail.com]
> Sent: Friday, August 20, 2010 4:07 PM
> To: Kammen van, Marco, Springer SBM NL
> Cc: beginners [at] perl.org
> Subject: Re: test contents of variable using alarm()
>
> On Fri, Aug 20, 2010 at 04:34, Kammen van, Marco, Springer SBM NL
> <Marco.vanKammen [at] springer.com> wrote:
>> Hi All,
>>
>> I want to use timers to check if certain variables are set and if not
>> send some data back to a client...
>>
>> Been searching for this a while now, but all I can find on alarm are
>> examples on timing out commands....
> snip
>
>>>All the [alarm][0] function does is send the [ALRM][0] signal to the
>>>current process after X seconds. =C2=A0It is often used to turn a blocki=
ng
>>>function into a non-blocking function (i.e. a timeout), but any code
>>>can be put into the signal handler. =C2=A0Here is some code that does
>>>something different with it:
>
> Hi Chas,
>
> I'm looking for something like this, I'm not sure if alarm() is made for =
such checks but I'm wondering how to do this...
>
>
> Timer for 30 seconds
> If ($data eq "something") {
> =C2=A0Print "OK\n";
> } else {
> =C2=A0After 30 seconds
> =C2=A0Print "No data received, try again later\n";
> }
>
> Marco!
>

Given that pseudocode, the else clause will always be run. You have
no code that modifies $data. Since you use the word received, I am
going to assume that you are trying to read from a filehandle (which
may be connected to a socket). If you just want to do nothing for
thirty seconds (i.e. you don't need to interrupt a blocking function
call), then you probably want the [sleep][0] function instead of the
alarm function.

#!/usr/bin/perl

use strict;
use warnings;

use Try::Tiny;

my $wait =3D 2;

my $data;
try {
local $SIG{ALRM} =3D sub { die "timeout" };
alarm $wait;
$data =3D <>;
alarm 0;
} catch {
die $ [at] unless $ [at] eq "timeout";
};

if (defined $data) {
print "OK\n";
} else {
print "No data received, try again later\n";
}

[0]: http://perldoc.perl.org/functions/sleep.html

--
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
chas.owens [ Mi, 25 August 2010 12:49 ] [ ID #2046685 ]
Perl » gmane.comp.lang.perl.beginners » test contents of variable using alarm()

Vorheriges Thema: Perl Recaptcha
Nächstes Thema: Image magick