SlideShare une entreprise Scribd logo
1  sur  32
Télécharger pour lire hors ligne
Perl Object ?
SLC.pm October 2015 Perl Object Nicolas Rochelemagne
Perl Object ?
•Why ?
•When ?
•How ?
Without objects
we manipulate scalars, arrays, hashes...
$hash = {
        key => 42,
        another => {
                one  => 43,
                deep => {
                        here => {
                                you => {
                                        can => 'find me'
                                }
                        }
                },
                str  => "Data value"
        },
        elts => [ 8, 25, 37, 64 ]
};
this lead to store data in /ugly/ hashes
Problems with this approach :
you need to know the "exact key" to use...
$hash->{key} = 42;
$hash->{anotherlevel}->{key} = "value";
$hash->{where}->{can}{be}{mykey} = { it => 's there' };
deep research into the code...
or need to dump huge hash
You can s/confound/mispell/ key name
somewhere
sub method {
        my $h = shift;
        $h->{key} = 42;
}
then later
...
        $h->{keys}++ and $h->{kyes} = 51;
...
You cannot change the key name
or storage level
cannot become neither
$hash->{current_key} = 42;
nor
$hash->{updated_key} = 42;
$hash->{better}{key} = 42;
without updating
the full code !
You cannot control life cycle of an attribute :
sub somewhere {
        ...
        $hash->{mykey} = 42 and additionnal_operation;
        ...
}
sub otherplace {
        ...
        $hash->{mykey} = 43 and additionnal_operation;
        ...
}
•on create
•on update
•before, after, around
•hooks : innner, augment...
without objects
You would need an accessor
it should be the only single way

to access to this piece of data
sub mykey {
        my ($self, $value) = @_;
        if (defined $value) {
                $self->{_mykey} = $value;
                $self->additionnal_operation();
        }
       
        $self->{_mykey};
}
Writing documentation
=pod
=head attribute
Parameters : attribute value ( optional )
Description : attribute method can be used to read or update it !
Returns : current value
Usage :
        # use as reader
        my $read = $object->attribute();
        # use as writter
        $object->attribute($new_value);
=cut
        sub attribute {
                ... some code there ( or not ! )
        }
... is difficult
rather than
Please use $object->{level1}{level2} to access the expected value !
# at this time, and it could never change ?
What is an object ?
•functions
•data
Small piece of code
Why object / module ?
•code is organized
•Object is DRY, Lazy & Fun
•code is easier to read / share
•design & development can be dissociate
•easier to test
•provides fault containment
•reduce code maintenance
•simplify product evolution
•documentation is easier
Code organization
Object
|__ Human
| |___ Male
| |___ Female
|
|__ Transport
|___ Car
|___ Plane
|___ Bicycle
#!perl
package Object::Transport::Car;
DRY : Do not Repeat Yourself
 package Document::Page;
  sub create {
      my $self = shift;
      ...  
      do_some_stuff; # inner();
      ...
  }
  package Document::Special;
  extends 'Document::Page';
  augment 'create' => sub {
      my $self = shift;
      $self->do_some_extra_stuff;
  };
•inheritance help you factorize your code
•roles ( ~ interface )
Lazy
package Person;
use Moose; # or any other object module
has 'age'    => ( is => 'rw', isa => 'Int', default => 42 );
 
package main;
my $p = Person->new() or Person->new(age => 64);
$current_age = $p->age();
$p->age($new_age);
•provide "new" method
•provide accessors
•parameters validation :

types, optional, default value...
Type checking
 package Person;
  use Try::Tiny;
  use Moose;
  use Moose::Util::TypeConstraints;
  subtype 'Sex'
      => as 'Str'
      => where { $_ =~ m{^[mf]$}s };
  has 'sex'    => ( is => 'ro', isa => 'Sex', required =>  1 );
  has 'age'    => ( is => 'rw', isa => 'Int', default  => 42 );  
  my $person = Person->new( sex => 'm', age => 45 );
  try {
        Person->new( sex => 'unknown' );
  } catch {
         warn "Error has been detected";
  };
Coercion
package My::Types::Date;
use Moose::Util::TypeConstraints; use DateTime; # ...
subtype 'MyType:Day' => as 'DateTime';
coerce  'MyType:Day' => from 'Str' => via {
    /^d{4}d{2}d{2}$/ or croak "Unable to coerce '$_' into a valid date";
    return DateTime::Format::DateParse->parse_datetime($_);
};
package Test::Coerce;
use Moose; use My::Types::Date;
has day => ( is  => 'rw', isa => 'MyType:Day',
        coerce => 1);
package main;
my $d1 = Test::Coerce->new(day => DateTime->now());
my $d2 = Test::Coerce->new(day => '20111130');
isa_ok $d2->day, 'DateTime', 'day is coerced to a DateTime object';
Which object library to choose ?
•Moose
•Mouse
•Moo
•Mo
•M
•...
•fields
•Object::Tiny(::XS)
•Class::XSAccessor
•write your own simple Object ?
and sometimes none
•the most advanced
•large ecosystem of extensions : 

