in reply to Re^2: Any one know how to use sigtrap with moose?
in thread Any one know how to use sigtrap with moose?

You could try something along the lines of:

use MyPackage; my $newest_instance; after BUILD => sub { $newest_instance = shift; };

And then in your signal handler, call:

if ($newest_instance) { $newest_instance->status("killed"); $newest_instance->end; }

Note that if there is more than one object of class MyPackage knocking around, this will only set the status on the newest (i.e. most recently constructed) object.

Depending on exactly what you're doing, and how many instances of MyPackage you expect to exist in the lifetime of a process, you may need something else.

perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

Replies are listed 'Best First'.
Re^4: Any one know how to use sigtrap with moose?
by Corion (Patriarch) on Jul 24, 2012 at 11:47 UTC

    This approach will also keep $newest_instance around until the end of the program, which may or may not be what the OP intended.

      Indeed, but there's always Scalar::Util::weaken for that.

      perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
Re^4: Any one know how to use sigtrap with moose?
by hisycg@gmail.com (Initiate) on Jul 24, 2012 at 12:23 UTC
    Very smart! That's exactly what I'm looking for! Thanks! But the "after BUILD" part does not work, it complains about

    The method 'BUILD' was not found in the inheritance hierarchy for Mypackage at /usr/lib/perl5/Class/MOP/Class.pm line 1053

    So I changed to :
    sub BUILD{ $newest_instance = shift; }
    Then it works. may be I miss some perl modules...