Created
October 6, 2015 09:13
-
-
Save ynonp/e68e6233020f0605ae98 to your computer and use it in GitHub Desktop.
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
use strict; | |
use warnings; | |
use v5.20; | |
package ImmutableString { | |
use Hash::Util qw/lock_hash/; | |
sub new { | |
my ($cls, $val) = @_; | |
my $self = { | |
val => $val, | |
}; | |
bless $self, $cls; | |
lock_hash(%$self); | |
} | |
sub get { shift->{val} } | |
} | |
my $s = ImmutableString->new('hello'); | |
# fail | |
$s->{val} = 'foo'; | |
# fail | |
substr($s->{val}, 0, 1, 'H'); | |
# print "hello" | |
say $s->get; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment