perl5 meta programming
DESCRIPTION
YAPC::Asia Tokyo 2014 DEMO: https://github.com/karupanerura/yapcasia2014-demoTRANSCRIPT
Perl5 meta programming
YAPC::Asia 2014 id:karupanerura
NOTICE
Sorry. I don’t speak in English.
Because, I’m not good at English.
So, maybe the wrong english has been many included.
about me
• Kenta Sato
• id:karupanerura
• github/twitter/etc..
• Perl/JavaScript/Java/elisp…
• CPAN Author
• PAUSE ID: KARUPA
• Mobile Factory, Inc.
• Lead Engineer
• Social Application
Rock’n’roll engineer The Monzllis
Gotanda.pm
• Conference
• #1 6/11 (Wed)
• #2 9/17 (Wed) <= come on !!!
• hash tag (twitter)
• #gotandapm
• http://gotanda-pm.github.io/
CPAN
• TOML.pm
• TOML::Parser
• Locale::Scope
• Parallel::Async
My YAPC::Asia history.
• 2011 LT
• 2012 LT + Individual Sponsor
• 2013 Talk 20min + Individual Sponsor
• 2014 Talk 40min + Individual Sponsor <= now
• + Volunteer Staff
• 2015 ???
– karupanerura
“I wanna be the best speaker.”
Please vote for me!!
Okay. Now starting.
What is meta programming?
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
–Eleanor Roosevelt
“With freedom comes responsibility.”
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.
UNIVERSAL
DEMO
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.
AUTOLOAD
DEMO
Package::Stash
• Provides symbol table modifier and accessor.
Package::Stash
DEMO
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.
B::svref_2object
DEMO
Class::Inspector
• Provides to access information about a class and its structure
• e.g.) methods, functions, etc…
Class::Inspector
DEMO
Class::Method::Modifiers
• Provides method modifiers.
• e.g.) before, after, around, etc…
Class::Method::Modifiers
DEMO
Mo[ou]se (Moo)
• Provides meta object protocol.
• Likes a Package::Stash.
• e.g.) add_method, remove_method, etc…
• Provides method modifiers.
• e.g.) before, after, around, etc…
Moo
DEMO
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)
!
Let’s work at Mobile Factory team!!
!
Thank you for listening!!
Any questions?