Using $SIG{"ALRM"} assignment inside a subroutine ...

T

torahul

Hi,

Wondering what's happening here -- is it a bug or an expected behavior
from perl ? I am trying to set SIGALRM handle from within a
subroutine; surprisingly it executes the handler rotine soon I assign
to SIG{"ALRM"}, even I haven't set any alarm! If I move SIG{"ALRM"}
assignment outside the subroutine, it's working as I would expect.

Following perl code :
----------------- Start of Script -----------------
#!/usr/local/bin/perl

$pid = 0;

sub s_alarm_handler {
print ("$$ : *** KILLING process(pid=$pid) after timeout\n");
kill ("KILL", $pid);
}

sub s_sub1 {
local (@args) = @_;

print ("Setting alarm routine to s_alarm_handler\n");
$SIG{"ALRM"} = s_alarm_handler;
print ("Alarm routine set to s_alarm_handler\n");
}

&s_sub1 ("something");
--------------- End of Script ---------------

Running above script generates this output :
---------- Output ---------
Setting alarm routine to s_alarm_handler
18411 : *** KILLING process(pid=0) after timeout
Killed
------ End of Output ------

$ /usr/local/bin/perl -v
This is perl, version 5.004_04 built for sun4-solaris
Copyright 1987-1997, Larry Wall
 
B

Ben Morrow

Quoth (e-mail address removed):
Hi,

Wondering what's happening here -- is it a bug or an expected behavior
from perl ? I am trying to set SIGALRM handle from within a
subroutine; surprisingly it executes the handler rotine soon I assign
to SIG{"ALRM"}, even I haven't set any alarm! If I move SIG{"ALRM"}
assignment outside the subroutine, it's working as I would expect.

No it's not: see below.

[Actually, if you set the sighandler *before* defining the sub and don't
use strict subs, then it will, but don't do that.]
Following perl code :
----------------- Start of Script -----------------
#!/usr/local/bin/perl

use warnings;
use strict;

and fix all errors and warnings.
$pid = 0;

sub s_alarm_handler {

There's absolutely no need for this sort of Hungarian notation. It just
makes things less clear (assuming, of course, that the 's_' stands for
'sub').
print ("$$ : *** KILLING process(pid=$pid) after timeout\n");
kill ("KILL", $pid);
}

sub s_sub1 {
local (@args) = @_;

print ("Setting alarm routine to s_alarm_handler\n");
$SIG{"ALRM"} = s_alarm_handler;

This is wrong; read the line starting 'Be sure not to use...' in the
section for %SIG in perlvar.

Ben
 
T

Tad McClellan

Wondering what's happening here


You are running without "use strict" turned on, so you
deserve any pain that you get.

If you don't like pain, enable strictures on every Perl
program you write.

-- is it a bug or an expected behavior
from perl ?


You told perl to _call_ the subroutine, so it calls the subroutine,
just as expected.

I am trying to set SIGALRM handle from within a
subroutine; surprisingly it executes the handler rotine soon I assign
to SIG{"ALRM"},


$SIG{"ALRM"} = s_alarm_handler;
^^ ^^ where's the quotes


You should put quotes around your strings.

"use strict" would have saved you from yourself here!

&s_sub1 ("something");


s_sub1 ("something");


See perldoc perlsub for why the 2nd way is usually Much Better
than the first way.
 
T

torahul

Thanks Ryan & Tad! Prefixing subroutine handler with \& worked for me.

As you already have imagined, I have just started with Perl! Hence
regarding calling subroutine withour '&' prefix, I wasn't aware of
'this' way of calling subroutine (and hence difference between both!).
The book which I am reading might be either older edition or has not
make these things clear about subroutine -- at least till the point I
have finished reading :).

Usage of 'my' instead of 'local', 'use strict' are also new to me --
and after browsing little thru online perl documentation, is making
things clearer now.

One thing sure for me -- still long way to go to learn perl :)

Regards,
Rahul.
 
B

Brian McCauley

[...] regarding calling subroutine withour '&' prefix, I wasn't aware of
'this' way of calling subroutine (and hence difference between both!).
The book which I am reading might be either older edition or has not
make these things clear about subroutine -- at least till the point I
have finished reading :).

Usage of 'my' instead of 'local', 'use strict' are also new to me --
and after browsing little thru online perl documentation, is making
things clearer now.

Sounds like your book predates Perl5. A lot changed between Perl4 and
Perl5. There is no point trying to learn Perl5 using a book about Perl4.
 
T

torahul

Brian McCauley said:
[...] regarding calling subroutine withour '&' prefix, I wasn't aware of
'this' way of calling subroutine (and hence difference between both!).
The book which I am reading might be either older edition or has not
make these things clear about subroutine -- at least till the point I
have finished reading :).

Usage of 'my' instead of 'local', 'use strict' are also new to me --
and after browsing little thru online perl documentation, is making
things clearer now.

Sounds like your book predates Perl5. A lot changed between Perl4 and
Perl5. There is no point trying to learn Perl5 using a book about Perl4.

You are very correct -- I looked at the Perl version this book uses
and it's 4.036! Anyway, nothing goes waste -- will now be able to
appreciate new features of Perl 5 and later versions, more easily :)

I'm now planning to read 3rd editions of "Learning Perl" by Randal and
then "Programming Perl" by Larry Wall -- any other recommendations ?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top