public
Created

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 putting it here to aid folks trying to understand how the code flows.

  • Download Gist
augment_inner.pl
Perl
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
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"
}
}
augment_inner2.pl
Perl
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
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!";
}
}
augment_inner3.pl
Perl
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
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;
}
}
augment_inner4.pl
Perl
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
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"
}
}
augment_inner5.pl
Perl
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.