Skip to content

Instantly share code, notes, and snippets.

@preaction
Created December 17, 2014 06:42
Show Gist options
  • Save preaction/6eb6b369d0cbf3521384 to your computer and use it in GitHub Desktop.
Save preaction/6eb6b369d0cbf3521384 to your computer and use it in GitHub Desktop.
use v5.20;
use Class::Anonymous;
my %hash = (
foo => 'bar',
baz => 'fuzz',
);
my $obj = make_readonly_obj( %hash );
say $obj->foo;
say $obj->baz;
sub make_readonly_obj {
my ( %hash ) = @_;
my $class = class {
my ( $self ) = @_;
for my $key ( keys %hash ) {
$self->( $key, sub { $hash{$key} } );
}
};
return $class->new;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment