File: lexical.pl

package info (click to toggle)
libclass-methodmaker-perl 2.25-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 524 kB
  • sloc: perl: 1,849; objc: 492; makefile: 3
file content (47 lines) | stat: -rw-r--r-- 937 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
use Benchmark qw(cmpthese);

package Foo;

sub new { bless {slot => "blot"}, shift }

sub getset_orig {
   my $self = shift;
   if (@_) {
       $self->{slot} = shift;
   } else {
       $self->{slot};
   }
}

sub getset_fast {
    return $_[0]->{slot} if @_ == 1;
    return $_[0]->{slot} = $_[1];
}

# lvalue doesn't play nicely with return :-(
sub getset_lvalue {
  if ( @_ == 1 ) {
    $_[0]->{slot};
  } else {
    $_[0]->{slot} = $_[1];
  }
}

package main;

my $obj = Foo->new();

cmpthese(-2, {
              getset_orig => sub {
                  $_ = $obj->getset_orig();
                  $obj->getset_orig($_);
              },
              getset_fast => sub {
                  $_ = $obj->getset_fast();
                  $obj->getset_fast($_);
              },
              getset_lvalue => sub {
                  $_ = $obj->getset_lvalue();
                  $obj->getset_lvalue($_);
              },
             });