MooseX : validate, getopt, singleton,
types...
•can use advanced types and coercion
methods
•hooks : before, after, inner, augment...
•startup time
•memory usage
•dependencies...
Moose : the fat one !
fat but so good...
Advantages :
Disadvantages :
•same goal as Mouse : provide a Moose lighter
•but provide "as little as possible" : minimalist
Moo : the light one !
package Demo::Moo;
use Moo;
with 'Some::Role';
has bar => (
        is => 'rw',
        isa => sub { $_[0] =~ /^[+-]?d+$/ },
        coerce => quote_sub q{ $_[0] + 1 unless $_[0] % 2 },
        lazy => 1,
);
•can use only one role at a time
•not available : super, override, inner, augment
•no initializer
Start with Moo and if you need more, switch to Mouse / Moose
•use Class::XSAccessor
•fast, easy but extremely limited
•only implement getters
•do not provide setters
Object::Tiny[::XS] : The fast one !
package MyClass;
use Object::Tiny::XS qw{ list of attributes };
1;
 
package main;
my $object = MyClass->new( list => [ 42, 51 ], of => 'numbers' );
say join(',', @{$object->list}, $object->of);
eval { $object->attributes( 63 ); } or warn "Update value failed !";
Writing your own object module ?
package My::Object; # Inspired from Object::Tiny
sub import {
    return unless shift eq __PACKAGE__;
    my $pkg = caller;
    @{"${pkg}::ISA"} = 'My::Object';
    map {
        my $method = "${pkg}::$_";
        my $code = _get_accessor_for($_);
        { no strict 'refs'; *$method = $code; }
    } @_;
    return 1;
}
sub _get_accessor_for {
    my ($key) = @_;
    return unless caller eq __PACKAGE__; # Did I say private ?       
    defined $key and !ref $key and $key =~ /^[^Wd]w*z/s
or croak("Invalid key '$key'");
    sub {
        my ($self, $v) = @_;
        $self->{$key} = $v if defined $v;
        $self->{$key};
    };
}
sub new {
    my $class = shift;
    bless { @_ }, $class;
}
# that’s all !
Using your own object module
package main;
# initialize your object using new method
my $elt  = MyClass->new(list => [ 0, 1, 1, 2, 3, 5, 8, 13, 21, 34 ]);
# getter available
my $list = $elt->list;
# setter available
$elt->of('numbers');
package MyClass;
use My::Object qw{ list of attributes };
# that's all you need !
create a class
start playing with it
Extreme performance with Class::XSAccessor
package SuperFast;
use Class::XSAccessor
    replace     => 1,
    constructor => 'new',
    accessors => {
        speed => 'speed',
    },
    predicates => {
        has_speed => 'speed',
};
package main;
my $o = SuperFast->new(speed => 'flash');
is($o->speed, 'flash');
$o->speed('max') and say $o->speed;
is($o->speed, 'max');
•Fast read & write
•provide accessors, getters, setters and predicates
•use Devel::PPPort Perl Pollution Portability
Still want to keep using [better] hash ?
package Animal::Sounds;
use fields qw(cat dog bird);
sub new {
    my Animal::Sounds $self = shift;
    $self = fields::new($self) unless ref $self;
    return $self;
}
package main;
my Animal::Sounds $sound = Animal::Sounds->new(); # typed lexical
$sound->{cat}   = 'miaaooo'; # ok
$sound->{horse} = 'hiia'; # generates compile-time error  
# No such class field "horse" in variable $sound of type Animal::Sounds
fields builtin provide compile-time class
Objects ( typed ) with named fields are
as compact and as fast arrays to access
fields limit
# extract from field.pm
*new = sub {
        my $class = shift;
        $class = ref $class if ref $class;
        require Hash::Util;
        my $self = bless {}, $class;
                                       
        # The lock_keys() prototype won't work since we require Hash::Util :(
        &Hash::Util::lock_keys(%$self, _accessible_keys($class));
    # spent  6.00s making 20172 calls to Hash::Util::lock_keys, avg 297us/call
    # spent 710ms making 20172 calls to fields::_accessible_keys, avg 35us/call
        return $self;
}
be careful in production ( perl >= 5.009 )
Trick : mock *fields::new depending on current env

