Skip to content

Instantly share code, notes, and snippets.

@dmanto
Created May 7, 2019 19:38
Show Gist options
  • Save dmanto/209a5908fc728f59dc176643d8c001c1 to your computer and use it in GitHub Desktop.
Save dmanto/209a5908fc728f59dc176643d8c001c1 to your computer and use it in GitHub Desktop.
#
# when you run this code, you don't get a line with "DESTROY called for two"
# as I guess it should be the case
#
# Is this behavior right? How can I force the DESTROY method to be called
# on a "started" object?
{
package MyServer;
use Mojo::Base 'Mojo::EventEmitter';
use Mojo::Util;
use Mojo::IOLoop;
has 'name';
has 'port';
sub start {
my $self = shift;
$self->{_server_id} = Mojo::IOLoop->server(
{address => '127.0.0.1'} => sub { # when some client connects
my ($loop, $stream, $id) = @_;
say "Somebody connected to ", $self->name;
}
);
$self->port(Mojo::IOLoop->acceptor($self->{_server_id})->port);
return $self;
}
sub DESTROY {
return if Mojo::Util::_global_destruction();
say "DESTROY callled for ", shift->name;
}
}
my $s1 = MyServer->new(name => 'one');
my $s2 = MyServer->new(name => 'two')->start;
undef $s1;
undef $s2;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment