SlideShare a Scribd company logo
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 ???
“I wanna be the best speaker.” 
– karupanerura
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.
Perl5 meta programming
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.
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?

More Related Content

PDF
Introduction to python
PDF
Raspberry using Python Session 1
PPTX
Austin Python Learners Meetup - Everything you need to know about programming...
PPTX
What is Python?
PDF
Why i love ruby than x
PPTX
Not Everything is an Object - Rocksolid Tour 2013
PDF
Ab(Using) the MetaCPAN API for Fun and Profit v2013
PPTX
Introduction about Python by JanBask Training
Introduction to python
Raspberry using Python Session 1
Austin Python Learners Meetup - Everything you need to know about programming...
What is Python?
Why i love ruby than x
Not Everything is an Object - Rocksolid Tour 2013
Ab(Using) the MetaCPAN API for Fun and Profit v2013
Introduction about Python by JanBask Training

What's hot (19)

ODP
ODP
Using ANTLR on real example - convert "string combined" queries into paramete...
ODP
ANTLR4 and its testing
PPT
Python intro and competitive programming
PPTX
Python introduction
PDF
Vimeo and Open Source (SMPTE Forum 2015)
PDF
Beginning Python
PDF
Webinar: OpenNLP and Solr for Superior Relevance
PPTX
Binary Studio Academy PRO: ANTLR course by Alexander Vasiltsov (lesson 1)
PDF
Sdforum 11-04-2010
PPTX
Not Everything Is An Object
PDF
Alexander Kutsan "Where Unicorns Live or How To Write Good C++ Code"
KEY
Erlang: TL;DR
PPTX
F# for startups
PDF
Multilingual sites in plone
PDF
Python tutorial
KEY
PL/Parrot San Francisco Perl Mongers 2010/05/25
PDF
A Static Type Analyzer of Untyped Ruby Code for Ruby 3
PPTX
Go for Rubyists. August 2018. RUG-B Meetup
Using ANTLR on real example - convert "string combined" queries into paramete...
ANTLR4 and its testing
Python intro and competitive programming
Python introduction
Vimeo and Open Source (SMPTE Forum 2015)
Beginning Python
Webinar: OpenNLP and Solr for Superior Relevance
Binary Studio Academy PRO: ANTLR course by Alexander Vasiltsov (lesson 1)
Sdforum 11-04-2010
Not Everything Is An Object
Alexander Kutsan "Where Unicorns Live or How To Write Good C++ Code"
Erlang: TL;DR
F# for startups
Multilingual sites in plone
Python tutorial
PL/Parrot San Francisco Perl Mongers 2010/05/25
A Static Type Analyzer of Untyped Ruby Code for Ruby 3
Go for Rubyists. August 2018. RUG-B Meetup
Ad

Viewers also liked (20)

PDF
Aniki has come
PDF
すいすいSwift
PDF
若手Itエンジニア飲み会-LT
PDF
Gotanda.pmの紹介
PDF
engineer-life
PDF
Aniki - The ORM as our great brother.
PDF
KOWAZA for mackerel
PDF
Why we use mruby with Perl5?
PDF
dwangocpp1-lt
PDF
The Crystal language *recently* update
PPTX
TIme::Moment+Time::Strptime=
PDF
Optimize perl5 code for perfomance freaks
PDF
【モバイルファクトリー】「いわて×駅メモ!」位置ゲームによる観光振興の可能性
PDF
モジュール開発におけるぼくの試行錯誤
PDF
Adjoint Shape Optimization using OpenFOAM
PDF
オープンソースの CFD ソフトウェア SU2 のチュートリアルをやってみた
PDF
Spatial Interpolation Schemes in OpenFOAM
PDF
OpenFOAM -空間の離散化と係数行列の取り扱い(Spatial Discretization and Coefficient Matrix)-
PDF
OpenFOAMの壁関数
PDF
Boundary Conditions in OpenFOAM
Aniki has come
すいすいSwift
若手Itエンジニア飲み会-LT
Gotanda.pmの紹介
engineer-life
Aniki - The ORM as our great brother.
KOWAZA for mackerel
Why we use mruby with Perl5?
dwangocpp1-lt
The Crystal language *recently* update
TIme::Moment+Time::Strptime=
Optimize perl5 code for perfomance freaks
【モバイルファクトリー】「いわて×駅メモ!」位置ゲームによる観光振興の可能性
モジュール開発におけるぼくの試行錯誤
Adjoint Shape Optimization using OpenFOAM
オープンソースの CFD ソフトウェア SU2 のチュートリアルをやってみた
Spatial Interpolation Schemes in OpenFOAM
OpenFOAM -空間の離散化と係数行列の取り扱い(Spatial Discretization and Coefficient Matrix)-
OpenFOAMの壁関数
Boundary Conditions in OpenFOAM
Ad

