summaryrefslogtreecommitdiff
path: root/lib/overloading.t
diff options
context:
space:
mode:
authorYuval Kogman <nothingmuch@woobling.org>2008-08-09 16:01:15 +0300
committerRafael Garcia-Suarez <rgarciasuarez@gmail.com>2009-01-02 09:52:55 +0100
commite46c382ee1a26c0abddc80ad1249dc544d229d4e (patch)
treed890a3e8023dbe3fbb3b35fe4f6b85efe8002a3f /lib/overloading.t
parent50eca76146e11e9c375c0a5c02f5f2102f0911bc (diff)
downloadperl-e46c382ee1a26c0abddc80ad1249dc544d229d4e.tar.gz
'overloading' pragma
Diffstat (limited to 'lib/overloading.t')
-rw-r--r--lib/overloading.t86
1 files changed, 86 insertions, 0 deletions
diff --git a/lib/overloading.t b/lib/overloading.t
new file mode 100644
index 0000000000..8121cc8b41
--- /dev/null
+++ b/lib/overloading.t
@@ -0,0 +1,86 @@
+#./perl
+
+BEGIN {
+ chdir 't' if -d 't';
+ @INC = '../lib';
+}
+
+BEGIN {
+ require "./test.pl";
+ plan(tests => 22);
+}
+
+use Scalar::Util qw(refaddr);
+
+{
+ package Stringifies;
+
+ use overload (
+ fallback => 1,
+ '""' => sub { "foo" },
+ '0+' => sub { 42 },
+ );
+
+ sub new { bless {}, shift };
+}
+
+my $x = Stringifies->new;
+
+is( "$x", "foo", "stringifies" );
+is( 0 + $x, 42, "numifies" );
+
+{
+ no overloading;
+ is( "$x", overload::StrVal($x), "no stringification" );
+ is( 0 + $x, refaddr($x), "no numification" );
+
+ {
+ no overloading '""';
+ is( "$x", overload::StrVal($x), "no stringification" );
+ is( 0 + $x, refaddr($x), "no numification" );
+ }
+}
+
+{
+ no overloading '""';
+
+ is( "$x", overload::StrVal($x), "no stringification" );
+ is( 0 + $x, 42, "numifies" );
+
+ {
+ no overloading;
+ is( "$x", overload::StrVal($x), "no stringification" );
+ is( 0 + $x, refaddr($x), "no numification" );
+ }
+
+ use overloading '""';
+
+ is( "$x", "foo", "stringifies" );
+ is( 0 + $x, 42, "numifies" );
+
+ no overloading '0+';
+ is( "$x", "foo", "stringifies" );
+ is( 0 + $x, refaddr($x), "no numification" );
+
+ {
+ no overloading '""';
+ is( "$x", overload::StrVal($x), "no stringification" );
+ is( 0 + $x, refaddr($x), "no numification" );
+
+ {
+ use overloading;
+ is( "$x", "foo", "stringifies" );
+ is( 0 + $x, 42, "numifies" );
+ }
+ }
+
+ is( "$x", "foo", "stringifies" );
+ is( 0 + $x, refaddr($x), "no numification" );
+
+
+ BEGIN { ok(exists($^H{overloading}), "overloading hint present") }
+
+ use overloading;
+
+ BEGIN { ok(!exists($^H{overloading}), "overloading hint removed") }
+}