Skip to content
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

Closed
wants to merge 1 commit into from

Conversation

jhthorsen
Copy link
Member

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)".

@jhthorsen jhthorsen self-assigned this Jul 8, 2019
@Grinnz
Copy link
Contributor

Grinnz commented Jul 8, 2019

I would not bother with the overload check. This will miss implied overloads from other operators.

@jhthorsen
Copy link
Member Author

@mojolicious/core: Calling for a vote.

@jhthorsen
Copy link
Member Author

I would not bother with the overload check. This will miss implied overloads from other operators.

That is correct, but I'm guessing it's not a very common case.

@kraih
Copy link
Member

kraih commented Jul 8, 2019

@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';
Copy link
Contributor

@karenetheridge karenetheridge Jul 8, 2019

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'

Copy link
Member

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.

Copy link
Contributor

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, '""');
Copy link
Member

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?

@kraih
Copy link
Member

kraih commented Jul 8, 2019

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants