Skip to content

Instantly share code, notes, and snippets.

@yanick
Created April 8, 2014 16:42
Show Gist options
  • Select an option

  • Save yanick/10153724 to your computer and use it in GitHub Desktop.

Select an option

Save yanick/10153724 to your computer and use it in GitHub Desktop.
automoo
use 5.10.0;
sub create_moo {
my ( $class, $prototype ) = @_;
eval join "\n", "package $class;", 'use Moo;',
map { "has $_ => ( is => 'ro' );" } keys %$prototype;
return $class;
}
my %test = ( a => 1, b => 2, c => 3 );
my $obj = create_moo( 'Stuff', \%test )->new( %test );
say $obj->c;
@jadeallenx
Copy link

I thought about using eval but wondered if directly manipulating a stash would be better.

@yanick
Copy link
Author

yanick commented Apr 8, 2014

I'm trying to remember a way of modifying the current namespace outside of doing eval "package $foo;", and I don't come up with anything...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment