-
Notifications
You must be signed in to change notification settings - Fork 577
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Can string match on object as well #1377
Conversation
I would not bother with the overload check. This will miss implied overloads from other operators. |
abc11dc
to
ef5db2a
Compare
@mojolicious/core: Calling for a vote. |
That is correct, but I'm guessing it's not a very common case. |
@Grinnz raises a good point. I keep going back and forth, but am really not sure which is better. |
@@ -26,15 +26,16 @@ sub check { | |||
|
|||
my ($default, $handler); | |||
my $is_obj = blessed $err; | |||
my $can_re = !$is_obj || $is_obj && overload::Method($err, '""'); | |||
CHECK: for (my $i = 0; $i < @spec; $i += 2) { | |||
my ($checks, $cb) = @spec[$i, $i + 1]; | |||
|
|||
($default = $cb) and next if $checks eq 'default'; | |||
|
|||
for my $c (ref $checks eq 'ARRAY' ? @$checks : $checks) { | |||
my $is_re = ref $c eq 'Regexp'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The proper way to check for regexes, that will work on all perl versions, is re->can('is_regexp') ? re::is_regexp($c) : ref($c) eq 'Regexp'
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't have to support all Perl versions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
re::is_regexp became available in perl 5.9.5, so that would be preferrable to checking ref eq 'Regexp' in all cases then.
@@ -26,15 +26,16 @@ sub check { | |||
|
|||
my ($default, $handler); | |||
my $is_obj = blessed $err; | |||
my $can_re = !$is_obj || $is_obj && overload::Method($err, '""'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't that $is_obj &&
redundant?
I know a few people from the core team have argued about this, and it seems nobody is comfortable voting on it at all. To me that's a vote against and i consider this proposal failed. We'll do it without the overload check. |
Summary
This change allow
check()
in Mojo::Exception to run regex checks on objects that can stringify.Motivation
@kraih asked for it in #mojo on freenode, and I think it makes a lot of sense to be able check if for example a Mojo::Exception object match certain regexes, since it's a very generic exception object.
I chose to check if the object is string overloaded, so we don't match against strings that look like "Foo::Bar=HASH(0x7f8ab5804248)".