Skip to content

Instantly share code, notes, and snippets.

@Util
Created April 6, 2012 14:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Util/2320105 to your computer and use it in GitHub Desktop.
Save Util/2320105 to your computer and use it in GitHub Desktop.
Example of Moose classes in the same single script as the mainline code.
# Yes, you can put Moose classes in the same source file as your mainline code, rather than doing the CPAN-standard of one class per module.
# However, you must place the class definitions above the uses of the classes.
package Point;
use Moose;
has 'x' => ( isa => 'Int', is => 'rw' );
has 'y' => ( isa => 'Int', is => 'rw' );
__PACKAGE__->meta->make_immutable;
package Point3d;
use Moose;
extends 'Point';
has 'z' => ( isa => 'Int', is => 'rw' );
__PACKAGE__->meta->make_immutable;
# This part would not work right if it were moved to the top of the program.
package main;
my @threeD_points = map { Point3d->new( x => $_->[0], y => $_->[1], z => $_->[2], ) } (
[ 2, 5, 9 ],
[ 8, 1, 2 ],
);
print $_->dump for @threeD_points;
__END__
output is:
$VAR1 = bless( {
'y' => 5,
'x' => 2,
'z' => 9
}, 'Point3d' );
$VAR1 = bless( {
'y' => 1,
'x' => 8,
'z' => 2
}, 'Point3d' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment