Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Perl's Syntactic Legacy

Perl's Syntactic Legacy

This is a talk I gave at YAPC::NA 2015 in Salt Lake City (http://www.yapcna.org/yn2015/talk/6085). The video can be found here: https://www.youtube.com/watch?v=sJC725e8ysM

Stevan Little

June 10, 2015
Tweet

More Decks by Stevan Little

Other Decks in Programming

Transcript

  1. Perl's
    Syntactic
    Legacy
    Using the future to improve the past

    View Slide

  2. View Slide

  3. View Slide

  4. View Slide

  5. View Slide

  6. #eatfirst

    View Slide

  7. View Slide

  8. View Slide

  9. $785.00!!!!

    View Slide

  10. View Slide

  11. #dammitcancer

    View Slide

  12. Perl's
    Syntactic
    Legacy
    Using the future to improve the past

    View Slide

  13. Legacy
    /ˈlɛɡəsɪ/

    !
    adjective
    !
    of or relating to old or outdated computer
    hardware, software, or data that, while
    still functional, does not work well with
    up-to-date systems.

    View Slide

  14. /ˈlɛɡəsɪ/

    !
    noun
    !
    something handed down or received
    from an ancestor or predecessor
    Legacy

    View Slide

  15. /ˈlɛɡəsɪ/

    !
    noun
    !
    something handed down or received
    from an ancestor or predecessor
    Legacy
    descendant or successor

    View Slide

  16. This talk is about

    View Slide

  17. This talk is about
    ‣ the evolution of a design

    View Slide

  18. This talk is about
    ‣ the evolution of a design
    ‣ the slow breakdown of
    my own stubbornness

    View Slide

  19. Learning

    View Slide

  20. Learning
    The act of present $you realizing
    how stupid past $you was being

    View Slide

  21. meta(data)

    View Slide

  22. STEVAN
    [email protected]
    stevan
    @stevanlittle
    cpan:
    email:
    github:
    twitter:

    View Slide

  23. 1998

    View Slide

  24. 2002

    View Slide

  25. 2004

    View Slide

  26. 2005

    View Slide

  27. 2006

    View Slide

  28. 2011

    View Slide

  29. mop?

    View Slide

  30. Classes,
    Methods,
    Attributes &
    Instances

    View Slide

  31. ... an abstraction of a
    system of abstractions
    that is used to build
    abstractions.

    View Slide

  32. ... an abstraction of a
    system of abstractions
    that is used to build
    abstractions.

    View Slide

  33. ... an abstraction of a
    system of abstractions
    that is used to build
    abstractions.

    View Slide

  34. ... an abstraction of a
    system of abstractions
    that is used to build
    abstractions.

    View Slide

  35. *{$pkg . '::foo'} = \&bar;

    View Slide

  36. p5-mop

    View Slide

  37. Goals

    View Slide

  38. Goals
    1. class attributes (slots)

    View Slide

  39. Goals
    1. class attributes (slots)
    2. class/method vs. package/sub

    View Slide

  40. Goals
    1. class attributes (slots)
    2. class/method vs. package/sub
    3. role composition

    View Slide

  41. Goals
    1. class attributes (slots)
    2. class/method vs. package/sub
    3. role composition
    4. introspection API

    View Slide

  42. Goals
    1. class attributes (slots)
    2. class/method vs. package/sub
    3. role composition
    4. introspection API
    5. improve OO syntax

    View Slide

  43. package Point;
    !
    sub new {
    my $class = shift;
    bless { x => 0, y => 0 }, $class;
    }
    !
    sub clear {
    my $self = shift;
    $self->{x} = 0;
    $self->{y} = 0;
    }

    View Slide

  44. class Point {
    !
    has $x = 0;
    has $y = 0;
    !
    method clear {
    ($x, $y) = (0, 0);
    }
    }

    View Slide

  45. package Point;
    !
    sub new {
    my $class = shift;
    bless { x => 0, y => 0 }, $class;
    }
    !
    sub clear {
    my $self = shift;
    $self->{x} = 0;
    $self->{y} = 0;
    }

    View Slide

  46. class Point {
    !
    has $x = 0;
    has $y = 0;
    !
    method clear {
    ($x, $y) = (0, 0);
    }
    }

    View Slide

  47. package Point;
    !
    sub new {
    my $class = shift;
    bless { x => 0, y => 0 }, $class;
    }
    !
    sub clear {
    my $self = shift;
    $self->{x} = 0;
    $self->{y} = 0;
    }

    View Slide

  48. package Point;
    !
    sub new :method {
    my $class = shift;
    bless { x => 0, y => 0 }, $class;
    }
    !
    sub clear :method {
    my $self = shift;
    $self->{x} = 0;
    $self->{y} = 0;
    }

    View Slide

  49. class Point {
    !
    has $x = 0;
    has $y = 0;
    !
    method clear {
    ($x, $y) = (0, 0);
    }
    }

    View Slide

  50. package Point;
    !
    sub new {
    my $class = shift;
    bless { x => 0, y => 0 }, $class;
    }
    !
    sub clear {
    my $self = shift;
    $self->{x} = 0;
    $self->{y} = 0;
    }

    View Slide

  51. package Point;
    !
    sub new {
    my $class = shift;
    bless { x => 0, y => 0 }, $class;
    }
    !
    sub clear {
    my $self = shift;
    $self->{x} = 0;
    $self->{y} = 0;
    }

    View Slide

  52. class Point {
    !
    has $x = 0;
    has $y = 0;
    !
    method clear {
    ($x, $y) = (0, 0);
    }
    }

    View Slide

  53. class Point {
    !
    has $x = 0;
    has $y = 0;
    !
    method clear {
    ($x, $y) = (0, 0);
    }
    }

    View Slide

  54. package Point;
    !
    sub new {
    my $class = shift;
    bless { x => 0, y => 0 }, $class;
    }
    !
    sub clear {
    my $self = shift;
    $self->{x} = 0;
    $self->{y} = 0;
    }

    View Slide

  55. class Point {
    !
    has $x = 0;
    has $y = 0;
    !
    method clear {
    ($x, $y) = (0, 0);
    }
    }

    View Slide

  56. Moose

    View Slide

  57. Perl 5 Core
    Class::MOP
    Moose

    View Slide

  58. Perl 5 Core
    Class::MOP
    Moose

    View Slide

  59. p5-mop

    View Slide

  60. Perl 5 Core
    mop.pm

    View Slide

  61. Perl 5 Core
    mop.pm

    View Slide

  62. View Slide

  63. View Slide

  64. View Slide

  65. View Slide

  66. Throw the first one away!

    View Slide

  67. PadWalker.pm

    View Slide

  68. PadWalker.pm

    View Slide

  69. View Slide

  70. Who is going to maintain this?!!!?!?!

    View Slide

  71. moe

    View Slide

  72. View Slide

  73. -Ofun

    View Slide

  74. ಠ_ಠ

    View Slide

  75. My Moe Wishlist
    • A more consistent syntax and sane grammar

    • with a real AST

    • and better tooling support

    • A less insane runtime

    • proper MOP (everything is an object)

    • slimmer core

    • easy (non-XS) extension mechanism

    • On a modern VM platform

    • JVM / CLR / LLVM / V8

    • cross language sharing

    View Slide

  76. CPAN

    View Slide

  77. View Slide

  78. View Slide

  79. View Slide

  80. View Slide

  81. View Slide

  82. View Slide

  83. View Slide

  84. p5-mop-redux

    View Slide

  85. Perl 5 Core
    mop.pm

    View Slide

  86. Perl 5 Core
    mop.pm

    View Slide

  87. package Foo;
    use strict;
    use warnings;
    !
    use Variable::Magic qw[ wizard cast ];
    use Hash::Util::FieldHash qw[ fieldhash ];
    !
    fieldhash my %foo;
    !
    my $wiz = wizard(
    data => sub { $_[1] },
    set => sub { $_[1]->[0]->{ $_[1]->[1] } = $_[0] },
    );
    !
    sub foo {
    my $self = shift;
    my $foo = $foo{$self};
    cast $foo, $wiz, [ \%foo, $self ];
    !
    $foo = shift if @_;
    $foo;
    }

    View Slide

  88. package Foo;
    use strict;
    use warnings;
    !
    use Variable::Magic qw[ wizard cast ];
    use Hash::Util::FieldHash qw[ fieldhash ];
    !
    fieldhash my %foo;
    !
    my $wiz = wizard(
    data => sub { $_[1] },
    set => sub { $_[1]->[0]->{ $_[1]->[1] } = $_[0] },
    );
    !
    sub foo {
    my $self = shift;
    my $foo = $foo{$self};
    cast $foo, $wiz, [ \%foo, $self ];
    !
    $foo = shift if @_;
    $foo;
    }

    View Slide

  89. package Foo;
    use strict;
    use warnings;
    !
    use Variable::Magic qw[ wizard cast ];
    use Hash::Util::FieldHash qw[ fieldhash ];
    !
    fieldhash my %foo;
    !
    my $wiz = wizard(
    data => sub { $_[1] },
    set => sub { $_[1]->[0]->{ $_[1]->[1] } = $_[0] },
    );
    !
    sub foo {
    my $self = shift;
    my $foo = $foo{$self};
    cast $foo, $wiz, [ \%foo, $self ];
    !
    $foo = shift if @_;
    $foo;
    }

    View Slide

  90. package Foo;
    use strict;
    use warnings;
    !
    use Variable::Magic qw[ wizard cast ];
    use Hash::Util::FieldHash qw[ fieldhash ];
    !
    fieldhash my %foo;
    !
    my $wiz = wizard(
    data => sub { $_[1] },
    set => sub { $_[1]->[0]->{ $_[1]->[1] } = $_[0] },
    );
    !
    sub foo {
    my $self = shift;
    my $foo = $foo{$self};
    cast $foo, $wiz, [ \%foo, $self ];
    !
    $foo = shift if @_;
    $foo;
    }

    View Slide

  91. package Foo;
    use strict;
    use warnings;
    !
    use Variable::Magic qw[ wizard cast ];
    use Hash::Util::FieldHash qw[ fieldhash ];
    !
    fieldhash my %foo;
    !
    my $wiz = wizard(
    data => sub { $_[1] },
    set => sub { $_[1]->[0]->{ $_[1]->[1] } = $_[0] },
    );
    !
    sub foo {
    my $self = shift;
    my $foo = $foo{$self};
    cast $foo, $wiz, [ \%foo, $self ];
    !
    $foo = shift if @_;
    $foo;
    }

    View Slide

  92. package Foo;
    use strict;
    use warnings;
    !
    use Variable::Magic qw[ wizard cast ];
    use Hash::Util::FieldHash qw[ fieldhash ];
    !
    fieldhash my %foo;
    !
    my $wiz = wizard(
    data => sub { $_[1] },
    set => sub { $_[1]->[0]->{ $_[1]->[1] } = $_[0] },
    );
    !
    sub foo {
    my $self = shift;
    my $foo = $foo{$self};
    cast $foo, $wiz, [ \%foo, $self ];
    !
    $foo = shift if @_;
    $foo;
    }

    View Slide

  93. View Slide

  94. package Foo;
    use strict;
    use warnings;
    !
    has ‘foo’;
    !
    method foo {
    $foo = shift if @_;
    $foo;
    }

    View Slide

  95. Devel::(every day i’m segfaultin)::Declare

    View Slide

  96. View Slide

  97. Everything should be as
    simple as it can be, but
    not simpler.
    – Albert Einstein

    View Slide

  98. It can scarcely be denied that the
    supreme goal of all theory is to
    make the irreducible basic elements
    as simple and as few as possible
    without having to surrender the
    adequate representation of a single
    datum of experience.

    View Slide

  99. View Slide

  100. • Plack

    View Slide

  101. • Plack
    • HTTP::Headers::ActionPack

    View Slide

  102. • Plack
    • HTTP::Headers::ActionPack
    • Promises

    View Slide

  103. • Plack
    • HTTP::Headers::ActionPack
    • Promises
    • Bread::Board

    View Slide

  104. • Plack
    • HTTP::Headers::ActionPack
    • Promises
    • Bread::Board
    • Reply

    View Slide

  105. • Plack
    • HTTP::Headers::ActionPack
    • Promises
    • Bread::Board
    • Reply
    • Forward::Routes

    View Slide

  106. • Plack
    • HTTP::Headers::ActionPack
    • Promises
    • Bread::Board
    • Reply
    • Forward::Routes
    • Action::Retry

    View Slide

  107. • Plack
    • HTTP::Headers::ActionPack
    • Promises
    • Bread::Board
    • Reply
    • Forward::Routes
    • Action::Retry
    • Hashids

    View Slide

  108. • Plack
    • HTTP::Headers::ActionPack
    • Promises
    • Bread::Board
    • Reply
    • Forward::Routes
    • Action::Retry
    • Hashids
    • Net::IPAddress::Util

    View Slide

  109. View Slide

  110. 200 OK

    View Slide

  111. Perl 5 Core
    mop.pm

    View Slide

  112. Perl 5 Core
    mop.pm

    View Slide

  113. Perl 5 Core
    mop.pm

    View Slide

  114. Perl 5 Core
    mop.pm

    View Slide

  115. Perl 5 Core
    mop.pm
    XS

    View Slide

  116. View Slide

  117. p5-mop-XS

    View Slide

  118. View Slide

  119. View Slide

  120. Perl 5 Core

    View Slide

  121. mop.xs
    Perl
    5
    Core

    View Slide

  122. p5-mop-again-seriously-wtf

    View Slide

  123. View Slide

  124. View Slide

  125. sub foo; # required method

    View Slide

  126. $ perl -MDevel::Peek -e 'sub foo; Dump \&foo;'
    SV = IV(0x7fa3b3003428) at 0x7fa3b3003438
    REFCNT = 1
    FLAGS = (TEMP,ROK)
    RV = 0x7fa3b3028978
    SV = PVCV(0x7fa3b30271b8) at 0x7fa3b3028978
    REFCNT = 2
    FLAGS = ()
    COMP_STASH = 0x7fa3b30032b8 "main"
    ROOT = 0x0
    GVGV::GV = 0x7fa3b3028960 "main" :: "foo"
    FILE = "-e"
    DEPTH = 0
    FLAGS = 0x0
    OUTSIDE_SEQ = 0
    PADLIST = 0x0
    OUTSIDE = 0x0 (null)

    View Slide

  127. # marking sub as method
    sub foo :method { … }

    View Slide

  128. perl -MDevel::Peek -e 'sub foo :method { 1 } Dump \&foo;'
    SV = IV(0x7fec82003428) at 0x7fec82003438
    REFCNT = 1
    FLAGS = (TEMP,ROK)
    RV = 0x7fec82029318
    SV = PVCV(0x7fec82027bb8) at 0x7fec82029318
    REFCNT = 2
    FLAGS = (METHOD)
    COMP_STASH = 0x7fec820032b8 "main"
    START = 0x7fec81c0dae8 ===> 1
    ROOT = 0x7fec81c0da60
    GVGV::GV = 0x7fec82029348 "main" :: "foo"
    FILE = "-e"
    DEPTH = 0
    FLAGS = 0x1
    OUTSIDE_SEQ = 95
    PADLIST = 0x7fec81c0cac0
    PADNAME = 0x7fec82029378(0x7fec81c0cf90)
    PAD = 0x7fec820293c0(0x7fec81c051e0)
    OUTSIDE = 0x7fec82003648 (MAIN)

    View Slide

  129. # signatures
    sub foo ($self) { … }

    View Slide

  130. + use v5.20;
    + use experimental ‘signatures’
    +
    - SV*
    - parse_signature (code, …)
    - # A WHOLE LOAD OF CRAZY XS
    - # PARSING CODE THAT MADE YOUR
    - # EYES BLEED AND YOUR HEART
    - # SINK IN DEEP DESPAIR
    - # *sigh*

    View Slide

  131. sub foo;
    !
    sub bar :method { … }
    !
    sub baz ($gorch) { … }

    View Slide

  132. package Foo;
    use v5.20;
    use warnings;
    !
    our @DOES = (‘BarRole’);
    !
    1;

    View Slide

  133. package Foo;
    use v5.20;
    use warnings;
    !
    our %HAS = (
    bar => sub { “BAR” },
    baz => sub { “BAZ” },
    );
    !
    1;

    View Slide

  134. our @DOES = (‘BarRole’);
    !
    our %HAS = (
    bar => sub { “BAR” },
    baz => sub { “BAZ” },
    );

    View Slide

  135. #include “mop.h”

    View Slide

  136. p5-mop

    View Slide

  137. Conclusion

    View Slide

  138. Respect
    the
    Legacy!

    View Slide

  139. Legacy is the
    remnants of solved
    problems.

    View Slide

  140. Many of the OO conventions
    that we take for granted are
    largely inherited from Java,
    !
    … which barely existed
    when Larry designed the
    OO features of Perl 5.

    View Slide

  141. Object Orientation in
    Ada is a Design Pattern

    View Slide

  142. What makes a good

    “Perl” programmer?
    • Laziness

    • Impatience

    • Hubris

    View Slide

  143. What makes a good

    “perl” programmer?
    • Do the hard work

    • Patience

    • Humility

    View Slide

  144. The End
    (or is it…)

    View Slide