Created
January 10, 2020 17:28
-
-
Save preaction/278860ea296d0c5a84f46b1e777491e2 to your computer and use it in GitHub Desktop.
Forbid breaking encapsulation of an object
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
package Foo; | |
use overload '%{}' => sub { | |
my ( $package ) = caller; | |
if ( !$package->isa( __PACKAGE__ ) ) { | |
require Carp; | |
Carp::cluck( "Use a method instead!" ); | |
} | |
return $_[0]; | |
}; | |
sub new { return bless { foo => 'bar' }, __PACKAGE__ } | |
sub foo { return shift->{foo} } | |
package main; | |
use v5.16; | |
my $foo = Foo->new; | |
say $foo->foo; | |
say $foo->{foo}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment