This document discusses Perl 5 meta programming and provides examples of how to implement meta programming in Perl 5. It describes techniques like string eval, modifying the symbol table and typeglobs, using AUTOLOAD, Package::Stash, B:: modules, Class::Inspector, Class::Method::Modifiers, and Moo to get and set information about classes and define/modify methods and packages at runtime. It emphasizes writing tests and solving problems with existing modules when possible.
2. NOTICE
Sorry. I don’t speak in English.
Because, I’m not good at English.
So, maybe the wrong english has been many included.
3. 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
12. 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)
13. 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…
19. 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.
21. 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.
22. 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.
23. 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.
30. symbol talbe
• e.g.) %main::
• Haves almost definition information of package.
• Haves all typeglobs.
• We can modify it.
• Define (package|subroutine) by program.
31. typeglob
• “a typeglob to hold an entire symbol table
entry.”(from perldoc perldata)
• *foo haves $foo and @foo and %foo.
46. 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.
47. 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.
48. 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)