23. package MyApache2::Sinatratic;
use strict;
use warnings;
# $CALLBACK->{$handler_package}->{$http_method} = [ [$url, $handler], ... ];
my $CALLBACK = {};
sub import {
my $pkg = shift;
my @args = @_;
my $callpkg = caller(0);
for my $method (qw(get post put del)) {
$CALLBACK->{$callpkg}->{$method} = [];
}
# sub handler definition
require Apache2::RequestRec;
require Apache2::RequestUtil;
require APR::Table;
no strict 'refs';
*{"$callpkg::handler"} = &import_handler;
for my $method (qw(get post put del)) {
*{"$callpkg::$method"} = sub {
my ($url, $handler) = @_;
push @{$CALLBACK->{$callpkg}->{$method}}, [$url, $handler];
};
}
*{"$callpkg::default"} = sub {
my $handler = shift;
$CALLBACK->{$callpkg} ||= {};
$CALLBACK->{$callpkg}->{default} = $handler;
};
}
26. package MyApache2::Hello2;
use strict;
use warnings;
use Apache2::RequestRec;
use Apache2::RequestIO;
use Apache2::Const -compile => qw(OK);
use MyApache2::Sinatratic;
get '/' => sub {
my $r = shift;
$r->content_type("text/plain");
$r->print("Hello, world");
return Apache2::Const::OK;
};
1;
__END__
<Location />
PerlResponseHandler MyApache2::Hello2
</Location>