in reply to Re^2: Demonize Module, What are your thoughts?
in thread Demonize Module, What are your thoughts?

I haven't read through the rewrite.

What I meant about passing a code reference is using an anonymous sub.

# create code reference my $anon_sub = sub { print "Hello world!\n" }; # execute the code $anon_sub->();

For Daemonize, it would look something like...

my $run = sub { die 'unfinished' }; my $d = Daemonize->new( run => $run ); # later, inside Daemonize $self->{run}->( $self );

I think it would be a good idea to pass the Daemonize object to the sub itself (as I did in the example) so it can call methods on it (like logging) if it wants to.

I'm not quite clear about what you're trying to do with $SIG{__DIE__}, but you might want an END block or a DESTROY method instead.

Replies are listed 'Best First'.
Re^4: Demonize Module, What are your thoughts?
by halfbaked (Sexton) on Dec 17, 2008 at 18:18 UTC
    Thanks that anonymous sub makes sense.

    What I'm trying to do with $SIG{__DIE__} is to run some code when the daemon dies to let me know it died. I just want to know if the daemon dies unexpectedly (perhaps Demonize isn't the best place to put this functionality).

      The first thing to realize is that whatever you do won't be reliable. If you receive a signal that can't be caught, you won't have the chance to take any action about it.

      That said, $SIG{__DIE__} should work fine. Just be sure to check $^S before you do anything rash, and test your handler when it's done because I'm not really sure this does what you want.