or switch to another object module
121     13.3ms
122     7.62ms
123     20.9ms
124     35.2ms
125    
126    
127     6.71s
128     61.6ms
129
•provides fast read access to attributes
•slow on object creation
Control hash life cycle
package main;
use Human;
use Test::More;
my $john = Human->new;
$john->{age} = 45;
is($john->{age}, 45, "nothing special");
$john->{age} = 123;
is($john->{age}, 99, "what happens there ?");
Still want to use hash ?
How is it possible ?
Use tie to hook
package Human;
sub TIESCALAR { die unless(ref $_[1] eq 'CODE'); my $cod=$_[1]; bless $cod, $_[0]; }
sub STORE { my $self = shift; $self->(@_); }
sub FETCH { shift->(); }
# many more available : DELETE, CLEAR, EXISTS, NEXTKEY...
sub new {
    my $class = shift;
    my $self = bless {}, $class;
    my $age;
    tie $self->{age}, __PACKAGE__, sub {
        my ($value) = @_;
        return $age unless defined $value;
        warn "You looks too old" and $value = 99 if $value >= 100;
        $age = $value;
        return $age;
    };
    $self;
}
You can also tie array, hash...
When we should not choose object ?
"Premature optimization is the root of all evil"
Tony Hoare
•performance ?
•difficult to learn ?
•need to create too many objects ?
•do not like abstraction ?
Performance problems ?
1/ Analyze the problem origin
•waiting for database
•waiting for IO
•CPU usage
•Memory usage
•.... use Devel::NYTProf
2/ then apply the correct solution
•use parallel algorithm / poe
•use inline C / XS
•do not use object
•...
Object Benchmark
Moo : best average solution ( also consider Mouse )
Class::XSAccessor : performance
fields : when many update and few object creation needed
Common Errors & Tricks
•make Moose package immutable

no Moose;
__PACKAGE__->meta->make_immutable();
•object creation is time consuming :

use cache or singleton if possible
•use factory for abstraction
•fields use Hash::Util::Lock

# bad idea if too many objects creation
•be careful with Time syscall, try to share it :

my $now = Datetime->now();
•be careful with coercion

# do not try to do too much
~ /black mag/ic
Thanks
atoomic@cpan.org

Contenu connexe

Tendances

The IoC Hydra - Dutch PHP Conference 2016
The IoC Hydra - Dutch PHP Conference 2016The IoC Hydra - Dutch PHP Conference 2016
The IoC Hydra - Dutch PHP Conference 2016Kacper Gunia
 
Caching and Scaling WordPress using Fragment Caching
Caching and Scaling WordPress using Fragment CachingCaching and Scaling WordPress using Fragment Caching
Caching and Scaling WordPress using Fragment CachingErick Hitter
 
Things I Believe Now That I'm Old
Things I Believe Now That I'm OldThings I Believe Now That I'm Old
Things I Believe Now That I'm OldRoss Tuck
 
Dependency Injection IPC 201
Dependency Injection IPC 201Dependency Injection IPC 201
Dependency Injection IPC 201Fabien Potencier
 
Doctrine fixtures
Doctrine fixturesDoctrine fixtures
Doctrine fixturesBill Chang
 
Speed Things Up with Transients
Speed Things Up with TransientsSpeed Things Up with Transients
Speed Things Up with TransientsCliff Seal
 
MTDDC 2010.2.5 Tokyo - Brand new API
MTDDC 2010.2.5 Tokyo - Brand new APIMTDDC 2010.2.5 Tokyo - Brand new API
MTDDC 2010.2.5 Tokyo - Brand new APISix Apart KK
 
PHP 7 – What changed internally? (Forum PHP 2015)
PHP 7 – What changed internally? (Forum PHP 2015)PHP 7 – What changed internally? (Forum PHP 2015)
PHP 7 – What changed internally? (Forum PHP 2015)Nikita Popov
 
Temporary Cache Assistance (Transients API): WordCamp Phoenix 2014
Temporary Cache Assistance (Transients API): WordCamp Phoenix 2014Temporary Cache Assistance (Transients API): WordCamp Phoenix 2014
Temporary Cache Assistance (Transients API): WordCamp Phoenix 2014Cliff Seal
 
The History of PHPersistence
The History of PHPersistenceThe History of PHPersistence
The History of PHPersistenceHugo Hamon
 
Php tips-and-tricks4128
Php tips-and-tricks4128Php tips-and-tricks4128
Php tips-and-tricks4128PrinceGuru MS
 
Dependency Injection with PHP and PHP 5.3
Dependency Injection with PHP and PHP 5.3Dependency Injection with PHP and PHP 5.3
Dependency Injection with PHP and PHP 5.3Fabien Potencier
 
Mongo db mug_2012-02-07
Mongo db mug_2012-02-07Mongo db mug_2012-02-07
Mongo db mug_2012-02-07Will Button
 
Dependency injection in PHP 5.3/5.4
Dependency injection in PHP 5.3/5.4Dependency injection in PHP 5.3/5.4
Dependency injection in PHP 5.3/5.4Fabien Potencier
 
Database Design Patterns
Database Design PatternsDatabase Design Patterns
Database Design PatternsHugo Hamon
 
[WLDN] Supercharging word press development in 2018
[WLDN] Supercharging word press development in 2018[WLDN] Supercharging word press development in 2018
[WLDN] Supercharging word press development in 2018Adam Tomat
 
HTML5 JavaScript Interfaces
HTML5 JavaScript InterfacesHTML5 JavaScript Interfaces
HTML5 JavaScript InterfacesAaron Gustafson
 

Tendances (20)

Nubilus Perl
Nubilus PerlNubilus Perl
Nubilus Perl
 
