Permalink
Browse files

allow overloaded objects to be stringified

  • Loading branch information...
1 parent cf88b7b commit a22bc9be39bfe0c818f7e4e74c26715e34a3e0b6 @gray committed Sep 8, 2012
Showing with 16 additions and 3 deletions.
  1. +1 −1 Snappy.xs
  2. +15 −2 t/01_snappy.t
View
2 Snappy.xs
@@ -23,7 +23,7 @@ PREINIT:
uint32_t dest_len;
void *working_memory;
CODE:
- if (SvROK(sv))
+ if (SvROK(sv) && ! SvAMAGIC(sv))
sv = SvRV(sv);
if (! SvOK(sv))
XSRETURN_NO;
View
17 t/01_snappy.t
@@ -17,7 +17,20 @@ for my $len (0 .. 1_024) {
is($decompressed, $in, "length: $len");
}
-my $scalar = '0' x 1_024;
-ok(compress($scalar) eq compress(\$scalar), 'scalar ref');
+{
+ my $scalar = '0' x 1_024;
+ ok compress($scalar) eq compress(\$scalar), 'scalar ref';
+}
+
+{
+ package TrimmedString;
+ sub new { bless(\"$_[1]", $_[0]) }
+ use overload q("") => \&str;
+ sub str { s/^\s+//, s/\s+$// for $_ = "${$_[0]}"; $_ }
+
+ package main;
+ my $scalar = TrimmedString->new(' string ');
+ ok compress($scalar) eq compress('string'), 'blessed scalar ref';
+}
done_testing;

0 comments on commit a22bc9b

Please sign in to comment.