Weitere ähnliche Inhalte Ähnlich wie コードの動的生成のお話 (20) Kürzlich hochgeladen (20) コードの動的生成のお話2. • OGATA Tetsuji a.k.a. @xtetsuji
• http://post.tetsuji.jp/
• RND INFR
• Perl 🐫/ Debian 🌀 / Postfix MTA 📩 / Mathematics 🔢 /
Classical Music 🎵 / Cafe ☕ / Route Bus 🚌
7. sub AUTOLOAD {
my ( $self, $args ) = @_;
our $AUTOLOAD;
my $class = ref $self; # PROJECT::Model::FUNC
my $method = $AUTOLOAD; # PROJECT::Model::FUNC::hoge
$method =~ s/$class:://;
$class =~ s/^PROJECT::Model:://;
return $self->mysql->{ $class }->$method( $args );
}
11. ### From CGI package version 2.99
sub AUTOLOAD {
print STDERR "CGI::AUTOLOAD for $AUTOLOADn" if $CGI::AUTOLOAD_DEBUG;
my $func = &_compile;
goto &$func;
}
13. sub _compile {
my($func) = $AUTOLOAD;
my($pack,$func_name);
{
local($1,$2); # this fixes an obscure variable suicide problem.
$func=~/(.+)::([^:]+)$/;
($pack,$func_name) = ($1,$2);
$pack=~s/::SUPER$//; # fix another obscure problem
$pack = ${"$pack::AutoloadClass"} || $CGI::DefaultClass
unless defined(${"$pack::AUTOLOADED_ROUTINES"});
my($sub) = %{"$pack::SUBS"};
unless (%$sub) {
my($auto) = ${"$pack::AUTOLOADED_ROUTINES"};
eval "package $pack; $$auto";
croak("$AUTOLOAD: $@") if $@;
$$auto = ''; # Free the unneeded storage (but don't undef it!!!)
}
my($code) = $sub->{$func_name};
$code = "sub $AUTOLOAD { }" if (!$code and $func_name eq 'DESTROY');
if (!$code) {
(my $base = $func_name) =~ s/^(start_|end_)//i;
if ($EXPORT{':any'} ||
$EXPORT{'-any'} ||
$EXPORT{$base} ||
(%EXPORT_OK || grep(++$EXPORT_OK{$_},&expand_tags(':html')))
&& $EXPORT_OK{$base}) {
$code = $CGI::DefaultClass->_make_tag_func($func_name);
}
}
croak("Undefined subroutine $AUTOLOADn") unless $code;
eval "package $pack; $code";
if ($@) {
$@ =~ s/ at .*n//;
croak("$AUTOLOAD: $@");
}
}
CORE::delete($sub->{$func_name}); #free storage
return "$pack::$func_name";
}
21. sub new {
my $class = shift;
my $fmt = shift || "combined";
$fmt = $formats{$fmt} if exists $formats{$fmt};
my %opts = @_;
my ($code_ref, $code) = compile($fmt, $opts{block_handlers} || {},
$opts{char_handlers} || {});
bless [$code_ref, $code], $class;
}
sub log_line {
my $self = shift;
$self->[0]->(@_) . "n";
}
22. sub compile {
my $fmt = shift;
my $extra_block_handlers = shift;
my $extra_char_handlers = shift;
$fmt =~ s/!/!/g;
$fmt =~ s!
(?:
%{(.+?)}([a-zA-Z]) |
%(?:[<>])?([a-zA-Z%])
)
! $1 ? $block_handler->($1, $2, $extra_block_handlers) : $char_handler->($3, $extra_char_handlers) !egx;
my @abbr = qw( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec );
my $c = {};
$fmt = q~sub {
$_[TIME] = time() if ! defined $_[TIME];
my @lt = localtime($_[TIME]);
if ( ! exists $c->{tz_cache} || ! exists $c->{isdst_cache} || $lt[8] != $c->{isdst_cache} ) {
$c->{tz_cache} = POSIX::strftime::Compiler::strftime('%z',@lt);
$c->{isdst_cache} = $lt[8];
}
my $t = sprintf '%02d/%s/%04d:%02d:%02d:%02d %s', $lt[3], $abbr[$lt[4]], $lt[5]+1900,
$lt[2], $lt[1], $lt[0], $c->{tz_cache};
q!~ . $fmt . q~!
}~;
my $code_ref = eval $fmt; ## no critic
die $@ . "n===n" . $fmt if $@;
wantarray ? ($code_ref, $fmt) : $code_ref;
}