The IoC Hydra - Dutch PHP Conference 2016
The IoC Hydra - Dutch PHP Conference 2016The IoC Hydra - Dutch PHP Conference 2016
The IoC Hydra - Dutch PHP Conference 2016
 
Caching and Scaling WordPress using Fragment Caching
Caching and Scaling WordPress using Fragment CachingCaching and Scaling WordPress using Fragment Caching
Caching and Scaling WordPress using Fragment Caching
 
Things I Believe Now That I'm Old
Things I Believe Now That I'm OldThings I Believe Now That I'm Old
Things I Believe Now That I'm Old
 
Dependency Injection IPC 201
Dependency Injection IPC 201Dependency Injection IPC 201
Dependency Injection IPC 201
 
Doctrine fixtures
Doctrine fixturesDoctrine fixtures
Doctrine fixtures
 
Drupal 8 database api
Drupal 8 database apiDrupal 8 database api
Drupal 8 database api
 
Speed Things Up with Transients
Speed Things Up with TransientsSpeed Things Up with Transients
Speed Things Up with Transients
 
MTDDC 2010.2.5 Tokyo - Brand new API
MTDDC 2010.2.5 Tokyo - Brand new APIMTDDC 2010.2.5 Tokyo - Brand new API
MTDDC 2010.2.5 Tokyo - Brand new API
 
Perl Web Client
Perl Web ClientPerl Web Client
Perl Web Client
 
PHP 7 – What changed internally? (Forum PHP 2015)
PHP 7 – What changed internally? (Forum PHP 2015)PHP 7 – What changed internally? (Forum PHP 2015)
PHP 7 – What changed internally? (Forum PHP 2015)
 
Temporary Cache Assistance (Transients API): WordCamp Phoenix 2014
Temporary Cache Assistance (Transients API): WordCamp Phoenix 2014Temporary Cache Assistance (Transients API): WordCamp Phoenix 2014
Temporary Cache Assistance (Transients API): WordCamp Phoenix 2014
 
The History of PHPersistence
The History of PHPersistenceThe History of PHPersistence
The History of PHPersistence
 
Php tips-and-tricks4128
Php tips-and-tricks4128Php tips-and-tricks4128
Php tips-and-tricks4128
 
Dependency Injection with PHP and PHP 5.3
Dependency Injection with PHP and PHP 5.3Dependency Injection with PHP and PHP 5.3
Dependency Injection with PHP and PHP 5.3
 
Mongo db mug_2012-02-07
Mongo db mug_2012-02-07Mongo db mug_2012-02-07
Mongo db mug_2012-02-07
 
Dependency injection in PHP 5.3/5.4
Dependency injection in PHP 5.3/5.4Dependency injection in PHP 5.3/5.4
Dependency injection in PHP 5.3/5.4
 
Database Design Patterns
Database Design PatternsDatabase Design Patterns
Database Design Patterns
 
[WLDN] Supercharging word press development in 2018
[WLDN] Supercharging word press development in 2018[WLDN] Supercharging word press development in 2018
[WLDN] Supercharging word press development in 2018
 
HTML5 JavaScript Interfaces
HTML5 JavaScript InterfacesHTML5 JavaScript Interfaces
HTML5 JavaScript Interfaces
 

En vedette

Gdz biologiya zhuravleva
Gdz biologiya zhuravlevaGdz biologiya zhuravleva
Gdz biologiya zhuravlevaLucky Alex
 
C# .net lecture 5 win forms (2)
C# .net lecture 5 win forms (2)C# .net lecture 5 win forms (2)
C# .net lecture 5 win forms (2)Doron Raifman
 
Gdz biologiya strashko
Gdz biologiya strashkoGdz biologiya strashko
Gdz biologiya strashkoLucky Alex
 
Stages des vacances d'Hiver
Stages des vacances d'HiverStages des vacances d'Hiver
Stages des vacances d'HiverECLAmjc
 
Mata electric QC PLAN
Mata electric QC PLANMata electric QC PLAN
Mata electric QC PLANTina Sharpe
 
Técnicas y herramientas para la administración del tiempo
Técnicas y herramientas para la administración del tiempoTécnicas y herramientas para la administración del tiempo
Técnicas y herramientas para la administración del tiempoMarcelo Bulk
 

En vedette (11)

Gdz biologiya zhuravleva
Gdz biologiya zhuravlevaGdz biologiya zhuravleva
Gdz biologiya zhuravleva
 
C# .net lecture 5 win forms (2)
C# .net lecture 5 win forms (2)C# .net lecture 5 win forms (2)
C# .net lecture 5 win forms (2)
 
DOC-20160831-WA0035
DOC-20160831-WA0035DOC-20160831-WA0035
DOC-20160831-WA0035
 
JONMIKELEN AURKEZPENA
JONMIKELEN AURKEZPENA JONMIKELEN AURKEZPENA
JONMIKELEN AURKEZPENA
 
