Re: How do I properly use global variables?



Jeff Pang wrote:
On 10/23/07, monk <rsarpi@xxxxxxxxx> wrote:

I'm having problems accessing a variable outside its subroutine.
I've tried several combinations too long to write here. Maybe I just
can't see the forest for the trees. But I'm lost. I need your
wisdom.

I'd like my program below to change $status to zero to exit the loop.

meaning...$> perl test.pl --start
it prints out indefinitely "hello world"

But if $> perl test.pl --stop
it gets out of the loop exiting the program.



And if you want to keep the notation similar to that above you'd need
the second instance of the script (with the --stop), to send the signal
to the process of the first instance (generally by storing the pid in a
file and then sending the signal as below if the pid is still running).

Hi,

When script is running,how can you re-run it with another argument to
make it stop?
The general way to let a running program stop is to send a signal.
Let me modify your code to,

use strict;
use warnings;

our $status = 1;
$SIG{TERM} = $SIG{INT} = sub {$status = 0};

start();

sub start {
while ($status){
print "hello world!\n";
sleep 1;
}

print "out of loop. Will exit now\n";
exit 0;
}

__END__


When you run it,you can send SIGINT or SIGTERM to let it exit gracefully.
Given the process id is 1234,under unix you can say,

$ kill -s 2 1234

The script would print "out of loop. Will exit now" and exit.
(-s 2 means sending SIGINT,see `man 7 signal` for details).

The most important change for the code above is that we re-defined
singal handlers:
$SIG{TERM} = $SIG{INT} = sub {$status = 0};

When the script receive SIGTERM or SIGINT,it set the global $status to
0,so the loop condition become false,the program exit.



.



Relevant Pages

  • Re: Errors when reading a file in Bourne shell -- HELP
    ... I am trying to exit out of the ... the do while and not the shell itself. ... whole shell script. ... to 1 and checking it after the loop. ...
    (comp.unix.questions)
  • Re: trying to understand fork and wait
    ... old habits based on learning to script in REXX on the ... > the child reads it. ... situation for me (drop through to bottom/go back to top of loop). ... just to keep a hold of the exit code. ...
    (comp.lang.perl.misc)
  • Re: Infinite Loops and Explicit Exits
    ... > CS> One of these proposals relaxes the current restriction that an EXIT ... > termination of the loop is not visible at that point. ... > terminating condition is visible in that context. ... > You now want to allow this remote procedure, ...
    (comp.lang.cobol)
  • Time::Local busted?
    ... I have a script that tails a file and on a SIGTERM I want it to exit. ... It traps the signal, executes an 'exit', and then hangs! ... If I comment it out the script exits on SIGTERM and if it leave it in it doesn't. ...
    (comp.lang.perl.misc)
  • Re: How do I properly use global variables?
    ... I'd like my program below to change $status to zero to exit the loop. ... When you run it,you can send SIGINT or SIGTERM to let it exit gracefully. ... The script would print "out of loop. ...
    (perl.beginners)