Telephone +44(0)1524 64544
Email: info@shadowcat.co.uk

cia-oslo-2010 - perl5-like-ruby

Sat Dec 22 00:30:00 2012

Slides for the talk perl5-like-ruby at cia-oslo-2010

Perl5 - a bit
like ruby but
with a usable
object system

-

Matt S Trout
(mst)

-

Shadowcat Systems Limited
http://shadowcat.co.uk/

-

Lancaster,
North West
England

-

Catalyst
DBIx::Class
Moose

-

Commit bit
+ clue

-

ruby

-

META META
WHITE THING

-

Re-openable
classes

-

... wait

-

What's a
closed class
again?

-

  *MyClass::foo = sub {
    my $self = shift;
    $self->print("foo!");
  }

-

  use Method::Signatures::Simple;

-

  *MyClass::foo = method {
    $self->print("foo!");
  };

-

ruby

-

  attr_accessor :name

-

Class
method

-

Wait.

-

Class::Accessor

-

  __PACKAGE__->mk_accessors(
    'name'
  );

-

Moose

-

  has name => (is => 'rw');

-

ruby

-

  attr_reader :name

-

Moose

-

  has name => (is => 'ro');

-

ruby

-

  def initialize (name)
    @name = name
  end

-

Moose

-

erm ...

-

you have
to write
constructors?

-

CLOS got
this right

-

Moose gets
this right

-

Class::Accessor
got this right!

-

  MyClass->new(
    name => 'Bob'
  )

-

WTFF?

-

Moose::Object::new

-

JFDIs

-

how?

-

META META
WHITE THING

-

  my $meta = $class->meta

-

Moose::Meta::Class

-

  my @attr = $meta->get_all_attributes

-

  my $name = $attr->name;

-

  def initialize (params)
    @name = params[:nom_de_plume]
  end

-

  has name => (
    is => 'rw',
    init_arg => 'nom_de_plume'
  );

-

  MyClass->new(
    nom_de_plume => 'Bob'
  );

-

META META
WHITE THING

-

  my $init_arg = $attr->init_arg;

-

perl

-

Multiple
Inheritance

-

  use base qw(One Two);

-

Moose

-

  extends qw(One Two);

-

ruby

-

"You didn't
want to do
that, Dave"

-

... right.

-

but MI
is evil!

-

  use mro 'c3';

-

works for
python too

-

(actually, we
stole it from
them - thanks!)

-

  sub One::foo {
    ...
    $self->next::method(...)
  }

-

ruby

-

  def foo
    super
  end

-

Without
MI?

-

Moose::Role

-

  around foo => sub {
    ...
  };

-

ruby

-

  alias_method :old_foo, :foo

  def foo
    old_foo
  end

-

wait
what?

-

or create
an anon
class

-

wait
what?

-

Ok, ok

-

MI is a
philosophical
thingy

-

  role One {
    method foo {}
  }

-

  role Two {
    method foo {}
  }

-

  class Three
    with One
    with Two {
  }

-

"method name
conflict
in roles"

-

ruby?

-

Bueller?

-

Not to
mention ...

-

  has foo => (
    is => 'rw',
    isa => Str,
    lazy_build => 1,
  );

-

  $obj->foo; # calls $obj->_build_foo
  $obj->has_foo; # built yet?
  $obj->clear_foo; # rebuild

-

Wait

-

... Str

-

Oh noez
typing!

-

Can't have
that. Write
unit tests!

-

"Duck
typing"

-

Aka "we can't
overload isa"

-

... sigh

-

Delegation

-

ruby

-

  def method_missing

-

  sub AUTOLOAD

-

AIEEEEE

-

  has foo => (
    ...
    isa => 'Some::Class',
    handles => '*'
  );

-

(quack)

-

Not to mention
parameterized
roles

-

Not to mention
multimethods

-

Not to mention
pattern matching

-

(quack!)

-

-

I've been
picking on
ruby too
long now

-

So let's
finish with
rakudo

-

Though, well,
s{picking on}
 {blatant theft}

-

signatures.pm!

-

  sub greet ($name) {
    say "Hello ${name}"
  }

-

Moose::Autobox!

-

  %hash->each(sub ($k, $v) {
    ...
  });

-

Perl6::Junction!
(cheating? :)

-

  any(@a) >= 60
  none(@b) >= 30

-

MooseX::Declare!

-

  method greet (
    Str $name,
    Int :$age where { $_ > 0 },
    Bool :$excited = 0
  ) {

-

autobox::DateTime::Duration!

-

  7->days->ago

-

... no wait
which language
was I picking
on again?

-

So if I've
offended
the rakudoists
and the rubyists

-

Any Questions?
Any Heckles?
http://shadowcat.co.uk/

-

Thank You
http://shadowcat.co.uk/