Similar to Perl5 meta programming (20)

PPTX
Day 1 - Intro to Ruby
PPTX
これからのPerlプロダクトのかたち(YAPC::Asia 2013)
PDF
Performance and Abstractions
PPT
Introduction to the intermediate Python - v1.1
PPT
Bridging the Gap - Laracon 2013
PDF
Packaging perl (LPW2010)
PDF
An Introduction to the Laravel Framework (AFUP Forum PHP 2014)
PDF
Finding Needles in Haystacks
PPTX
Python Tutorial Part 2
PDF
From SaltStack to Puppet and beyond...
PDF
Functional Programming for Busy Object Oriented Programmers
PDF
How to Use Selenium, Successfully
PPTX
How I Learned to Stop Worrying and Love Legacy Code.....
PDF
Reproducibility and automation of machine learning process
PPTX
Intro to Python for C# Developers
PPTX
Introduction to Python for Security Professionals
PPTX
python_module_.................................................................
PPTX
Tuples, Dicts and Exception Handling
PDF
Puppet Camp Paris 2014: Test Driven Development
PDF
20140408 tdd puppetcamp-paris
Day 1 - Intro to Ruby
これからのPerlプロダクトのかたち(YAPC::Asia 2013)
Performance and Abstractions
Introduction to the intermediate Python - v1.1
Bridging the Gap - Laracon 2013
Packaging perl (LPW2010)
An Introduction to the Laravel Framework (AFUP Forum PHP 2014)
Finding Needles in Haystacks
Python Tutorial Part 2
From SaltStack to Puppet and beyond...
Functional Programming for Busy Object Oriented Programmers
How to Use Selenium, Successfully
How I Learned to Stop Worrying and Love Legacy Code.....
Reproducibility and automation of machine learning process
Intro to Python for C# Developers
Introduction to Python for Security Professionals
python_module_.................................................................
Tuples, Dicts and Exception Handling
Puppet Camp Paris 2014: Test Driven Development
20140408 tdd puppetcamp-paris

More from karupanerura (17)

PDF
Perl5 VS JSON
PDF
コンテキストと仲良く
PDF
The plan of Aniki 2.0
PDF
Aniki::Internal
PPTX
DateTimeX::Moment
PDF
Technology for reduce of mistakes - うっかりをなくす技術
PDF
router-simple.cr
PDF
はかたの塩
PDF
Perlにおけるclass実装パターン
PDF
mysqlcasual6-next-key-lock
PDF
Hachioji.pm #40
PDF
Hachioji.pm #39
PDF
perl5の日付時刻処理とか
PDF
Yapc asia-2012-lt-thon
PDF
ぼくがかんがえたさいきょうのMvc
PDF
Perl RDBMS Programming(DBI/DBIx::Sunnyのはなし)
KEY
Teng tips
Perl5 VS JSON
コンテキストと仲良く
The plan of Aniki 2.0
Aniki::Internal
DateTimeX::Moment
Technology for reduce of mistakes - うっかりをなくす技術
router-simple.cr
はかたの塩
Perlにおけるclass実装パターン
mysqlcasual6-next-key-lock
Hachioji.pm #40
Hachioji.pm #39
perl5の日付時刻処理とか
Yapc asia-2012-lt-thon
ぼくがかんがえたさいきょうのMvc
Perl RDBMS Programming(DBI/DBIx::Sunnyのはなし)
Teng tips

