Subject: | $uri2 != $uri2 doesn't DWIM |
URI overloads == to do object equality but != is not similarly
overloaded. This arguably should be covered by overload's fallback.
The OCD in me must make it consistent. Patch attached.
Subject: | 0001-Fix-overloading-to-match.patch |
From 65dc30fe197147c157c65f651d9dcc2cedfac674 Mon Sep 17 00:00:00 2001
From: Michael G. Schwern <schwern@pobox.com>
Date: Wed, 7 Oct 2009 20:31:30 -0700
Subject: [PATCH] Fix != overloading to match ==
---
MANIFEST | 1 +
URI.pm | 10 +++++++---
t/num_eq.t | 17 +++++++++++++++++
3 files changed, 25 insertions(+), 3 deletions(-)
create mode 100644 t/num_eq.t
diff --git a/MANIFEST b/MANIFEST
index b0a008b..5f16beb 100644
--- a/MANIFEST
+++ b/MANIFEST
@@ -68,6 +68,7 @@ t/mailto.t
t/mix.t
t/mms.t
t/news.t
+t/num_eq.t
t/old-absconf.t
t/old-base.t
t/old-file.t
diff --git a/URI.pm b/URI.pm
index c8a9274..f76bbb7 100644
--- a/URI.pm
+++ b/URI.pm
@@ -22,12 +22,16 @@ use Carp ();
use URI::Escape ();
use overload ('""' => sub { ${$_[0]} },
- '==' => sub { overload::StrVal($_[0]) eq
- overload::StrVal($_[1])
- },
+ '==' => sub { _obj_eq(@_) },
+ '!=' => sub { !_obj_eq(@_) },
fallback => 1,
);
+# Check if two objects are the same object
+sub _obj_eq {
+ return overload::StrVal($_[0]) eq overload::StrVal($_[1]);
+}
+
sub new
{
my($class, $uri, $scheme) = @_;
diff --git a/t/num_eq.t b/t/num_eq.t
new file mode 100644
index 0000000..2d06120
--- /dev/null
+++ b/t/num_eq.t
@@ -0,0 +1,17 @@
+#!/usr/bin/perl -w
+
+# Test URI's overloading of numeric comparision for checking object
+# equality
+
+use strict;
+use Test::More 'no_plan';
+
+use URI;
+
+my $uri1 = URI->new("http://foo.com");
+my $uri2 = URI->new("http://foo.com");
+
+# cmp_ok() has a bug/misfeature where it strips overloading
+# before doing the comparison. So use a regular ok().
+ok $uri1 == $uri1, "==";
+ok $uri1 != $uri2, "!=";
--
1.6.2.4