perl5 meta programming

51
Perl5 meta programming YAPC::Asia 2014 id:karupanerura

Upload: karupanerura-xmp

Post on 24-May-2015

1.911 views

Category:

Technology


1 download

DESCRIPTION

YAPC::Asia Tokyo 2014 DEMO: https://github.com/karupanerura/yapcasia2014-demo

TRANSCRIPT

Page 1: Perl5 meta programming

Perl5 meta programming

YAPC::Asia 2014 id:karupanerura

Page 2: Perl5 meta programming

NOTICE

Sorry. I don’t speak in English.

Because, I’m not good at English.

So, maybe the wrong english has been many included.

Page 3: Perl5 meta programming

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

Page 4: Perl5 meta programming

Rock’n’roll engineer The Monzllis

Page 5: Perl5 meta programming

Gotanda.pm

• Conference

• #1 6/11 (Wed)

• #2 9/17 (Wed) <= come on !!!

• hash tag (twitter)

• #gotandapm

• http://gotanda-pm.github.io/

Page 6: Perl5 meta programming

CPAN

• TOML.pm

• TOML::Parser

• Locale::Scope

• Parallel::Async

Page 7: Perl5 meta programming

My YAPC::Asia history.

• 2011 LT

• 2012 LT + Individual Sponsor

• 2013 Talk 20min + Individual Sponsor

• 2014 Talk 40min + Individual Sponsor <= now

• + Volunteer Staff

• 2015 ???

Page 8: Perl5 meta programming

– karupanerura

“I wanna be the best speaker.”

Page 9: Perl5 meta programming

Please vote for me!!

Page 10: Perl5 meta programming

Okay. Now starting.

Page 11: Perl5 meta programming

What is meta programming?

Page 12: Perl5 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)

Page 13: Perl5 meta programming

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…

Page 14: Perl5 meta programming

Perl5 has meta programming culture.

Page 15: Perl5 meta programming
Page 16: Perl5 meta programming

What do we get by meta programing?

• Support to DRY

• Inner DSL

• And, readable code.

• Preprocessing

• And, faster processing.

Page 17: Perl5 meta programming

In other words, freedom

Page 18: Perl5 meta programming

–Eleanor Roosevelt

“With freedom comes responsibility.”

Page 19: Perl5 meta programming

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.

Page 20: Perl5 meta programming

Usage of meta programming.

Page 21: Perl5 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.

Page 22: Perl5 meta programming

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.

Page 23: Perl5 meta programming

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.

Page 24: Perl5 meta programming

How to implement by meta programming.

in perl5

Page 25: Perl5 meta programming

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!!!

Page 26: Perl5 meta programming

string eval

• Execute code by string.

• Very freedom.

• Very useful.

• Be careful perl code injection attack.

• So, do *NOT* use it if not needed.

Page 27: Perl5 meta programming

string eval

DEMO

Page 28: Perl5 meta programming

UNIVERSAL

• Base package of all packages.

Page 29: Perl5 meta programming

UNIVERSAL

DEMO

Page 30: Perl5 meta programming

symbol talbe

• e.g.) %main::

• Haves almost definition information of package.

• Haves all typeglobs.

• We can modify it.

• Define (package|subroutine) by program.

Page 31: Perl5 meta programming

typeglob

• “a typeglob to hold an entire symbol table entry.”(from perldoc perldata)

• *foo haves $foo and @foo and %foo.

Page 32: Perl5 meta programming

symbol table & typeglob

DEMO

Page 33: Perl5 meta programming

AUTOLOAD

• Fallback of subroutine call.

Page 34: Perl5 meta programming

AUTOLOAD

DEMO

Page 35: Perl5 meta programming

Package::Stash

• Provides symbol table modifier and accessor.

Page 36: Perl5 meta programming

Package::Stash

DEMO

Page 37: Perl5 meta programming

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

Page 38: Perl5 meta programming

B::svref_2object

• Convert reference to B::* instance.

• Access to inner information of object.

Page 39: Perl5 meta programming

B::svref_2object

DEMO

Page 40: Perl5 meta programming

Class::Inspector

• Provides to access information about a class and its structure

• e.g.) methods, functions, etc…

Page 41: Perl5 meta programming

Class::Inspector

DEMO

Page 42: Perl5 meta programming

Class::Method::Modifiers

• Provides method modifiers.

• e.g.) before, after, around, etc…

Page 43: Perl5 meta programming

Class::Method::Modifiers

DEMO

Page 44: Perl5 meta programming

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…

Page 45: Perl5 meta programming

Moo

DEMO

Page 46: Perl5 meta programming

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.

Page 47: Perl5 meta programming

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.

Page 48: Perl5 meta programming

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)

Page 49: Perl5 meta programming

!

Let’s work at Mobile Factory team!!

Page 50: Perl5 meta programming

!

Thank you for listening!!

Page 51: Perl5 meta programming

Any questions?