Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
try to move monkey_patch back to Mojo::Util
  • Loading branch information
kraih committed Nov 6, 2017
1 parent 1733297 commit 832fe2e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 18 deletions.
21 changes: 5 additions & 16 deletions lib/Mojo/Base.pm
Expand Up @@ -8,6 +8,7 @@ use feature ();
# No imports because we get subclassed, a lot!
use Carp ();
use Scalar::Util ();
use Mojo::Util;

# Only Perl 5.14+ requires it on demand
use IO::Handle ();
Expand All @@ -16,21 +17,9 @@ use IO::Handle ();
use constant ROLES =>
!!(eval { require Role::Tiny; Role::Tiny->VERSION('2.000001'); 1 });

# Supported on Perl 5.22+
my $NAME
= eval { require Sub::Util; Sub::Util->can('set_subname') } || sub { $_[1] };

# Protect subclasses using AUTOLOAD
sub DESTROY { }

# Declared here to avoid circular require problems in Mojo::Util
sub _monkey_patch {
my ($class, %patch) = @_;
no strict 'refs';
no warnings 'redefine';
*{"${class}::$_"} = $NAME->("${class}::$_", $patch{$_}) for keys %patch;
}

sub attr {
my ($self, $attrs, $value) = @_;
return unless (my $class = ref $self || $self) && $attrs;
Expand All @@ -43,7 +32,7 @@ sub attr {

# Very performance-sensitive code with lots of micro-optimizations
if (ref $value) {
_monkey_patch $class, $attr, sub {
Mojo::Util::monkey_patch $class, $attr, sub {
return
exists $_[0]{$attr} ? $_[0]{$attr} : ($_[0]{$attr} = $value->($_[0]))
if @_ == 1;
Expand All @@ -52,15 +41,15 @@ sub attr {
};
}
elsif (defined $value) {
_monkey_patch $class, $attr, sub {
Mojo::Util::monkey_patch $class, $attr, sub {
return exists $_[0]{$attr} ? $_[0]{$attr} : ($_[0]{$attr} = $value)
if @_ == 1;
$_[0]{$attr} = $_[1];
$_[0];
};
}
else {
_monkey_patch $class, $attr,
Mojo::Util::monkey_patch $class, $attr,
sub { return $_[0]{$attr} if @_ == 1; $_[0]{$attr} = $_[1]; $_[0] };
}
}
Expand All @@ -87,7 +76,7 @@ sub import {
my $caller = caller;
no strict 'refs';
push @{"${caller}::ISA"}, $flags[0];
_monkey_patch $caller, 'has', sub { attr($caller, @_) };
Mojo::Util::monkey_patch $caller, 'has', sub { attr($caller, @_) };
}

# Mojo modules are strict!
Expand Down
12 changes: 10 additions & 2 deletions lib/Mojo/Util.pm
Expand Up @@ -31,6 +31,10 @@ use constant {
PC_INITIAL_N => 128
};

# Supported on Perl 5.22+
my $NAME
= eval { require Sub::Util; Sub::Util->can('set_subname') } || sub { $_[1] };

# To generate a new HTML entity table run this command
# perl examples/entities.pl
my %ENTITIES;
Expand Down Expand Up @@ -151,8 +155,12 @@ sub getopt {
sub html_attr_unescape { _html(shift, 1) }
sub html_unescape { _html(shift, 0) }

# Declared in Mojo::Base to avoid circular require problems
sub monkey_patch { Mojo::Base::_monkey_patch(@_) }
sub monkey_patch {
my ($class, %patch) = @_;
no strict 'refs';
no warnings 'redefine';
*{"${class}::$_"} = $NAME->("${class}::$_", $patch{$_}) for keys %patch;
}

# Direct translation of RFC 3492
sub punycode_decode {
Expand Down

0 comments on commit 832fe2e

Please sign in to comment.