Created
October 15, 2011 22:44
-
-
Save worr/1290254 to your computer and use it in GitHub Desktop.
How immutable is immutable?
This file contains hidden or 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
| #!/usr/bin/env perl | |
| use strict; | |
| use warnings; | |
| use v5.10; | |
| use Class::MOP; | |
| my $tc_meta = Class::MOP::Class->initialize("TestClass"); | |
| $tc_meta->make_immutable(inline_constructor => 0); | |
| eval { | |
| $tc_meta->add_method("cm_test", sub { say "Not so immutable" }); | |
| my $test1 = TestClass->new; | |
| $test1->cm_test; | |
| } or warn "Can not add method the Class::MOP way"; | |
| eval { | |
| no strict 'refs'; | |
| *{"TestClass::of_test"} = sub { my $self = shift; say "Not so immutable" }; | |
| my $test2 = TestClass->new; | |
| $test2->of_test; | |
| use strict; | |
| } or warn "Can not add method the old fashioned way"; | |
| package TestClass; | |
| sub new { | |
| return bless({}, shift); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
When doing evals of the form
eval { ... } or warn "blah"shouldn't you have a true value as the last expression in the eval block? Like so:eval { ...; 1; } or warn "blah"