Recently uploaded (20)

PDF
cuic standard and advanced reporting.pdf
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PPT
Teaching material agriculture food technology
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
gpt5_lecture_notes_comprehensive_20250812015547.pdf
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Empathic Computing: Creating Shared Understanding
PDF
A comparative analysis of optical character recognition models for extracting...
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPTX
Machine Learning_overview_presentation.pptx
PDF
Approach and Philosophy of On baking technology
PPTX
A Presentation on Artificial Intelligence
PDF
Machine learning based COVID-19 study performance prediction
PDF
Review of recent advances in non-invasive hemoglobin estimation
PPTX
Spectroscopy.pptx food analysis technology
cuic standard and advanced reporting.pdf
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Teaching material agriculture food technology
“AI and Expert System Decision Support & Business Intelligence Systems”
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
gpt5_lecture_notes_comprehensive_20250812015547.pdf
Spectral efficient network and resource selection model in 5G networks
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Chapter 3 Spatial Domain Image Processing.pdf
Empathic Computing: Creating Shared Understanding
A comparative analysis of optical character recognition models for extracting...
Advanced methodologies resolving dimensionality complications for autism neur...
Machine Learning_overview_presentation.pptx
Approach and Philosophy of On baking technology
A Presentation on Artificial Intelligence
Machine learning based COVID-19 study performance prediction
Review of recent advances in non-invasive hemoglobin estimation
Spectroscopy.pptx food analysis technology

Perl5 meta programming

  • 1. Perl5 meta programming YAPC::Asia 2014 id:karupanerura
  • 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
  • 5. Gotanda.pm • Conference • #1 6/11 (Wed) • #2 9/17 (Wed) <= come on !!! • hash tag (twitter) • #gotandapm • http://gotanda-pm.github.io/
  • 6. CPAN • TOML.pm • TOML::Parser • Locale::Scope • Parallel::Async
  • 7. My YAPC::Asia history. • 2011 LT • 2012 LT + Individual Sponsor • 2013 Talk 20min + Individual Sponsor • 2014 Talk 40min + Individual Sponsor <= now • + Volunteer Staff • 2015 ???
  • 8. “I wanna be the best speaker.” – karupanerura
  • 11. What is meta programming?
  • 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…
  • 14. Perl5 has meta programming culture.
  • 16. What do we get by meta programing? • Support to DRY • Inner DSL • And, readable code. • Preprocessing • And, faster processing.
  • 17. In other words, freedom
  • 18. “With freedom comes responsibility.” –Eleanor Roosevelt
  • 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.
  • 20. Usage of meta programming.
  • 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.
  • 24. How to implement by meta programming. in perl5
  • 25. 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!!!
  • 26. string eval • Execute code by string. • Very freedom. • Very useful. • Be careful perl code injection attack. • So, do *NOT* use it if not needed.
  • 28. UNIVERSAL • Base package of all packages.
  • 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.
  • 32. symbol table & typeglob DEMO
  • 33. AUTOLOAD • Fallback of subroutine call.
  • 35. Package::Stash • Provides symbol table modifier and accessor.
  • 37. 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
  • 38. B::svref_2object • Convert reference to B::* instance. • Access to inner information of object.
  • 40. Class::Inspector • Provides to access information about a class and its structure • e.g.) methods, functions, etc…
  • 42. Class::Method::Modifiers • Provides method modifiers. • e.g.) before, after, around, etc…
  • 44. 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…
  • 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)
  • 49. ! Let’s work at Mobile Factory team!!
  • 50. ! Thank you for listening!!