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 full-size slide

  2. #dammitcancer

    View full-size slide

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

    View full-size slide

  4. 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 full-size slide

  5. /ˈlɛɡəsɪ/

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

    View full-size slide

  6. /ˈlɛɡəsɪ/

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

    View full-size slide

  7. This talk is about

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  12. Classes,
    Methods,
    Attributes &
    Instances

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  18. Goals
    1. class attributes (slots)

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  23. 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 full-size slide

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

    View full-size slide

  25. 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 full-size slide

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

    View full-size slide

  27. 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 full-size slide

  28. 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 full-size slide

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

    View full-size slide

  30. 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 full-size slide

  31. 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 full-size slide

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

    View full-size slide

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

    View full-size slide

  34. 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 full-size slide

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

    View full-size slide

  36. Perl 5 Core
    Class::MOP
    Moose

    View full-size slide

  37. Perl 5 Core
    Class::MOP
    Moose

    View full-size slide

  38. Perl 5 Core
    mop.pm

    View full-size slide

  39. Perl 5 Core
    mop.pm

    View full-size slide

  40. Throw the first one away!

    View full-size slide

  41. PadWalker.pm

    View full-size slide

  42. PadWalker.pm

    View full-size slide

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

    View full-size slide

  44. 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 full-size slide

  45. p5-mop-redux

    View full-size slide

  46. Perl 5 Core
    mop.pm

    View full-size slide

  47. Perl 5 Core
    mop.pm

    View full-size slide

  48. 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 full-size slide

  49. 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 full-size slide

  50. 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 full-size slide

  51. 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 full-size slide

  52. 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 full-size slide

  53. 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 full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  57. 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 full-size slide

  58. • Plack
    • HTTP::Headers::ActionPack

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  66. Perl 5 Core
    mop.pm

    View full-size slide

  67. Perl 5 Core
    mop.pm

    View full-size slide

  68. Perl 5 Core
    mop.pm

    View full-size slide

  69. Perl 5 Core
    mop.pm

    View full-size slide

  70. Perl 5 Core
    mop.pm
    XS

    View full-size slide

  71. mop.xs
    Perl
    5
    Core

    View full-size slide

  72. p5-mop-again-seriously-wtf

    View full-size slide

  73. sub foo; # required method

    View full-size slide

  74. $ 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 full-size slide

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

    View full-size slide

  76. 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 full-size slide

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

    View full-size slide

  78. + 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 full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  83. #include “mop.h”

    View full-size slide

  84. Respect
    the
    Legacy!

    View full-size slide

  85. Legacy is the
    remnants of solved
    problems.

    View full-size slide

  86. 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 full-size slide

  87. Object Orientation in
    Ada is a Design Pattern

    View full-size slide

  88. What makes a good

    “Perl” programmer?
    • Laziness

    • Impatience

    • Hubris

    View full-size slide

  89. What makes a good

    “perl” programmer?
    • Do the hard work

    • Patience

    • Humility

    View full-size slide

  90. The End
    (or is it…)

    View full-size slide