World of meta programming • Make a program by program. • Example: • Define subroutine by program. • Define package by program. • Call specified subroutine by a variable. • Rewrite AST by program. (not talk about it today.) • Called as “PL_check hack”. (XS)
Which is implemented by meta programming? • constant / Exporter / parent • Class::Accessor / Class::MOP (Moose) etc… • Class::Method::Modifiers • Module::Spy / Test::MockObject / Test::Mock::Guard • Apache::LogFormat::Compiler / Template-Toolkit • and more…
Perl5 has meta programming culture.
What do we get by meta programing? • Support to DRY • Inner DSL • And, readable code. • Preprocessing • And, faster processing.
In other words, freedom
“With freedom comes responsibility.” –Eleanor Roosevelt
What do we get by meta programing? • Incomprehension code. • Dirty inner DSL • Not readable. • Long, and long, and long time preprocessing. • Yep, you get slower code.
Usage of meta programming.
Meta programming is difficult. • Very freedom. • Yes. We can do anything! • Tend to the complex. • Difficult to analyze code by human. • You can’t know the process until execute. • It’s extreme.
But, Why we want meta programming? • Want to apply monkey patch. • But, Don’t want to rewrite source code. • We want to create many method. • But, There are similar almost. • Want to generate methods automatically.
Vital point • Write test scripts!!!!!!!!!!!!!!!!!!!!!!! • Solve the problem in existing modules. • as much as possible. • Create module as simply. • Give up when you can not be simple. • Think about more fundamental problem.
How to implement by meta programming. in perl5
string eval • Preprocessing. • Create minimized code by template. (Apache::LogFormat::Compiler/Template-Toolkit) • Define package/subroutine by program. • Create something like an anonymous class. (Moo::Role/Mo[uo]se::Role/etc…) • And, more!!!
string eval • Execute code by string. • Very freedom. • Very useful. • Be careful perl code injection attack. • So, do *NOT* use it if not needed.
string eval DEMO
UNIVERSAL • Base package of all packages.
symbol talbe • e.g.) %main:: • Haves almost definition information of package. • Haves all typeglobs. • We can modify it. • Define (package|subroutine) by program.
typeglob • “a typeglob to hold an entire symbol table entry.”(from perldoc perldata) • *foo haves $foo and @foo and %foo.
symbol table & typeglob DEMO
AUTOLOAD • Fallback of subroutine call.
Package::Stash • Provides symbol table modifier and accessor.
B.pm • Perl compiler backend. • Provides interface for access to inner data structure. • SV (PV|IV|NV|RV) / AV / HV / GV / CV / IO • e.g.) B::PV / B::IV / B::GV / B::CV / etc… • Provides many utility. (e.g. B::perlstring) • I'm not familiar. :0
B::svref_2object • Convert reference to B::* instance. • Access to inner information of object.
Class::Inspector • Provides to access information about a class and its structure • e.g.) methods, functions, etc…
Summary • Get information from package. • Class::Inspector/Package::Stash/symbol table • and, meta object protocol (Mo[ou]se|Moo) • Define methods or packages. • Package::Stash/symbol table/meta object protocol. • A last resort • String eval.
Vital point AGAIN • Write test scripts!!!!!!!!!!!!!!!!!!!!!!! • Solve the problem in existing modules. • as much as possible. • Create module as simply. • Give up when you can not be simple. • Think about more fundamental problem.
Mobile Factory, Inc. • Using modern perl in many projects. • perl 5.18 + Carton + Amon2 + Starlet • perl 5.14 + Amon2 • Free vegetable juices and other something drinks. • Everyone available dual display. (22inch x 2)