Subject: | PATCH: Adds overload methods for eq, ne, ==, and != |
The attached patch adds support for comparison operators to IO::Scalar,
so that this code:
Show quoted text
> #!/usr/bin/perl
>
> use strict;
> use IO::Scalar;
> my $a; my $b;
> my $fh_a=new IO::Scalar \$a;
> my $fh_b=new IO::Scalar \$b;
> if ($fh_a eq $fh_b) { print "eq (wrong)\n"; }
> if ($fh_a eq $fh_a) { print "eq (right)\n"; }
> if ($fh_a ne $fh_a) { print "ne (wrong)\n"; }
> if ($fh_a ne $fh_b) { print "ne (right)\n"; }
> if ($fh_a == $fh_b) { print "== (wrong)\n"; }
> if ($fh_a == $fh_a) { print "== (right)\n"; }
> if ($fh_a != $fh_a) { print "!= (wrong)\n"; }
> if ($fh_a != $fh_b) { print "!= (right)\n"; }
will yield this result, when run:
Show quoted text> eq (right)
> ne (right)
> == (right)
> != (right)
I have noted that some perl modules, that use file handles, use these
types of comparison operators to determine if the handles are the same.
Subject: | IO_Scalar.comparison_ops.patch |
--- /usr/share/perl5/IO/Scalar.pm.orig 2008-08-21 14:30:34.000000000 -0400
+++ /usr/share/perl5/IO/Scalar.pm 2008-08-21 14:30:40.000000000 -0400
@@ -150,12 +150,22 @@
use strict;
use vars qw($VERSION @ISA);
use IO::Handle;
+use Scalar::Util;
use 5.005;
### Stringification, courtesy of B. K. Oxley (binkley): :-)
use overload '""' => sub { ${*{$_[0]}->{SR}} };
+use overload 'eq' => sub {
+ return (Scalar::Util::refaddr($_[0]) == Scalar::Util::refaddr($_[1]));
+};
+use overload '==' => overload::Method(__PACKAGE__,'eq');
+use overload 'ne' => sub {
+ return (Scalar::Util::refaddr($_[0]) != Scalar::Util::refaddr($_[1]));
+};
+use overload '!=' => overload::Method(__PACKAGE__,'ne');
use overload 'bool' => sub { 1 }; ### have to do this, so object is true!
+use overload 'fallback' => 0;
### The package version, both in 1.23 style *and* usable by MakeMaker:
$VERSION = "2.110";