How do I properly use global variables?



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.



#!/usr/bin/perl
use strict;
use warnings;

<snip>

our $status;

sub start{
$status = 1;
while ($status == 1){
print "hello world!\n";
}
print "out of loop. Will exit now";
exit;
}


sub stop {
$status = 0;

}

.



Relevant Pages

  • Re: File::Find beginner question
    ... I am trying to use file::find to generate a list of leaf directories. ... Case matters for Perl modules, so it is better to call it File::Find. ... terminating the outer file::find loop. ... A sub *can* last out of a loop in ...
    (comp.lang.perl.misc)
  • Re: casting question
    ... sub x { ... In a for loop, ... No offense to the Perl gurus here, but Perl is not the easiest ... language for learning OOP. ...
    (comp.lang.perl.misc)
  • Re: killing a sub from within an if statement
    ... I see where you are denying the loop after it's run ... > terminate any objects through normal execution. ... You don't just exit the sub. ...
    (microsoft.public.excel.programming)
  • Re: exit(), die, ??
    ... Don't worry about how you capitalize "PERL", most of us can parse your ... capabilities to exit programs, subroutines, and loops asynchronously. ... to break out of a program, loop, or subroutine without somehow first ...
    (comp.lang.perl.misc)
  • Re: `next LABEL usage
    ... Wants to exit a while loop inside a File::Find \&wanted sub routine. ... Is exiting the while loop sufficient.. ...
    (perl.beginners)