Skip to content

Instantly share code, notes, and snippets.

@barefootcoder
Created May 19, 2013 23:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save barefootcoder/5609435 to your computer and use it in GitHub Desktop.
Save barefootcoder/5609435 to your computer and use it in GitHub Desktop.
These are the code snippets I created for my blog post ["Augment and Inner: Haters Gotta Hate"](http://blogs.perl.org/users/buddy_burden/2013/05/augment-and-inner-haters-gotta-hate.html). Since blogs.perl.org always seems to mangle the code formatting (although not right away--it happens slowly, like the code is decaying before your eyes), I'm p…
use 5.12.0;
use warnings;
use MooseX::Declare;
use Method::Signatures::Modifiers;
class My::Command extends MooseX::App::Cmd::Command
{
# common stuff goes here
}
class My::Command::foo extends My::Command
{
method execute ($opt, $args)
{
# perform the duties of "mycommand foo"
}
}
class My::Command::bar extends My::Command
{
method execute ($opt, $args)
{
# perform the duties of "mycommand bar"
}
}
use 5.12.0;
use warnings;
use MooseX::Declare;
use Method::Signatures::Modifiers;
class My::Command extends MooseX::App::Cmd::Command
{
# global options
has yes => (
traits => [qw< Getopt ENV >],
documentation => "Assume 'yes' for all confirmations and default values for all prompts.",
cmd_aliases => 'y',
env_prefix => 'MYCOMMAND',
is => 'ro', isa => 'Bool',
);
method print_cmdline_args ()
{
# print all command line switches,
# including defaults for those not specified
}
method verify_continue ()
{
print "Is this correct? (y/N) ";
exit unless <STDIN> =~ /^y/i;
}
}
class My::Command::foo extends My::Command
{
method execute ($opt, $args)
{
$self->print_cmdline_args;
$self->verify_continue unless $self->yes;
# perform the duties of "mycommand foo"
say "All done!";
}
}
class My::Command::bar extends My::Command
{
method execute ($opt, $args)
{
$self->print_cmdline_args;
$self->verify_continue unless $self->yes;
# perform the duties of "mycommand bar"
say "All done!";
}
}
use 5.12.0;
use warnings;
use MooseX::Declare;
use Method::Signatures::Modifiers;
class My::Command extends MooseX::App::Cmd::Command
{
# global options
has yes => (
traits => [qw< Getopt ENV >],
documentation => "Assume 'yes' for all confirmations and default values for all prompts.",
cmd_aliases => 'y',
env_prefix => 'MYCOMMAND',
is => 'ro', isa => 'Bool',
);
method print_cmdline_args ()
{
# print all command line switches,
# including defaults for those not specified
}
method verify_continue ()
{
print "Is this correct? (y/N) ";
exit unless <STDIN> =~ /^y/i;
}
method cmd_prefix ()
{
$self->print_cmdline_args;
$self->verify_continue unless $self->yes;
}
method cmd_postfix ()
{
say "All done!";
}
}
class My::Command::foo extends My::Command
{
method execute ($opt, $args)
{
$self->cmd_prefix;
# perform the duties of "mycommand foo"
$self->cmd_postfix;
}
}
class My::Command::bar extends My::Command
{
method execute ($opt, $args)
{
$self->cmd_prefix;
# perform the duties of "mycommand bar"
$self->cmd_postfix;
}
}
use 5.12.0;
use warnings;
use MooseX::Declare;
use Method::Signatures::Modifiers;
class My::Command extends MooseX::App::Cmd::Command
{
# global options
has yes => (
traits => [qw< Getopt ENV >],
documentation => "Assume 'yes' for all confirmations and default values for all prompts.",
cmd_aliases => 'y',
env_prefix => 'MYCOMMAND',
is => 'ro', isa => 'Bool',
);
method print_cmdline_args ()
{
# print all command line switches,
# including defaults for those not specified
}
method verify_continue ()
{
print "Is this correct? (y/N) ";
exit unless <STDIN> =~ /^y/i;
}
method execute ($opt, $args)
{
$self->print_cmdline_args;
$self->verify_continue unless $self->yes;
inner();
say "All done!";
}
}
class My::Command::foo extends My::Command
{
augment execute
{
# perform the duties of "mycommand foo"
}
}
class My::Command::bar extends My::Command
{
augment execute
{
# perform the duties of "mycommand bar"
}
}
use 5.12.0;
use warnings;
use MooseX::Declare;
use Method::Signatures::Modifiers;
class baseclass
{
method actions ()
{
my $actions = inner();
return wantarray ? () : undef unless $actions;
return wantarray ? keys %$actions : $actions;
}
}
class subclass1
{
augment actions
{
return { foo => 'do some fooey things', bar => 'belly up to the bar' };
}
}
# etc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment