make your own perl with moops

60
flickr: philip66 Make your own Perl with Moops @friedo Mike Friedman London Perl Workshop 2014

Upload: mike-friedman

Post on 04-Jul-2015

261 views

Category:

Technology


1 download

DESCRIPTION

Use Moops to create a customized Perl environment with your own syntax extensions.

TRANSCRIPT

Page 1: Make Your Own Perl with Moops

flickr: philip66

Make your own Perl

with

Moop s

@friedo

Mike Friedman London Perl Workshop 2014

Page 2: Make Your Own Perl with Moops

A brief history…

Page 3: Make Your Own Perl with Moops

A brief history…

In the Beginning, Perl 5

was already extensible.

Page 4: Make Your Own Perl with Moops

use Foo qw(bar);

Page 5: Make Your Own Perl with Moops

use Foo qw(bar);

BEGIN {! require Foo;! Foo->import( ‘bar’ );!};

Page 6: Make Your Own Perl with Moops

How does !

import!!

actually work?

Page 7: Make Your Own Perl with Moops

sub import {! my $pkg = shift;! ...

a typical import sub

Page 8: Make Your Own Perl with Moops

sub import {! my $pkg = shift;! my $callpkg = caller;! ...

a typical import sub

Page 9: Make Your Own Perl with Moops

sub import {! my $pkg = shift;! my $callpkg = caller;! my @imports = @_;! ...

a typical import sub

Page 10: Make Your Own Perl with Moops

sub import {! ... ! foreach ! my $sym(@imports) {! *{"${callpkg}::$sym"}! = *{"${pkg}::$sym"}

a typical import sub

Page 11: Make Your Own Perl with Moops

(many details omitted.)

Page 12: Make Your Own Perl with Moops

A brief history…

import is compile-time.

Page 13: Make Your Own Perl with Moops

A brief history…

import is compile-time.

1. add functions. 2. pretend they’re keywords.

Page 14: Make Your Own Perl with Moops

package Adder;!!

sub import { ... }!sub add2 { ! return shift + 2;!}

a pretend keyword

Page 15: Make Your Own Perl with Moops

package Mine;!!

use Adder;!!

say add2 42;

a pretend keyword

Page 16: Make Your Own Perl with Moops

A brief history…

Perl 5.2 !

Prototypes

Page 17: Make Your Own Perl with Moops

Make fake keywords !

That behave like builtins

Page 18: Make Your Own Perl with Moops

(kinda)

Page 19: Make Your Own Perl with Moops

sub doblock(&@) { ! my $blk = shift;! return $blk->( @_ );!}!!

doblock { ! say $_ * $_ for @_;!} ( 1, 2, 3, 4, 5 );!

a prototype pretend keyword

Page 20: Make Your Own Perl with Moops

sub doblock(&) { ... }!!

doblock { ! say "first";!}!!

say "second";

a prototype pretend keyword

Page 21: Make Your Own Perl with Moops

sub doblock(&) { ... }!!

doblock { ! say "first";!}!!

say "second";

a prototype pretend keyword

# Fail :(

Page 22: Make Your Own Perl with Moops

Prototype subs only work as expressions.

!

Not statements.

Page 23: Make Your Own Perl with Moops

sub doblock($&) { ... }!!

doblock name { ! say "first";!}!!

a prototype pretend keyword

# Fail :(

Page 24: Make Your Own Perl with Moops

a prototype pretend keyword

# We have to say:

Page 25: Make Your Own Perl with Moops

!

!

doblock name => sub { ! say "first";!};!!

a prototype pretend keyword

# We have to say:

Page 26: Make Your Own Perl with Moops

"&" prototypes are only magical if they're first.

Page 27: Make Your Own Perl with Moops

A brief history…

Perl 5.8 !

Devel::Declare

Page 28: Make Your Own Perl with Moops

The first real way to add !

new syntax.

Page 29: Make Your Own Perl with Moops

use MooseX::Declare;!!

class Foo { ! method bar { ... }! ...!}!!

a parser magic keyword

Page 30: Make Your Own Perl with Moops

Devel::Declare is complicated

Page 31: Make Your Own Perl with Moops

Devel::Declare is complicated

•Declarators?

Page 32: Make Your Own Perl with Moops

Devel::Declare is complicated

•Declarators?•Scope injectors?

Page 33: Make Your Own Perl with Moops

Devel::Declare is complicated

•Declarators?•Scope injectors?•Method shadowing?

Page 34: Make Your Own Perl with Moops

Devel::Declare is complicated

•Declarators?•Scope injectors?•Method shadowing?•WTF?

Page 35: Make Your Own Perl with Moops

MooseX::Declare is big, slow, and difficult to

extend.

Page 36: Make Your Own Perl with Moops

A brief history…

Perl 5.12 !

The keyword API

Page 37: Make Your Own Perl with Moops

Real keyword interface in the

Perl core API.

Page 38: Make Your Own Perl with Moops

package My::Keyword;!use Keyword::Simple;!!

sub import { ! Keyword::Simple::define( ! class => sub { ! munge_code( $$_[0] );! }!);!

a real custom keyword

Page 39: Make Your Own Perl with Moops

So... !

what is !

M o o p s ?

Page 40: Make Your Own Perl with Moops

use Moops;!role NamedThing {! has name => ! (is => "ro", isa => Str);!}! !class Person with NamedThing;!...

a Moops-defined class

Page 41: Make Your Own Perl with Moops

So it's like MooseX::Declare

?

Page 42: Make Your Own Perl with Moops

Not exactly.

Page 43: Make Your Own Perl with Moops

Not exactly.

•Moo by default, but can use Moose

Page 44: Make Your Own Perl with Moops

Not exactly.

•Moo by default, but can use Moose•Uses Keyword::Simple for new syntax.

Page 45: Make Your Own Perl with Moops

Not exactly.

•Moo by default, but can use Moose•Uses Keyword::Simple for new syntax.•Uses Kavorka for functions/methods

Page 46: Make Your Own Perl with Moops

Not exactly.

•Moo by default, but can use Moose•Uses Keyword::Simple for new syntax.•Uses Kavorka for functions/methods •Designed for easy extensibility

Page 47: Make Your Own Perl with Moops

A brief history…

One year ago, !

I did this:

Page 48: Make Your Own Perl with Moops

I like imports

Page 49: Make Your Own Perl with Moops

I hate boilerplate

Page 50: Make Your Own Perl with Moops

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

Page 51: Make Your Own Perl with Moops

!

!

sub import { ! my $callpkg = caller;! Scalar::Util->import::into(! $callpkg, 'blessed', 'refaddr'! );! List::Util->import::into(! $callpkg, 'reduce', 'all', ...! );! ...!}

Custom setup class

Page 52: Make Your Own Perl with Moops

Works pretty great!

Page 53: Make Your Own Perl with Moops

Unless I want to use custom syntax

Page 54: Make Your Own Perl with Moops

use MooseX::Declare; !# (or Moops)!!

class MyApp::Thing { ! use MyApp::Setup;!}!!

# :/!Custom setup class

Page 55: Make Your Own Perl with Moops

I have to use the Setup class from within

the right namespace.

Page 56: Make Your Own Perl with Moops

What if the Setup class could also inject custom

syntax?

Page 57: Make Your Own Perl with Moops

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

Page 58: Make Your Own Perl with Moops

sub import {! ...! $pkg->SUPER::import(! imports => \@imports! );!}!!

!

# :D

Moops-based setup class

Page 59: Make Your Own Perl with Moops

Cool!

Page 60: Make Your Own Perl with Moops

flickr: philip66

Questions

@friedo

Mike Friedman London Perl Workshop 2014