SlideShare a Scribd company logo
Code Generation in Perl
Presentation by Ian Kluft
for Silicon Valley Perl
September 7, 2017
Santa Clara, California
What is Code Generation?
●
Programs that write programs
●
There are many ways to approach this
●
Advantage: lets the coder work at a higher level
●
Disadvantage: someone still has to write program
The simplest example
●
A program hard-coded to print another program
●
Even the simple example has non-trivial quoting
#!/usr/bin/perl
use strict;
use warnings;
print qq(#!/usr/bin/perln);
print qq(use strict;n);
print qq(print "hello worldn";n);
Other approaches
●
Templates – fill in the blanks
●
Translators – parse, analyze and re-generate
●
Compilers – parse high level, generate lower level
●
SQL schemas & queries – generate SQL code
●
XML parsing – generate programs from XML data
●
UML tools – draw diagrams, generate code
CPAN Modules
which generate code
●
At a low level it’s widespread
– Many modules & scripts generate code for eval()
●
Perl build tools
●
SQL generation for queries and schemas
●
Perl application frameworks
– Easier to get started with tools that generate examples
●
Compilers
CPAN Modules
Perl build tools
●
CPAN Modules which generate code
– Dist::Zilla
– ExtUtils::MakeMaker
– Module::Build
●
These generate build scripts from module
configuration data
CPAN Modules
SQL code generation
●
DBIx::Class – object relational mapper
●
Fey – SQL schema and query generator
●
…CPAN is huge - the list goes on and on...
●
Many examples – which do you like?
CPAN Modules
XML & data code generators
●
Data::Dumper – dumps data as Perl code
●
Faker – extensible fake data generator
●
Not as much code generation from XML on CPAN
– Obvious category for ad hoc tools
– Any XML parser can be used to generate code
●
Writing HTML is not technically writing code
– But it also has similarities
CPAN Modules
Compilers
●
B::* - access to Perl compiler
●
Compiler generation tools
– Parse::Yapp/Parse::Eyapp – compiler parser generator
●
Similar to Unix “yacc” but generates Perl code
– Parse::RecDescent – generate recursive descent
compiler
– Parse::Lex – generate lexical analyzer for compiler
●
A lot of CPAN modules claiming to “compile”
actually “translate”
How Compilers Work
●
High level view of a compiler
Preprocessor
Scanner/
Tokenizer
Parser Semantics
●
Preprocessor – macros & text manipulation
●
Scanner – generates stream of tokens
– Removes comments
●
Parser – recognizes language structures
●
Semantics – generate code
– Some compilers like Perl make intermediate code for interpretation
Using Parser Generator Tools
●
Have you noticed that language compilers do things that
are way above the capabilities of regular expressions?
– RegEx’es are ideal for tokenizers/scanners
– There’s another level of language parsing above that
●
One example is to grab a yacc grammar and translate it
to Parse::Yapp or Parse::Eyapp
– I did that once for a Perl script that parsed /etc/sudoers files for
automated security audits
●
Language definitions in Backus-Naur Form (BNF) can be
converted to language parser defintions
UML
Unified Modeling Language
●
XML format representing standard diagrams
– Parts of a program or system design
– Analysis of code can generate UML for documentation
– Active research into ways to generate code from UML
●
Code generation can be done
●
But diagramming is far more tedious than typing code
●
You can experiment with this on your own projects
●
Idea is to use UML as design/documentation & generate code
UML – Unified Modeling Language
types of diagrams
Diagram
Class
Diagram
Component
Diagram
Object
Diagram
Activity
Diagram
Use Case
Diagram
Profile
Diagram
Composite
Structure
Diagram
Deployment
Diagram
Package
Diagram
Interaction
Diagram
State
Machine
Diagram
Sequence
Diagram
Communication
Diagram
Interaction
Overview
Diagram
Timing
Diagram Notation: UML
Behaviour
Diagram
Structure
Diagram
UML diagram types – as a class diagram (from Wikipedia)
UML – Unified Modeling Language
example class: Pizza
UML code generation makes class
source code starter templates for
●
Pizza
●
CrustEnum
●
ToppingList
●
ToppingEnum
●
SizeEnum
How would you go from here to write the code for the classes in UML?
Good ways to do that are still open to suggestion.
UML – Unified Modeling Language
Tools for Code Generation from UML
●
Open source “umbrello”
from KDE generated
previous slide’s graphic
●
Eclipse IDE has UML
generation tools
●
CPAN has many UML
modules, mostly for
generating UML from Perl for
analysis
●
Many other examples...
Setting expectations
What a non-engineer
manager wants…
This one doesn’t grasp
the current limitations.
He isn’t alone.
What people want is
often a hint where
research should push
the limits for the future.
Setting expectations
”Artificial Intelligence”
●
Artificial Intelligence has a broken promise built right into
its name
●
It has always been advanced algorithms
– It’s always non-trivial
– Not all uses of the term AI are as sophisticated as claimed
●
Its results usually appear clever
●
It can do lots of things people don’t do well
●
But it still can’t think
●
It isn’t real or artificial intelligence… yet
Science Fiction examples
Code generation and AI
●
One early SciFi story: “The Adolescence of P1” by
Thomas J Ryan, 1977
●
Based on modem-connected IBM 360 mainframes
●
Program to break OS “supervisor”
– Needed decision trees, code generator, threat analysis
– When told to shut down it played dead, kept running
– AI evolved organically
– Installed itself like a worm, broke OS like a virus
●
Few SciFi AI’s attempted such depth
Code Generation
Setting expectations back to reality
●
SciFi myths need to be debunked
●
Code generation just adds a level of abstraction
●
Someone still has to write the code
●
Short of an actual artificial intelligence, programs won’t
be writing programs any time soon
●
Maybe during 21st Century?
– 20th Century showed how much advancement can happen
– But AI is a fundamentally difficult computing challenge
– Don’t count on it
Code Generation
Q & A

More Related Content

PDF
Language Workbenches
PPSX
Intro flash cards
PPTX
Intro flash cards
PPSX
Ic lecture8
PDF
Implementing DSLs in practice
PDF
DSLs: what, why, how
PDF
Zoo of domain-specific languages
PPTX
Programming assignment-help at myassignmenthelp.net
Language Workbenches
Intro flash cards
Intro flash cards
Ic lecture8
Implementing DSLs in practice
DSLs: what, why, how
Zoo of domain-specific languages
Programming assignment-help at myassignmenthelp.net

What's hot (15)

PPTX
sitHH - No comment?
PPTX
Programming Paradigms
PDF
EMPEX LA 2018 - Inclusion Starts with Docs
PPTX
Learning programming by: Ysa & Faye
PPTX
Learning programming
PPSX
SitFRA - No Comment?
PPT
Programming language
PPTX
Dion computerprogramming
PPT
Programming Methodology
PPTX
Error hanadling in c programming presentation
PDF
ALPHA Script - Presentation
PDF
APU RoR Workshop Series #1 - Introduction to Programming
PDF
ALPHA Script - Development Steps
PDF
1 introduction to compiler
sitHH - No comment?
Programming Paradigms
EMPEX LA 2018 - Inclusion Starts with Docs
Learning programming by: Ysa & Faye
Learning programming
SitFRA - No Comment?
Programming language
Dion computerprogramming
Programming Methodology
Error hanadling in c programming presentation
ALPHA Script - Presentation
APU RoR Workshop Series #1 - Introduction to Programming
ALPHA Script - Development Steps
1 introduction to compiler
Ad

Viewers also liked (13)

PPTX
The Astonishing Resurrection of AI (A Primer on Artificial Intelligence)
PPTX
artificial intelligence in power plants.
PDF
Machine Intelligence: Promises and Challenges
PDF
Py paris2017 / promises and perils in artificial intelligence, by Andreas Muller
PPT
Social Machines: The coming collision of Artificial Intelligence, Social Netw...
PDF
Issues on Artificial Intelligence and Future (Standards Perspective)
PPT
artificial intelligence
ODP
Artificial Intelligence
PDF
Hype vs. Reality: The AI Explainer
PDF
Deep Learning - The Past, Present and Future of Artificial Intelligence
PPT
Artificial Intelligence
PDF
What is Artificial Intelligence | Artificial Intelligence Tutorial For Beginn...
PPTX
AI and Machine Learning Demystified by Carol Smith at Midwest UX 2017
The Astonishing Resurrection of AI (A Primer on Artificial Intelligence)
artificial intelligence in power plants.
Machine Intelligence: Promises and Challenges
Py paris2017 / promises and perils in artificial intelligence, by Andreas Muller
Social Machines: The coming collision of Artificial Intelligence, Social Netw...
Issues on Artificial Intelligence and Future (Standards Perspective)
artificial intelligence
Artificial Intelligence
Hype vs. Reality: The AI Explainer
Deep Learning - The Past, Present and Future of Artificial Intelligence
Artificial Intelligence
What is Artificial Intelligence | Artificial Intelligence Tutorial For Beginn...
AI and Machine Learning Demystified by Carol Smith at Midwest UX 2017
Ad

Similar to Code Generation in Perl (20)

PPT
C# Fundamental
PDF
Node & Express as Workflow Tools
PPTX
Programming Paradigm & Languages
PPTX
Programming Paradigm & Languages
ODP
The Art of Evolutionary Algorithms Programming
PDF
ProgFund_Lecture_1_Introduction_to_Programming.pdf
PPTX
CD - CH1 - Introduction to compiler design.pptx
PPTX
Cd ch1 - introduction
PPTX
4_5802928814682016556.pptx
PDF
C Language
ODP
YAPC::NA 2007 - Epic Perl Coding
PPT
Topic 1 B C programming exercises one.ppt
PDF
Half-automatic Compilable Source Code Recovery
PPTX
Serverless Functions and Machine Learning: Putting the AI in APIs
PDF
Quick Intro to Clean Coding
PPTX
これからのPerlプロダクトのかたち(YAPC::Asia 2013)
PDF
Compiler Design Introduction
PDF
Merged Automation Talk - Pete Carapetyan - Feb 2016
PPT
Compilers and interpreters
C# Fundamental
Node & Express as Workflow Tools
Programming Paradigm & Languages
Programming Paradigm & Languages
The Art of Evolutionary Algorithms Programming
ProgFund_Lecture_1_Introduction_to_Programming.pdf
CD - CH1 - Introduction to compiler design.pptx
Cd ch1 - introduction
4_5802928814682016556.pptx
C Language
YAPC::NA 2007 - Epic Perl Coding
Topic 1 B C programming exercises one.ppt
Half-automatic Compilable Source Code Recovery
Serverless Functions and Machine Learning: Putting the AI in APIs
Quick Intro to Clean Coding
これからのPerlプロダクトのかたち(YAPC::Asia 2013)
Compiler Design Introduction
Merged Automation Talk - Pete Carapetyan - Feb 2016
Compilers and interpreters

More from Ian Kluft (14)

PDF
"#AprilFools Hijinks" at SVPerl April 2021 meeting
PDF
Secure Coding in Perl
PDF
New Perl module Container::Buildah - SVPerl presentation
PDF
Securing a Raspberry Pi and other DIY IoT devices
PDF
Best Practices for Recovering Rocket & Balloon Payloads
PDF
PiFlash: Linux utility to flash SD cards for Raspberry Pi computers
PDF
Aerospace applications of Perl
PDF
Command Line Arguments with Getopt::Long
PDF
Geographic Computation in Perl
PDF
Black Rock Desert Impact Theory
ODP
Exception Handling in Perl
ODP
Geographic Computation in Perl
ODP
Stratofox Aerospace Tracking Team presentation at Space Access 2013
ODP
Pacificon 200905
"#AprilFools Hijinks" at SVPerl April 2021 meeting
Secure Coding in Perl
New Perl module Container::Buildah - SVPerl presentation
Securing a Raspberry Pi and other DIY IoT devices
Best Practices for Recovering Rocket & Balloon Payloads
PiFlash: Linux utility to flash SD cards for Raspberry Pi computers
Aerospace applications of Perl
Command Line Arguments with Getopt::Long
Geographic Computation in Perl
Black Rock Desert Impact Theory
Exception Handling in Perl
Geographic Computation in Perl
Stratofox Aerospace Tracking Team presentation at Space Access 2013
Pacificon 200905

Recently uploaded (20)

PDF
Design an Analysis of Algorithms II-SECS-1021-03
PDF
medical staffing services at VALiNTRY
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PPTX
Computer Software and OS of computer science of grade 11.pptx
PPTX
L1 - Introduction to python Backend.pptx
PPTX
Embracing Complexity in Serverless! GOTO Serverless Bengaluru
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PDF
top salesforce developer skills in 2025.pdf
PDF
Nekopoi APK 2025 free lastest update
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PPTX
assetexplorer- product-overview - presentation
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PPTX
ai tools demonstartion for schools and inter college
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
Design an Analysis of Algorithms II-SECS-1021-03
medical staffing services at VALiNTRY
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
Design an Analysis of Algorithms I-SECS-1021-03
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
wealthsignaloriginal-com-DS-text-... (1).pdf
Computer Software and OS of computer science of grade 11.pptx
L1 - Introduction to python Backend.pptx
Embracing Complexity in Serverless! GOTO Serverless Bengaluru
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
top salesforce developer skills in 2025.pdf
Nekopoi APK 2025 free lastest update
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
assetexplorer- product-overview - presentation
Odoo Companies in India – Driving Business Transformation.pdf
ai tools demonstartion for schools and inter college
Upgrade and Innovation Strategies for SAP ERP Customers
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...

Code Generation in Perl

  • 1. Code Generation in Perl Presentation by Ian Kluft for Silicon Valley Perl September 7, 2017 Santa Clara, California
  • 2. What is Code Generation? ● Programs that write programs ● There are many ways to approach this ● Advantage: lets the coder work at a higher level ● Disadvantage: someone still has to write program
  • 3. The simplest example ● A program hard-coded to print another program ● Even the simple example has non-trivial quoting #!/usr/bin/perl use strict; use warnings; print qq(#!/usr/bin/perln); print qq(use strict;n); print qq(print "hello worldn";n);
  • 4. Other approaches ● Templates – fill in the blanks ● Translators – parse, analyze and re-generate ● Compilers – parse high level, generate lower level ● SQL schemas & queries – generate SQL code ● XML parsing – generate programs from XML data ● UML tools – draw diagrams, generate code
  • 5. CPAN Modules which generate code ● At a low level it’s widespread – Many modules & scripts generate code for eval() ● Perl build tools ● SQL generation for queries and schemas ● Perl application frameworks – Easier to get started with tools that generate examples ● Compilers
  • 6. CPAN Modules Perl build tools ● CPAN Modules which generate code – Dist::Zilla – ExtUtils::MakeMaker – Module::Build ● These generate build scripts from module configuration data
  • 7. CPAN Modules SQL code generation ● DBIx::Class – object relational mapper ● Fey – SQL schema and query generator ● …CPAN is huge - the list goes on and on... ● Many examples – which do you like?
  • 8. CPAN Modules XML & data code generators ● Data::Dumper – dumps data as Perl code ● Faker – extensible fake data generator ● Not as much code generation from XML on CPAN – Obvious category for ad hoc tools – Any XML parser can be used to generate code ● Writing HTML is not technically writing code – But it also has similarities
  • 9. CPAN Modules Compilers ● B::* - access to Perl compiler ● Compiler generation tools – Parse::Yapp/Parse::Eyapp – compiler parser generator ● Similar to Unix “yacc” but generates Perl code – Parse::RecDescent – generate recursive descent compiler – Parse::Lex – generate lexical analyzer for compiler ● A lot of CPAN modules claiming to “compile” actually “translate”
  • 10. How Compilers Work ● High level view of a compiler Preprocessor Scanner/ Tokenizer Parser Semantics ● Preprocessor – macros & text manipulation ● Scanner – generates stream of tokens – Removes comments ● Parser – recognizes language structures ● Semantics – generate code – Some compilers like Perl make intermediate code for interpretation
  • 11. Using Parser Generator Tools ● Have you noticed that language compilers do things that are way above the capabilities of regular expressions? – RegEx’es are ideal for tokenizers/scanners – There’s another level of language parsing above that ● One example is to grab a yacc grammar and translate it to Parse::Yapp or Parse::Eyapp – I did that once for a Perl script that parsed /etc/sudoers files for automated security audits ● Language definitions in Backus-Naur Form (BNF) can be converted to language parser defintions
  • 12. UML Unified Modeling Language ● XML format representing standard diagrams – Parts of a program or system design – Analysis of code can generate UML for documentation – Active research into ways to generate code from UML ● Code generation can be done ● But diagramming is far more tedious than typing code ● You can experiment with this on your own projects ● Idea is to use UML as design/documentation & generate code
  • 13. UML – Unified Modeling Language types of diagrams Diagram Class Diagram Component Diagram Object Diagram Activity Diagram Use Case Diagram Profile Diagram Composite Structure Diagram Deployment Diagram Package Diagram Interaction Diagram State Machine Diagram Sequence Diagram Communication Diagram Interaction Overview Diagram Timing Diagram Notation: UML Behaviour Diagram Structure Diagram UML diagram types – as a class diagram (from Wikipedia)
  • 14. UML – Unified Modeling Language example class: Pizza UML code generation makes class source code starter templates for ● Pizza ● CrustEnum ● ToppingList ● ToppingEnum ● SizeEnum How would you go from here to write the code for the classes in UML? Good ways to do that are still open to suggestion.
  • 15. UML – Unified Modeling Language Tools for Code Generation from UML ● Open source “umbrello” from KDE generated previous slide’s graphic ● Eclipse IDE has UML generation tools ● CPAN has many UML modules, mostly for generating UML from Perl for analysis ● Many other examples...
  • 16. Setting expectations What a non-engineer manager wants… This one doesn’t grasp the current limitations. He isn’t alone. What people want is often a hint where research should push the limits for the future.
  • 17. Setting expectations ”Artificial Intelligence” ● Artificial Intelligence has a broken promise built right into its name ● It has always been advanced algorithms – It’s always non-trivial – Not all uses of the term AI are as sophisticated as claimed ● Its results usually appear clever ● It can do lots of things people don’t do well ● But it still can’t think ● It isn’t real or artificial intelligence… yet
  • 18. Science Fiction examples Code generation and AI ● One early SciFi story: “The Adolescence of P1” by Thomas J Ryan, 1977 ● Based on modem-connected IBM 360 mainframes ● Program to break OS “supervisor” – Needed decision trees, code generator, threat analysis – When told to shut down it played dead, kept running – AI evolved organically – Installed itself like a worm, broke OS like a virus ● Few SciFi AI’s attempted such depth
  • 19. Code Generation Setting expectations back to reality ● SciFi myths need to be debunked ● Code generation just adds a level of abstraction ● Someone still has to write the code ● Short of an actual artificial intelligence, programs won’t be writing programs any time soon ● Maybe during 21st Century? – 20th Century showed how much advancement can happen – But AI is a fundamentally difficult computing challenge – Don’t count on it