Created
November 11, 2012 17:28
-
-
Save pdl/4055604 to your computer and use it in GitHub Desktop.
Why do overloaded objects bechave differently depending on their construction?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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