For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > March 2006 > SIGNALS & OOP









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author SIGNALS & OOP
Tom Allison

2006-03-21, 9:58 pm


I'm trying to refactor an application I wrote a few months ago and ran
into a question about SIG{TERM}.

Currently I have a single application that uses the approach of:

my $please_die =3D 0;
$SIG{TERM} =3D sub {$please_die =3D 1 };

to control when I should exit out of different loops and structures.

But if I have a script that uses objects, how to I propogate this to the
objects from the main script?

The process to 'please_die' would be to exit nicely from each of the
current objects and propogating this all back to the main script and
closing out nicely.

hints?
Mr. Shawn H. Corey

2006-03-21, 9:58 pm

Tom Allison wrote:
> I'm trying to refactor an application I wrote a few months ago and ran
> into a question about SIG{TERM}.
>
> Currently I have a single application that uses the approach of:
>
> my $please_die = 0;
> $SIG{TERM} = sub {$please_die = 1 };
>
> to control when I should exit out of different loops and structures.
>
> But if I have a script that uses objects, how to I propogate this to the
> objects from the main script?
>
> The process to 'please_die' would be to exit nicely from each of the
> current objects and propogating this all back to the main script and
> closing out nicely.
>
> hints?
>


You can access variables in another package by fully qualifying the
name. As in:

return if $main::please_die;

But you would have to make them 'our' variables:

our $please_die = 0;


--

Just my 0.00000002 million dollars worth,
--- Shawn

"For the things we have to learn before we can do them,
we learn by doing them."
Aristotle

"The man who sets out to carry a cat by its tail learns something that
will always be useful and which will never grow dim or doubtful."
Mark Twain

"Believe in the Divine, but paddle away from the rocks."
Hindu Proverb

* Perl tutorials at http://perlmonks.org/?node=Tutorials
* A searchable perldoc is at http://perldoc.perl.org/

John W. Krahn

2006-03-21, 9:58 pm

Mr. Shawn H. Corey wrote:
> Tom Allison wrote:
>
> You can access variables in another package by fully qualifying the
> name. As in:
>
> return if $main::please_die;
>
> But you would have to make them 'our' variables:


If you use the package name then you don't need to use our():

$ perl -Mwarnings -Mstrict -le'$main::var = q[test]; print $main::var'
test



John
--
use Perl;
program
fulfillment
Mr. Shawn H. Corey

2006-03-21, 9:58 pm

John W. Krahn wrote:
> If you use the package name then you don't need to use our():
>
> $ perl -Mwarnings -Mstrict -le'$main::var = q[test]; print $main::var'
> test
>
>
>
> John


True.

But if you don't use 'our' you would always have to use its
fully-qualified name.

$main::please_die = 0;
$SIG{TERM} = sub { $main::please_die = 1; };

instead of:

our $please_die = 0;
$SIG{TERM} = sub { $please_die = 1; };

Of course, you could shorten your keystrokes by using '$::please_die'


--

Just my 0.00000002 million dollars worth,
--- Shawn

"For the things we have to learn before we can do them,
we learn by doing them."
Aristotle

"The man who sets out to carry a cat by its tail learns something that
will always be useful and which will never grow dim or doubtful."
Mark Twain

"Believe in the Divine, but paddle away from the rocks."
Hindu Proverb

* Perl tutorials at http://perlmonks.org/?node=Tutorials
* A searchable perldoc is at http://perldoc.perl.org/

Jeff Pang

2006-03-21, 9:58 pm

when I should exit out of different loops and structures.
>
>But if I have a script that uses objects, how to I propogate this to the
>objects from the main script?
>


I would suggest you read this article " Using Global Variables and Sharing Them Between Modules/Packages" writen by Stas Bekman:

http://www.perl.com/pub/a/2002/04/2...erl.html?page=1

--
Jeff Pang
NetEase AntiSpam Team
http://corp.netease.com
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com