Subject: | stringification is overloaded but comparison is not. |
When a Text::Balanced function generates an error and I compare $@ to ''
afterwards I get the following error (see attached example program:
Operation "ne": no method found,
left argument in overloaded package Text::Balanced::ErrorMsg,
right argument has no overloaded magic at /tmp/autoload line 7.
This is obviously because cmp is not overloaded. Since "" is overloaded I
think cmp should be overloaded too. The attached patch implements this.
Subject: | example_program_cmp_issue.pl |
use Text::Balanced 'extract_delimited';
use strict;
use warnings;
my $text = 'while($a == "test"){ print "true";}';
my ($extracted, $reminder) = extract_delimited($text, '#' );
print $@ if '' ne $@;
Subject: | overload_cmp.patch |
diff --git a/lib/Text/Balanced.pm b/lib/Text/Balanced.pm
index 07d9567..dff2c6a 100644
--- a/lib/Text/Balanced.pm
+++ b/lib/Text/Balanced.pm
@@ -1019,6 +1019,7 @@ sub extract($$) # ($self, $text)
package Text::Balanced::ErrorMsg;
use overload '""' => sub { "$_[0]->{error}, detected at offset $_[0]->{pos}" };
+use overload 'cmp' => sub { $_[0]->{error} cmp $_[1] };
1;