Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
use more Perl 5.10 features
  • Loading branch information
kraih committed Oct 1, 2011
1 parent 998a12d commit e52714c
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 19 deletions.
1 change: 1 addition & 0 deletions Changes
Expand Up @@ -5,6 +5,7 @@ This file documents the revision history for Perl extension Mojolicious.
- Increased Perl version requirement to 5.10.1.
- Disabled some more tests on Windows with older Perl versions.
- Improved message parser performance slightly.
- Improved resolver tests.
- Fixed small formatting bug in Mojo::Headers.

1.99 2011-09-29 00:00:00
Expand Down
18 changes: 4 additions & 14 deletions lib/Mojo/IOLoop.pm
Expand Up @@ -34,23 +34,13 @@ has timeout => '0.025';
# Ignore PIPE signal
$SIG{PIPE} = 'IGNORE';

# Singleton
our $LOOP;

sub new {
my $class = shift;

# Build new loop from singleton and inherit watcher
my $loop = $LOOP;
local $LOOP = undef;
my $self;
if ($loop) {
$self = $loop->new(@_);
$self->iowatcher($loop->iowatcher->new);
}

# Start from scratch
else { $self = $class->SUPER::new(@_) }
my $loop = $class->singleton;
my $self = $loop->SUPER::new(@_);
$self->iowatcher($loop->iowatcher->new);

return $self;
}
Expand Down Expand Up @@ -288,7 +278,7 @@ sub remote_info {
return {address => $handle->peerhost, port => $handle->peerport};
}

sub singleton { $LOOP ||= shift->new(@_) }
sub singleton { state $loop ||= shift->SUPER::new }

sub start {
my $self = shift;
Expand Down
6 changes: 3 additions & 3 deletions lib/Mojo/IOWatcher/EV.pm
Expand Up @@ -4,12 +4,12 @@ use Mojo::Base 'Mojo::IOWatcher';
use EV;
use Scalar::Util 'weaken';

my $SINGLETON;
my $EV;

sub DESTROY { undef $SINGLETON }
sub DESTROY { undef $EV }

# We have to fall back to Mojo::IOWatcher, since EV is unique
sub new { $SINGLETON++ ? Mojo::IOWatcher->new : shift->SUPER::new }
sub new { $EV++ ? Mojo::IOWatcher->new : shift->SUPER::new }

sub not_writing {
my ($self, $handle) = @_;
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojolicious/Guides/Cookbook.pod
Expand Up @@ -274,7 +274,7 @@ implementation L<Mojo::JSON> built right in.
my $user = $tweet->{from_user};

# Show both
my $result = "$text --$user\n\n";
my $result = "$text --$user";
utf8::encode $result;
say $result;
}
Expand Down
25 changes: 24 additions & 1 deletion t/mojo/resolver.t
Expand Up @@ -7,11 +7,12 @@ BEGIN {
$ENV{MOJO_IOWATCHER} = 'Mojo::IOWatcher';
}

use Test::More tests => 16;
use Test::More tests => 30;

# "Oh, I'm in no condition to drive. Wait a minute.
# I don't have to listen to myself. I'm drunk."
use_ok 'Mojo::IOLoop';
use_ok 'Mojo::IOLoop::Resolver';

my $r = Mojo::IOLoop->singleton->resolver;

Expand All @@ -31,6 +32,28 @@ is $r->is_ipv6('foo.1.1.1.1.de'), undef, 'not an IPv4 address';
is $r->is_ipv4('1.1.1.1.1.1'), undef, 'not an IPv4 address';
is $r->is_ipv6('1.1.1.1.1.1'), undef, 'not an IPv4 address';

# Shared ioloop
my $r2 = Mojo::IOLoop::Resolver->new;
is $r->ioloop, $r2->ioloop, 'same ioloop';

# Shared server pool
$r->servers('8.8.8.8', '1.2.3.4');
is_deeply [$r->servers], ['8.8.8.8', '1.2.3.4'], 'right servers';
is scalar $r->servers, '8.8.8.8', 'right server';
$r2->servers('8.8.8.8', '1.2.3.4');
is_deeply [$r2->servers], ['8.8.8.8', '1.2.3.4'], 'right servers';
is scalar $r2->servers, '8.8.8.8', 'right server';
$r->servers('1.2.3.4');
is_deeply [$r->servers], ['1.2.3.4'], 'right servers';
is scalar $r->servers, '1.2.3.4', 'right server';
is_deeply [$r2->servers], ['1.2.3.4'], 'right servers';
is scalar $r2->servers, '1.2.3.4', 'right server';
$r->servers('1.2.3.4', '4.3.2.1');
is_deeply [$r->servers], ['1.2.3.4', '4.3.2.1'], 'right servers';
is scalar $r->servers, '1.2.3.4', 'right server';
is_deeply [$r2->servers], ['1.2.3.4', '4.3.2.1'], 'right servers';
is scalar $r2->servers, '1.2.3.4', 'right server';

# Lookup "localhost" (pass through)
my $result;
$r->lookup(
Expand Down

2 comments on commit e52714c

@29x10
Copy link

@29x10 29x10 commented on e52714c Jan 7, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the difference between shift->new(@_) and shift->SUPER::new in Mojo::IOLoop

@zoffixznet
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@secjex you should use the appropriate community support channels, such as the mailing list or the IRC channel (#mojo on irc.perl.org) to ask these questions, rather than commenting on old commits and alerting the entire core dev team with questions that can be answered by the community instead.

Please sign in to comment.