Caitlin Sturms-Salyer Resume
Caitlin Sturms-Salyer Resume Caitlin Sturms-Salyer Resume
Caitlin Sturms-Salyer Resume
 
Gdz biologiya strashko
Gdz biologiya strashkoGdz biologiya strashko
Gdz biologiya strashko
 
Stages des vacances d'Hiver
Stages des vacances d'HiverStages des vacances d'Hiver
Stages des vacances d'Hiver
 
Johan
JohanJohan
Johan
 
Mata electric QC PLAN
Mata electric QC PLANMata electric QC PLAN
Mata electric QC PLAN
 
Técnicas y herramientas para la administración del tiempo
Técnicas y herramientas para la administración del tiempoTécnicas y herramientas para la administración del tiempo
Técnicas y herramientas para la administración del tiempo
 
Diseño visual para web
Diseño visual para webDiseño visual para web
Diseño visual para web
 

Similaire à Perl object ?

Intro To Moose
Intro To MooseIntro To Moose
Intro To MoosecPanel
 
Moose - YAPC::NA 2012
Moose - YAPC::NA 2012Moose - YAPC::NA 2012
Moose - YAPC::NA 2012xSawyer
 
DBIx::Skinnyと仲間たち
DBIx::Skinnyと仲間たちDBIx::Skinnyと仲間たち
DBIx::Skinnyと仲間たちRyo Miyake
 
Perl Teach-In (part 2)
Perl Teach-In (part 2)Perl Teach-In (part 2)
Perl Teach-In (part 2)Dave Cross
 
Can't Miss Features of PHP 5.3 and 5.4
Can't Miss Features of PHP 5.3 and 5.4Can't Miss Features of PHP 5.3 and 5.4
Can't Miss Features of PHP 5.3 and 5.4Jeff Carouth
 
Good Evils In Perl (Yapc Asia)
Good Evils In Perl (Yapc Asia)Good Evils In Perl (Yapc Asia)
Good Evils In Perl (Yapc Asia)Kang-min Liu
 
Introducing CakeEntity
Introducing CakeEntityIntroducing CakeEntity
Introducing CakeEntityBasuke Suzuki
 
Virtual Madness @ Etsy
Virtual Madness @ EtsyVirtual Madness @ Etsy
Virtual Madness @ EtsyNishan Subedi
 
The Zen of Lithium
The Zen of LithiumThe Zen of Lithium
The Zen of LithiumNate Abele
 
Extending Moose
Extending MooseExtending Moose
Extending Moosesartak
 
My app is secure... I think
My app is secure... I thinkMy app is secure... I think
My app is secure... I thinkWim Godden
 
My app is secure... I think
My app is secure... I thinkMy app is secure... I think
My app is secure... I thinkWim Godden
 
My app is secure... I think
My app is secure... I thinkMy app is secure... I think
My app is secure... I thinkWim Godden
 
Internationalizing CakePHP Applications
Internationalizing CakePHP ApplicationsInternationalizing CakePHP Applications
Internationalizing CakePHP ApplicationsPierre MARTIN
 
PHP Static Code Review
PHP Static Code ReviewPHP Static Code Review
PHP Static Code ReviewDamien Seguy
 

Similaire à Perl object ? (20)

OOP in PHP.pptx
OOP in PHP.pptxOOP in PHP.pptx
OOP in PHP.pptx
 
Intro To Moose
Intro To MooseIntro To Moose
Intro To Moose
 
Moose - YAPC::NA 2012
Moose - YAPC::NA 2012Moose - YAPC::NA 2012
Moose - YAPC::NA 2012
 
php2.pptx
php2.pptxphp2.pptx
php2.pptx
 
DBIx::Skinnyと仲間たち
DBIx::Skinnyと仲間たちDBIx::Skinnyと仲間たち
DBIx::Skinnyと仲間たち
 
Perl Teach-In (part 2)
Perl Teach-In (part 2)Perl Teach-In (part 2)
Perl Teach-In (part 2)
 
Can't Miss Features of PHP 5.3 and 5.4
Can't Miss Features of PHP 5.3 and 5.4Can't Miss Features of PHP 5.3 and 5.4
Can't Miss Features of PHP 5.3 and 5.4
 
Good Evils In Perl (Yapc Asia)
Good Evils In Perl (Yapc Asia)Good Evils In Perl (Yapc Asia)
Good Evils In Perl (Yapc Asia)
 
Introducing CakeEntity
Introducing CakeEntityIntroducing CakeEntity
Introducing CakeEntity
 
Virtual Madness @ Etsy
Virtual Madness @ EtsyVirtual Madness @ Etsy
Virtual Madness @ Etsy
 
Laravel doctrine
Laravel doctrineLaravel doctrine
Laravel doctrine
 
The Zen of Lithium
The Zen of LithiumThe Zen of Lithium
The Zen of Lithium
 
Extending Moose
Extending MooseExtending Moose
Extending Moose
 
Separation of concerns - DPC12
Separation of concerns - DPC12Separation of concerns - DPC12
Separation of concerns - DPC12
 
