Skip to content

Instantly share code, notes, and snippets.

@ynonp
Created October 6, 2015 09:13
Show Gist options
  • Save ynonp/e68e6233020f0605ae98 to your computer and use it in GitHub Desktop.
Save ynonp/e68e6233020f0605ae98 to your computer and use it in GitHub Desktop.
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