[AnnArbor.pm] Moose: errata and follow-up

Bryan Smith besmit at umich.edu
Thu Jan 27 06:59:12 PST 2011


1. CORRECTION: I made a mistake when discussing the attributes. I had said
that the default is "read/write", and that omiting the 'is' option for an
attribute would mean it is read/write.

But by default the attribute has no accessors (which you can confirm by
setting 'is' to 'bare').

http://search.cpan.org/~drolsky/Moose-1.21/lib/Moose/Manual/Attributes.pod#Read-write_vs._read-only

So:

has 'val1' => ( isa => 'Str' , is => 'rw' ); # You need the 'is' if you want
read-write accessors
has 'val2' => ( isa => 'Num' , is => 'rw' ); # You need the 'is' if you want
read-write accessors

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - -

2. In the around method modifier, the $orig argument below is a code ref:

around 'to_html' => sub {
  my $orig = shift;
  my $self = shift;

  print "ref \$orig : " . ref( $orig ) . "\n";

  print '<strong>';
  $self->$orig;
  print '</strong>';
};

~ ~ ~

bryan at bryan-laptop:~/public_html_bryanesmith/demos/perl/oo$
./strong-paragraph.plx
<p>ref $orig : CODE
<strong>Hello, World!</strong></p>

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - -

3. I did some simple performance testing with Moose and blessed hashes. (The
code will be posted at end of email).

The results show drastic differences between Moose with and without
immutability and between Moose and old-style.

# Moose
bryan at bryan-laptop:~/public_html_bryanesmith/demos/perl/oo$
./benchmark-oo.plx
timethis 100000: 22 wallclock secs (22.36 usr +  0.01 sys = 22.37 CPU) @
4470.27/s (n=100000)

# Moose, immutable
bryan at bryan-laptop:~/public_html_bryanesmith/demos/perl/oo$
./benchmark-oo.plx
timethis 100000:  2 wallclock secs ( 2.47 usr +  0.00 sys =  2.47 CPU) @
40485.83/s (n=100000)

# blessed hash
bryan at bryan-laptop:~/public_html_bryanesmith/demos/perl/oo$
./benchmark-old-oo.plx
timethis 100000:  0 wallclock secs ( 0.89 usr +  0.00 sys =  0.89 CPU) @
112359.55/s (n=100000)

The methodology was crude (no hypothesis except that there might be a time
difference), and I'd like to revisit this. Also, I want to know about memory
as well.

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - -

4. Thanks for the info, Chris:

http://search.cpan.org/~drolsky/Moose-1.21/lib/Moose/Meta/Attribute/Native.pm

This is certainly cleaner, and I will be using it.


Cheers,
Bryan

P.S.,

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - -
Moose
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - -
#!/usr/bin/perl

package Test;
use Moose;

has 'val1' => ( isa => 'Str' , is => 'rw' );
has 'val2' => ( isa => 'Num' , is => 'rw' );

package main;
use Benchmark qw(:all) ;

sub testMoose {
  my $val1 = rand( 1000000 );
  my $val2 = rand( 1000000 );

  my $test = Test->new( 'val1' => $val1, 'val2' => $val2 );
  die "Wrong val" unless $val1 == $test->val1;
  die "Wrong val" unless $val2 == $test->val2;
}

timethis ( 100000, "testMoose");

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - -
Moose (immutable)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - -
#!/usr/bin/perl

package Test;
use Moose;

has 'val1' => ( isa => 'Str' , is => 'rw' );
has 'val2' => ( isa => 'Num' , is => 'rw' );

no Moose;
__PACKAGE__->meta->make_immutable;

package main;
use Benchmark qw(:all) ;

sub testMoose {
  my $val1 = rand( 1000000 );
  my $val2 = rand( 1000000 );

  my $test = Test->new( 'val1' => $val1, 'val2' => $val2 );
  die "Wrong val" unless $val1 == $test->val1;
  die "Wrong val" unless $val2 == $test->val2;
}

timethis ( 100000, "testMoose");

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - -
Non-moose
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - -
#!/usr/bin/perl

package Test;

sub new {
  my $class = shift;
  my $self = { _val1 => shift, _val2 => shift };
  bless $self, $class;
  return $self;
}

sub val1 {
  my $self = shift;
  return $self->{ _val1 };
}

sub val2 {
  my $self = shift;
  return $self->{ _val2 };
}

package main;
use Benchmark qw(:all) ;

sub testMoose {
  my $val1 = rand( 1000000 );
  my $val2 = rand( 1000000 );

  my $test = Test->new( $val1, $val2 );
  die "Wrong val" unless $val1 == $test->val1;
  die "Wrong val" unless $val2 == $test->val2;
}

timethis ( 100000, "testMoose");
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/pipermail/annarbor-pm/attachments/20110127/8467a857/attachment.html>


More information about the AnnArbor-pm mailing list