My app is secure... I think
My app is secure... I thinkMy app is secure... I think
My app is secure... I think
 
My app is secure... I think
My app is secure... I thinkMy app is secure... I think
My app is secure... I think
 
My app is secure... I think
My app is secure... I thinkMy app is secure... I think
My app is secure... I think
 
CakePHP workshop
CakePHP workshopCakePHP workshop
CakePHP workshop
 
Internationalizing CakePHP Applications
Internationalizing CakePHP ApplicationsInternationalizing CakePHP Applications
Internationalizing CakePHP Applications
 
PHP Static Code Review
PHP Static Code ReviewPHP Static Code Review
PHP Static Code Review
 

Plus de ℕicolas ℝ.

Overloading Perl OPs using XS
Overloading Perl OPs using XSOverloading Perl OPs using XS
Overloading Perl OPs using XSℕicolas ℝ.
 
2018 Perl Retrospective at Houston.pm
2018 Perl Retrospective at Houston.pm2018 Perl Retrospective at Houston.pm
2018 Perl Retrospective at Houston.pmℕicolas ℝ.
 
Lightning Talk Perl Test mock module
Lightning Talk Perl Test mock moduleLightning Talk Perl Test mock module
Lightning Talk Perl Test mock moduleℕicolas ℝ.
 
Introduction to Perl Internals
Introduction to Perl InternalsIntroduction to Perl Internals
Introduction to Perl Internalsℕicolas ℝ.
 
YAPC::EU 2015 - Perl Conferences
YAPC::EU 2015 - Perl ConferencesYAPC::EU 2015 - Perl Conferences
YAPC::EU 2015 - Perl Conferencesℕicolas ℝ.
 

Plus de ℕicolas ℝ. (8)

Overloading Perl OPs using XS
Overloading Perl OPs using XSOverloading Perl OPs using XS
Overloading Perl OPs using XS
 
2018 Perl Retrospective at Houston.pm
2018 Perl Retrospective at Houston.pm2018 Perl Retrospective at Houston.pm
2018 Perl Retrospective at Houston.pm
 
Lightning Talk Perl Test mock module
Lightning Talk Perl Test mock moduleLightning Talk Perl Test mock module
Lightning Talk Perl Test mock module
 
Perl XS by example
Perl XS by examplePerl XS by example
Perl XS by example
 
Introduction to Perl Internals
Introduction to Perl InternalsIntroduction to Perl Internals
Introduction to Perl Internals
 
grep.metacpan.org
grep.metacpan.orggrep.metacpan.org
grep.metacpan.org
 
Amazon::Dash::Button
Amazon::Dash::ButtonAmazon::Dash::Button
Amazon::Dash::Button
 
YAPC::EU 2015 - Perl Conferences
YAPC::EU 2015 - Perl ConferencesYAPC::EU 2015 - Perl Conferences
YAPC::EU 2015 - Perl Conferences
 

Dernier

+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...Health
 
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdfAzure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdfryanfarris8
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxComplianceQuest1
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfVishalKumarJha10
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Modelsaagamshah0812
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
How to Choose the Right Laravel Development Partner in New York City_compress...
How to Choose the Right Laravel Development Partner in New York City_compress...How to Choose the Right Laravel Development Partner in New York City_compress...
How to Choose the Right Laravel Development Partner in New York City_compress...software pro Development
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech studentsHimanshiGarg82
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsJhone kinadey
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...panagenda
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdfPearlKirahMaeRagusta1
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionSolGuruz
 
Exploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfExploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfproinshot.com
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesVictorSzoltysek
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsAlberto González Trastoy
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Steffen Staab
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
 

Dernier (20)

+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdfAzure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
 
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS LiveVip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
How to Choose the Right Laravel Development Partner in New York City_compress...
How to Choose the Right Laravel Development Partner in New York City_compress...How to Choose the Right Laravel Development Partner in New York City_compress...
How to Choose the Right Laravel Development Partner in New York City_compress...
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdf
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
 
Exploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfExploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdf
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 

