Skip to content

Instantly share code, notes, and snippets.

@j1n3l0
Created May 29, 2010 12:10
Show Gist options
  • Save j1n3l0/418248 to your computer and use it in GitHub Desktop.
Save j1n3l0/418248 to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl
use Modern::Perl;
use Test::Most;
{
package Some::Role;
use Moose::Role;
use MooseX::Types::Moose qw( Str );
has attribute => ( is => 'ro', isa => Str, default => 'default' );
no Moose::Role;
}
{
package Some::Class::With::Override;
use Moose;
with qw( Some::Role MooseX::Getopt );
has '+attribute' => ( traits => ['NoGetopt'] );
no Moose;
}
{
package Some::Class::No::Override;
use Moose;
with qw( Some::Role MooseX::Getopt );
no Moose;
}
{
local @ARGV = ();
ok my $object = Some::Class::With::Override->new_with_options,
'can create object without attribute';
is $object->attribute, 'default', '... and has default value';
}
{
local @ARGV = qw( --attribute test );
dies_ok { Some::Class::With::Override->new_with_options }
'cannot create object with option "attribute"';
}
{
local @ARGV = qw( --attribute value );
ok my $object = Some::Class::No::Override->new_with_options,
'can create object with attribute';
is $object->attribute, 'value', '... and has provided value';
}
done_testing;
@j1n3l0
Copy link
Author

j1n3l0 commented May 29, 2010

Some times you want to consume a role into a command line application but you don't want it's attributes to show up on as command line options. This is a simple way to override the default traits of the attribute ... although I'm not sure if this will update or override the attributes original traits ;)

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