SlideShare a Scribd company logo
flickr: philip66
Make your own Perl
with
M o o p s
@friedo
Mike Friedman
London Perl Workshop 2014
A brief history…
A brief history…
In the Beginning,
Perl 5
was already extensible.
use Foo qw(bar);
use Foo qw(bar);
BEGIN {!
require Foo;!
Foo->import( ‘bar’ );!
};
How does
!
import!
!
actually work?
sub import {!
my $pkg = shift;!
...
a typical import sub
sub import {!
my $pkg = shift;!
my $callpkg = caller;!
...
a typical import sub
sub import {!
my $pkg = shift;!
my $callpkg = caller;!
my @imports = @_;!
...
a typical import sub
sub import {!
... !
foreach !
my $sym(@imports) {!
*{"${callpkg}::$sym"}
= *{"${pkg}::$sym"}
a typical import sub
(many details omitted.)
A brief history…
import is compile-time.
A brief history…
import is compile-time.
1. add functions.
2. pretend they’re keywords.
package Adder;!
!
sub import { ... }!
sub add2 { !
return shift + 2;!
}
a pretend keyword
package Mine;!
!
use Adder;!
!
say add2 42;
a pretend keyword
A brief history…
Perl 5.2
!
Prototypes
Make fake keywords
!
That behave like
builtins
(kinda)
sub doblock(&@) { !
my $blk = shift;!
return $blk->( @_ );!
}!
!
doblock { !
say $_ * $_ for @_;!
} ( 1, 2, 3, 4, 5 );!
a prototype pretend keyword
sub doblock(&) { ... }!
!
doblock { !
say "first";!
}!
!
say "second";
a prototype pretend keyword
sub doblock(&) { ... }!
!
doblock { !
say "first";!
}!
!
say "second";
a prototype pretend keyword
# Fail :(
Prototype subs only
work as expressions.
!
Not statements.
sub doblock($&) { ... }!
!
doblock name { !
say "first";!
}!
!
a prototype pretend keyword
# Fail :(
a prototype pretend keyword
# We have to say:
!
!
doblock name => sub { !
say "first";!
};!
!
a prototype pretend keyword
# We have to say:
"&" prototypes are only
magical if they're first.
A brief history…
Perl 5.8
!
Devel::Declare
The first real way to add
!
new syntax.
use MooseX::Declare;!
!
class Foo { !
method bar { ... }!
...!
}!
!
a parser magic keyword
Devel::Declare is
complicated
Devel::Declare is
complicated
•Declarators?
Devel::Declare is
complicated
•Declarators?
•Scope injectors?
Devel::Declare is
complicated
•Declarators?
•Scope injectors?
•Method shadowing?
Devel::Declare is
complicated
•Declarators?
•Scope injectors?
•Method shadowing?
•WTF?
MooseX::Declare is
big, slow, and difficult to
extend.
A brief history…
Perl 5.12
!
The keyword API
Real keyword interface
in the
Perl core API.
package My::Keyword;!
use Keyword::Simple;!
!
sub import { !
Keyword::Simple::define( !
class => sub { !
munge_code( $$_[0] );!
}!
);!
a real custom keyword
So...
!
what is
!
M o o p s ?
use Moops;!
role NamedThing {!
has name => !
(is => "ro", isa => Str);!
}!
!
class Person with NamedThing;!
...
a Moops-defined class
So it's like
MooseX::Declare
?
Not exactly.
Not exactly.
•Moo by default, but can use Moose
Not exactly.
•Moo by default, but can use Moose
•Uses Keyword::Simple for new syntax.
Not exactly.
•Moo by default, but can use Moose
•Uses Keyword::Simple for new syntax.
•Uses Kavorka for functions/methods
Not exactly.
•Moo by default, but can use Moose
•Uses Keyword::Simple for new syntax.
•Uses Kavorka for functions/methods
•Designed for easy extensibility
A brief history…
One year ago,
!
I did this:
I like imports
I hate boilerplate
package MyApp::Setup;!
use Import::Into;!
!
use List::Util ();!
use List::MoreUtils ();!
use Scalar::Util ();!
use Const::Fast ();!
use Try::Tiny ();!
use Data::Alias ();!
use autodie ();!
...
Custom setup class
!
!
sub import { !
my $callpkg = caller;!
Scalar::Util->import::into(!
$callpkg, 'blessed', 'refaddr'!
);!
List::Util->import::into(!
$callpkg, 'reduce', 'all', ...!
);!
...!
}
Custom setup class
Works pretty great!
Unless I want to use
custom syntax
use MooseX::Declare; !
# (or Moops)!
!
class MyApp::Thing { !
use MyApp::Setup;!
}!
!
# :/!
Custom setup class
I have to use the Setup
class from within
the right namespace.
What if the Setup class
could also inject custom
syntax?
package MyApp::Setup;!
use parent 'Moops';!
sub import { !
my $pkg = shift;!
my @imports = (!
'List::Util' => ['any'],!
'Scalar::Util' => ['blessed'],!
'experimental' => ['postderef']!
...!
);!
}
Moops-based setup class
sub import {!
...!
$pkg->SUPER::import(!
imports => @imports!
);!
}!
!
!
# :D
Moops-based setup class
Cool!
flickr: philip66
Questions
@friedo
Mike Friedman
London Perl Workshop 2014

More Related Content

PDF
The Perl API for the Mortally Terrified (beta)
ODP
Perl Moderno
ODP
What's new in Perl 5.10?
PDF
Perl.Hacks.On.Vim Perlchina
PDF
Perl.Hacks.On.Vim Perlchina
PDF
Good Evils In Perl
PDF
Perl.Hacks.On.Vim
PDF
Ruby 入門 第一次就上手
The Perl API for the Mortally Terrified (beta)
Perl Moderno
What's new in Perl 5.10?
Perl.Hacks.On.Vim Perlchina
Perl.Hacks.On.Vim Perlchina
Good Evils In Perl
Perl.Hacks.On.Vim
Ruby 入門 第一次就上手

What's hot (20)

PDF
Ruby 程式語言綜覽簡介
PDF
Just-In-Time Compiler in PHP 8
PDF
Debugging with pry
ODP
Red Flags in Programming
PDF
Perl 5.10
PDF
Create your own PHP extension, step by step - phpDay 2012 Verona
ODP
Embed--Basic PERL XS
PDF
PHP7 is coming
PDF
PHP7. Game Changer.
PDF
An OCaml newbie meets Camlp4 parser
PPTX
Php Extensions for Dummies
PDF
PHP Enums - PHPCon Japan 2021
PDF
Happy Go Programming
PDF
Building Custom PHP Extensions
PDF
What's new in PHP 8.0?
PPTX
Php7 HHVM and co
PDF
Nikita Popov "What’s new in PHP 8.0?"
PDF
How to write a TableGen backend
PPT
Dealing with Legacy Perl Code - Peter Scott
ODP
How Xslate Works
Ruby 程式語言綜覽簡介
Just-In-Time Compiler in PHP 8
Debugging with pry
Red Flags in Programming
Perl 5.10
Create your own PHP extension, step by step - phpDay 2012 Verona
Embed--Basic PERL XS
PHP7 is coming
PHP7. Game Changer.
An OCaml newbie meets Camlp4 parser
Php Extensions for Dummies
PHP Enums - PHPCon Japan 2021
Happy Go Programming
Building Custom PHP Extensions
What's new in PHP 8.0?
Php7 HHVM and co
Nikita Popov "What’s new in PHP 8.0?"
How to write a TableGen backend
Dealing with Legacy Perl Code - Peter Scott
How Xslate Works
Ad

Similar to Make Your Own Perl with Moops (20)

PDF
RubyConf Portugal 2014 - Why ruby must go!
KEY
plackdo, plack-like web interface on perl6
PPTX
Php extensions
PDF
Es.next
PDF
Php Crash Course - Macq Electronique 2010
PPTX
Php extensions
PPTX
Php extensions
PDF
Documenting with xcode
PDF
CDI In Real Life
PDF
Introduction to Writing Readable and Maintainable Perl (YAPC::EU 2011 Version)
PDF
The Ring programming language version 1.5.4 book - Part 181 of 185
PDF
Living With Legacy Code
KEY
Using PHP
PDF
Developing A Procedural Language For Postgre Sql
PPTX
PHP Basics and Demo HackU
PDF
"Managing API Complexity". Matthew Flaming, Temboo
PDF
The Ring programming language version 1.5.2 book - Part 177 of 181
PDF
Go(lang) for the Rubyist
PDF
2 BytesC++ course_2014_c12_ polymorphism
PPT
PHP-03-Functions.ppt
RubyConf Portugal 2014 - Why ruby must go!
plackdo, plack-like web interface on perl6
Php extensions
Es.next
Php Crash Course - Macq Electronique 2010
Php extensions
Php extensions
Documenting with xcode
CDI In Real Life
Introduction to Writing Readable and Maintainable Perl (YAPC::EU 2011 Version)
The Ring programming language version 1.5.4 book - Part 181 of 185
Living With Legacy Code
Using PHP
Developing A Procedural Language For Postgre Sql
PHP Basics and Demo HackU
"Managing API Complexity". Matthew Flaming, Temboo
The Ring programming language version 1.5.2 book - Part 177 of 181
Go(lang) for the Rubyist
2 BytesC++ course_2014_c12_ polymorphism
PHP-03-Functions.ppt
Ad

More from Mike Friedman (8)

PDF
Basic Symbolic Computation in Perl
PDF
21st Century CPAN Testing: CPANci
PPTX
Data Modeling for the Real World
PDF
CPANci: Continuous Integration for CPAN
PPTX
MongoDB Schema Design: Four Real-World Examples
PDF
Building a MongoDB App with Perl
PPTX
Building Your First App with MongoDB
KEY
Building Scalable, Distributed Job Queues with Redis and Redis::Client
Basic Symbolic Computation in Perl
21st Century CPAN Testing: CPANci
Data Modeling for the Real World
CPANci: Continuous Integration for CPAN
MongoDB Schema Design: Four Real-World Examples
Building a MongoDB App with Perl
Building Your First App with MongoDB
Building Scalable, Distributed Job Queues with Redis and Redis::Client

Recently uploaded (20)

PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Enable Enterprise-Ready Security on IBM i Systems.pdf
PDF
Smarter Business Operations Powered by IoT Remote Monitoring
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PDF
ai-archetype-understanding-the-personality-of-agentic-ai.pdf
PDF
solutions_manual_-_materials___processing_in_manufacturing__demargo_.pdf
PDF
Modernizing your data center with Dell and AMD
PDF
CIFDAQ's Teaching Thursday: Moving Averages Made Simple
PDF
How Onsite IT Support Drives Business Efficiency, Security, and Growth.pdf
PPTX
How to Build Crypto Derivative Exchanges from Scratch.pptx
PDF
Transforming Manufacturing operations through Intelligent Integrations
PDF
AI And Its Effect On The Evolving IT Sector In Australia - Elevate
PDF
Chapter 2 Digital Image Fundamentals.pdf
PDF
madgavkar20181017ppt McKinsey Presentation.pdf
PDF
HCSP-Presales-Campus Network Planning and Design V1.0 Training Material-Witho...
PPTX
Telecom Fraud Prevention Guide | Hyperlink InfoSystem
PDF
Top Generative AI Tools for Patent Drafting in 2025.pdf
PPTX
ABU RAUP TUGAS TIK kelas 8 hjhgjhgg.pptx
Chapter 3 Spatial Domain Image Processing.pdf
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Enable Enterprise-Ready Security on IBM i Systems.pdf
Smarter Business Operations Powered by IoT Remote Monitoring
NewMind AI Weekly Chronicles - August'25 Week I
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
ai-archetype-understanding-the-personality-of-agentic-ai.pdf
solutions_manual_-_materials___processing_in_manufacturing__demargo_.pdf
Modernizing your data center with Dell and AMD
CIFDAQ's Teaching Thursday: Moving Averages Made Simple
How Onsite IT Support Drives Business Efficiency, Security, and Growth.pdf
How to Build Crypto Derivative Exchanges from Scratch.pptx
Transforming Manufacturing operations through Intelligent Integrations
AI And Its Effect On The Evolving IT Sector In Australia - Elevate
Chapter 2 Digital Image Fundamentals.pdf
madgavkar20181017ppt McKinsey Presentation.pdf
HCSP-Presales-Campus Network Planning and Design V1.0 Training Material-Witho...
Telecom Fraud Prevention Guide | Hyperlink InfoSystem
Top Generative AI Tools for Patent Drafting in 2025.pdf
ABU RAUP TUGAS TIK kelas 8 hjhgjhgg.pptx

Make Your Own Perl with Moops