Perl object ?

  • 1. Perl Object ? SLC.pm October 2015 Perl Object Nicolas Rochelemagne
  • 2. Perl Object ? •Why ? •When ? •How ?
  • 3. Without objects we manipulate scalars, arrays, hashes... $hash = {         key => 42,         another => {                 one  => 43,                 deep => {                         here => {                                 you => {                                         can => 'find me'                                 }                         }                 },                 str  => "Data value"         },         elts => [ 8, 25, 37, 64 ] }; this lead to store data in /ugly/ hashes
  • 4. Problems with this approach : you need to know the "exact key" to use... $hash->{key} = 42; $hash->{anotherlevel}->{key} = "value"; $hash->{where}->{can}{be}{mykey} = { it => 's there' }; deep research into the code... or need to dump huge hash
  • 5. You can s/confound/mispell/ key name somewhere sub method {         my $h = shift;         $h->{key} = 42; } then later ...         $h->{keys}++ and $h->{kyes} = 51; ...
  • 6. You cannot change the key name or storage level cannot become neither $hash->{current_key} = 42; nor $hash->{updated_key} = 42; $hash->{better}{key} = 42; without updating the full code !
  • 7. You cannot control life cycle of an attribute : sub somewhere {         ...         $hash->{mykey} = 42 and additionnal_operation;         ... } sub otherplace {         ...         $hash->{mykey} = 43 and additionnal_operation;         ... } •on create •on update •before, after, around •hooks : innner, augment... without objects
  • 8. You would need an accessor it should be the only single way
 to access to this piece of data sub mykey {         my ($self, $value) = @_;         if (defined $value) {                 $self->{_mykey} = $value;                 $self->additionnal_operation();         }                 $self->{_mykey}; }
  • 9. Writing documentation =pod =head attribute Parameters : attribute value ( optional ) Description : attribute method can be used to read or update it ! Returns : current value Usage :         # use as reader         my $read = $object->attribute();         # use as writter         $object->attribute($new_value); =cut         sub attribute {                 ... some code there ( or not ! )         } ... is difficult rather than Please use $object->{level1}{level2} to access the expected value ! # at this time, and it could never change ?
  • 10. What is an object ? •functions •data Small piece of code
  • 11. Why object / module ? •code is organized •Object is DRY, Lazy & Fun •code is easier to read / share •design & development can be dissociate •easier to test •provides fault containment •reduce code maintenance •simplify product evolution •documentation is easier
  • 12. Code organization Object |__ Human | |___ Male | |___ Female | |__ Transport |___ Car |___ Plane |___ Bicycle #!perl package Object::Transport::Car;
  • 13. DRY : Do not Repeat Yourself  package Document::Page;   sub create {       my $self = shift;       ...         do_some_stuff; # inner();       ...   }   package Document::Special;   extends 'Document::Page';   augment 'create' => sub {       my $self = shift;       $self->do_some_extra_stuff;   }; •inheritance help you factorize your code •roles ( ~ interface )
  • 14. Lazy package Person; use Moose; # or any other object module has 'age'    => ( is => 'rw', isa => 'Int', default => 42 );   package main; my $p = Person->new() or Person->new(age => 64); $current_age = $p->age(); $p->age($new_age); •provide "new" method •provide accessors •parameters validation :
 types, optional, default value...
  • 15. Type checking  package Person;   use Try::Tiny;   use Moose;   use Moose::Util::TypeConstraints;   subtype 'Sex'       => as 'Str'       => where { $_ =~ m{^[mf]$}s };   has 'sex'    => ( is => 'ro', isa => 'Sex', required =>  1 );   has 'age'    => ( is => 'rw', isa => 'Int', default  => 42 );     my $person = Person->new( sex => 'm', age => 45 );   try {         Person->new( sex => 'unknown' );   } catch {          warn "Error has been detected";   };
  • 16. Coercion package My::Types::Date; use Moose::Util::TypeConstraints; use DateTime; # ... subtype 'MyType:Day' => as 'DateTime'; coerce  'MyType:Day' => from 'Str' => via {     /^d{4}d{2}d{2}$/ or croak "Unable to coerce '$_' into a valid date";     return DateTime::Format::DateParse->parse_datetime($_); }; package Test::Coerce; use Moose; use My::Types::Date; has day => ( is  => 'rw', isa => 'MyType:Day',         coerce => 1); package main; my $d1 = Test::Coerce->new(day => DateTime->now()); my $d2 = Test::Coerce->new(day => '20111130'); isa_ok $d2->day, 'DateTime', 'day is coerced to a DateTime object';
  • 17. Which object library to choose ? •Moose •Mouse •Moo •Mo •M •... •fields •Object::Tiny(::XS) •Class::XSAccessor •write your own simple Object ? and sometimes none
  • 18. •the most advanced •large ecosystem of extensions : 
 MooseX : validate, getopt, singleton, types... •can use advanced types and coercion methods •hooks : before, after, inner, augment... •startup time •memory usage •dependencies... Moose : the fat one ! fat but so good... Advantages : Disadvantages :
  • 19. •same goal as Mouse : provide a Moose lighter •but provide "as little as possible" : minimalist Moo : the light one ! package Demo::Moo; use Moo; with 'Some::Role'; has bar => (         is => 'rw',         isa => sub { $_[0] =~ /^[+-]?d+$/ },         coerce => quote_sub q{ $_[0] + 1 unless $_[0] % 2 },         lazy => 1, ); •can use only one role at a time •not available : super, override, inner, augment •no initializer Start with Moo and if you need more, switch to Mouse / Moose
  • 20. •use Class::XSAccessor •fast, easy but extremely limited •only implement getters •do not provide setters Object::Tiny[::XS] : The fast one ! package MyClass; use Object::Tiny::XS qw{ list of attributes }; 1;   package main; my $object = MyClass->new( list => [ 42, 51 ], of => 'numbers' ); say join(',', @{$object->list}, $object->of); eval { $object->attributes( 63 ); } or warn "Update value failed !";
  • 21. Writing your own object module ? package My::Object; # Inspired from Object::Tiny sub import {     return unless shift eq __PACKAGE__;     my $pkg = caller;     @{"${pkg}::ISA"} = 'My::Object';     map {         my $method = "${pkg}::$_";         my $code = _get_accessor_for($_);         { no strict 'refs'; *$method = $code; }     } @_;     return 1; } sub _get_accessor_for {     my ($key) = @_;     return unless caller eq __PACKAGE__; # Did I say private ?            defined $key and !ref $key and $key =~ /^[^Wd]w*z/s or croak("Invalid key '$key'");     sub {         my ($self, $v) = @_;         $self->{$key} = $v if defined $v;         $self->{$key};     }; } sub new {     my $class = shift;     bless { @_ }, $class; } # that’s all !
  • 22. Using your own object module package main; # initialize your object using new method my $elt  = MyClass->new(list => [ 0, 1, 1, 2, 3, 5, 8, 13, 21, 34 ]); # getter available my $list = $elt->list; # setter available $elt->of('numbers'); package MyClass; use My::Object qw{ list of attributes }; # that's all you need ! create a class start playing with it
  • 23. Extreme performance with Class::XSAccessor package SuperFast; use Class::XSAccessor     replace     => 1,     constructor => 'new',     accessors => {         speed => 'speed',     },     predicates => {         has_speed => 'speed', }; package main; my $o = SuperFast->new(speed => 'flash'); is($o->speed, 'flash'); $o->speed('max') and say $o->speed; is($o->speed, 'max'); •Fast read & write •provide accessors, getters, setters and predicates •use Devel::PPPort Perl Pollution Portability
  • 24. Still want to keep using [better] hash ? package Animal::Sounds; use fields qw(cat dog bird); sub new {     my Animal::Sounds $self = shift;     $self = fields::new($self) unless ref $self;     return $self; } package main; my Animal::Sounds $sound = Animal::Sounds->new(); # typed lexical $sound->{cat}   = 'miaaooo'; # ok $sound->{horse} = 'hiia'; # generates compile-time error   # No such class field "horse" in variable $sound of type Animal::Sounds fields builtin provide compile-time class Objects ( typed ) with named fields are as compact and as fast arrays to access
  • 25. fields limit # extract from field.pm *new = sub {         my $class = shift;         $class = ref $class if ref $class;         require Hash::Util;         my $self = bless {}, $class;                                                 # The lock_keys() prototype won't work since we require Hash::Util :(         &Hash::Util::lock_keys(%$self, _accessible_keys($class));     # spent  6.00s making 20172 calls to Hash::Util::lock_keys, avg 297us/call     # spent 710ms making 20172 calls to fields::_accessible_keys, avg 35us/call         return $self; } be careful in production ( perl >= 5.009 ) Trick : mock *fields::new depending on current env
 or switch to another object module 121     13.3ms 122     7.62ms 123     20.9ms 124     35.2ms 125     126     127     6.71s 128     61.6ms 129 •provides fast read access to attributes •slow on object creation
  • 26. Control hash life cycle package main; use Human; use Test::More; my $john = Human->new; $john->{age} = 45; is($john->{age}, 45, "nothing special"); $john->{age} = 123; is($john->{age}, 99, "what happens there ?"); Still want to use hash ? How is it possible ?
  • 27. Use tie to hook package Human; sub TIESCALAR { die unless(ref $_[1] eq 'CODE'); my $cod=$_[1]; bless $cod, $_[0]; } sub STORE { my $self = shift; $self->(@_); } sub FETCH { shift->(); } # many more available : DELETE, CLEAR, EXISTS, NEXTKEY... sub new {     my $class = shift;     my $self = bless {}, $class;     my $age;     tie $self->{age}, __PACKAGE__, sub {         my ($value) = @_;         return $age unless defined $value;         warn "You looks too old" and $value = 99 if $value >= 100;         $age = $value;         return $age;     };     $self; } You can also tie array, hash...
  • 28. When we should not choose object ? "Premature optimization is the root of all evil" Tony Hoare •performance ? •difficult to learn ? •need to create too many objects ? •do not like abstraction ?
  • 29. Performance problems ? 1/ Analyze the problem origin •waiting for database •waiting for IO •CPU usage •Memory usage •.... use Devel::NYTProf 2/ then apply the correct solution •use parallel algorithm / poe •use inline C / XS •do not use object •...
  • 30. Object Benchmark Moo : best average solution ( also consider Mouse ) Class::XSAccessor : performance fields : when many update and few object creation needed
  • 31. Common Errors & Tricks •make Moose package immutable
 no Moose; __PACKAGE__->meta->make_immutable(); •object creation is time consuming :
 use cache or singleton if possible •use factory for abstraction •fields use Hash::Util::Lock
 # bad idea if too many objects creation •be careful with Time syscall, try to share it :
 my $now = Datetime->now(); •be careful with coercion
 # do not try to do too much ~ /black mag/ic