Skip to content

Instantly share code, notes, and snippets.

@pdl
Created November 11, 2012 17:28
Show Gist options
  • Save pdl/4055604 to your computer and use it in GitHub Desktop.
Save pdl/4055604 to your computer and use it in GitHub Desktop.
Why do overloaded objects bechave differently depending on their construction?
package Acme::Object::Overloaded;
use overload '""' => sub{ 'String' }, '0+' => sub{ 123 }, 'fallback' => '0+';
sub new{
bless {}, shift;
}
1;
package main;
use Test::More;
sub new_object { Acme::Object::Overloaded->new(@_) };
is ((new_object() + 1), 124); # passes
is ((new_object + 1), 124); # fails, got: 'String'
is ((new_object() + 1), 124); # passes
done_testing();
# SOLVED by huf:
# They don't behave differently, the above code contains a subtle bug, as demonstrated below:
#
# > 17:34 < huf> deparse: sub foo {} foo + 1
# > 17:34 < perlbot> huf: foo(1);
#
# i.e. test 2 is being parsed as `is(new_object(+1), 124);`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment