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
|
package Dancer2::Plugin::Polite;
use strict;
use warnings;
use Dancer2::Plugin;
has smiley => (
is => 'ro',
default => sub {
$_[0]->config->{smiley} || ':-)'
}
);
plugin_keywords 'add_smileys';
sub BUILD {
my $plugin = shift;
$plugin->app->add_hook( Dancer2::Core::Hook->new(
name => 'after',
code => sub { $_[0]->content( $_[0]->content . " ... please?" ) }
));
$plugin->app->add_route(
method => 'get',
regexp => '/goodbye',
code => sub { 'farewell!' },
);
}
sub add_smileys {
my( $plugin, $text ) = @_;
$text =~ s/ (?<= \. ) / $plugin->smiley /xeg;
return $text;
}
1;
|