SlideShare uma empresa Scribd logo
1 de 20
Baixar para ler offline
Object::Exercise



Keeping your objects healthy.

      Steven Lembark 
<slembark@cheetahmail.com>
Objects Need Exercise
●
    Both in development and “normal” use.
    –   Error & regression testing.
    –   Benchmarking and capacity planning.
    –   Setup for normal operations.
    –   Bulk data processing.
●
    This is a description of how the 
    Object::Execute harness evolved.
Common Execution Cycle
●
    Run a command.
●
    Check $@, optionally aborting if it is set.
●
    Compare the return value to a known one or 
    run a code snipped to evaluate it, optionally 
    aborting on a mismatched value.
●
    During development, set a breakpoint if the 
    operation or comparison fails.
●
    Run the next command...
The Cycle is Repetitive
●
    We've all seen – or written – code like this:


my $object = Some::Class­>construct( @argz );
my $sanity = '';
$sanity = $object­>first_method( @more_argz );
'HASH' eq ref $sanity or die “Not a hashref...”;
$sanity = $object­>second_part( @even_more_argz );
$DB::single = 1 unless $sanity;
$sanity­>[0] eq 'foobar' or die “No foobar...”
$sanity = $object­>third_part( @yet_more_argz );
$DB::single = 1 if 'ARRAY' ne ref $sanity;
'ARRAY' eq ref $sanity or die “Not an arrayref”;
$sanity = $object­>third_part( 'a' .. 'z' );
Replacing the Cruft
●
     This is a good example MJD's “Red Flags”:
    –   Most of the code is framework, probably updated 
        via cut+paste.
    –   You can easily spend more time writing and 
        debugging the framework code than the module 
        itself.
    –   It is difficult to generate the code so you end up 
        having to code it all manuall.
●
    The trick to cleaning it up is abstracting out 
    the framework and leaving the data.
A better way: Lazy Exercise
●
    Being lazy means not doing something the 
    computer can do for you or having to do it 
    yourself more than once.
●
    Perl's OO handles this gracefully by allowing 
    $object­>$name( @argz ) notation: I can 
    store the method calls as data instead of 
    hard­coding them.
●
    This allows storing the operations as data 
    instead of code.
Using Object::Exercise
●
    O::E is basically a “little language” for object 
    execution – a very little language.
●
    Instructions are either arrayref's that are 
    executed by the object or a small­ish set of 
    text instructions that control the harness.
●
    Upcoming improvement is allowing 
    Plugin::Installer to easily add add your own 
    instructions.
Object Execution: Arrayref
●
    The most common thing you'll do is call a 
    method, possibly checking the results.
●
    To run a method and ignore anything other 
    than failure use: [ $method => @argz ] (i.e., a 
    method and list of arguments).
●
    $method can be either text or a coderef, 
    which allows dispatching to local handler 
    code.
Pass 1: Storing Operations
●
    The previous code could be reduced to a list of 
    arrayref's each one with a method name (or 
    subref) and some arguments.
●
    All I need then is an object:
    sub pass1
    {
       my $obj = shift;
       for( @_ )
       {
          my $method = shift @$_;
          $obj­>$method( @$_ )
       }
    }
Dealing With Failure
●
    Adding an eval{} allows checking the results 
    and stopping execution on a failure:
    sub pass2
    {
       my $obj = shift;
       for( @_ )
       {
          my $method = shift @$_;
          eval { $obj­>$method( @$_ ) };

            print $@ if $@;
            $DB::single = 1 if $@;
            0
        }
    }
Taking a Closure Look
●
    Setting $DB::single = 1 in the Perl debugger 
    starts a breakpoint.
●
    $DB::single = 1 if $debug sets a breakpoint 
    conditional on $debug being perly­true.
●
    Adding “our $debug” means it can be 
    localized to true if $@ is set or or cmp_deeply 
    returns false.
●
    This allows “&$cmd” to stop right before the 
    method is called.
sub pass3
{
   my $obj = shift;
   for( @_ )
   {
      my ( $method, @argz ) = @$_;
      my $cmd = sub{ $obj­>$method( @argz ) };

        eval { &$cmd };
        if( $@ )
        {
           print $@;
           $DB::single = 1;
           0                  # &$cmd here re­runs
        }
    }
}
Adding A Breakpoint
●
    I wanted to stop execution before dispatching 
    the method call on re­runs. But “&$cmd” 
    immediately runs the code.
●
    Adding a breakpoint to the closure allows 
    stopping before the method is called:
our $debug = '';
sub pass3
{
   my $obj = shift;
   for( @_ )
   {
      my ( $method, @argz ) = @$_;
      my $cmd
      = sub
      {
         $DB::single = 1 if $debug;
         $obj­>$method( @argz )
      };

        eval { &$cmd };
        if( $@ )
        {
           print $@;
           local $debug = 1; # &$cmd stops before the method.
           $DB::single  = 1; 
           0                 # harness execution stops here.
        }
    }
}
Getting Testy About It
●
    Usually you will want to  check the results of 
    execution. 
●
    Instead of a single arrayref, I nested two of 
    them: one with the method the other 
    expected results:
    [
        [ $method => @argz ],
        [ comparison value ],
    ]
How methods are executed
●
    If there is data for comparison then the 
    return is saved, otherwise it can be ignored.
    for( @_ )
    {
        my ( $operation, $compare )
        = 'ARRAY' eq ref $_­>[0] ? @{ $_ } : ( $_ );
             
        ...
             
        if( $@ )
        {
            # set $debug, $DB::single = 1.
        }
        elsif( ! cmp_deeply $result, $compare, $message )
        {
            # set $debug, $DB::single = 1.
        }
        elsif( $verbose )
        {
            print ...
        }
    }
sub pass3
{
   my $obj = shift;
   for( @_ )
   {
      my ( $method, @argz ) = @$_;
      my $cmd
      = sub
      {
         $DB::single = 1 if $debug;
         $obj­>$method( @argz );
      };

        eval { &$cmd };
        if( $@ )
        {
           print $@;
           local $debug = 1; # &$cmd stops at $obj­>method()
           $DB::single  = 1;
           0                 # execution stops here 
        }
    }
}
Doing it your way...
●
    Q: What if the execution requires more than 
    just $obj­>$method( @argz )?
●
    A: Use a coderef as the method.
●
    It will be called as $obj­>$coderef with a sanity check 
    for $@ afterward. This can be handy for updating the 
    object's internals as operations progress.
    [
         [ make_hash => ( 'a' .. 'z' ) ], [ { 'a' .. 'z' } ]
    ],
    [
         [ $reset_object_params, qw( verbose 1 )
    ],
    [
         ...
Control statements
●
    There is a [very] little language for 
    controlling execution.
●
    These are non­arrayref entries in the list of 
    operations:
    –   'break' & 'continue' set $debug to true/false.
    –   'verbose' & 'quiet' set $verbose for log messages.
The Result: Object::Exercise
●
    An early version is on CPAN.
●
    Supports benchmark mode (no comparison, 
    full speed execution w/ Benchmark & 
    :hireswallclock.
●
    Few more control statements.
●
    Plenty of POD.

Mais conteúdo relacionado

Mais procurados

RSpec User Stories
RSpec User StoriesRSpec User Stories
RSpec User Storiesrahoulb
 
シックス・アパート・フレームワーク
シックス・アパート・フレームワークシックス・アパート・フレームワーク
シックス・アパート・フレームワークTakatsugu Shigeta
 
Oracle - Program with PL/SQL - Lession 17
Oracle - Program with PL/SQL - Lession 17Oracle - Program with PL/SQL - Lession 17
Oracle - Program with PL/SQL - Lession 17Thuan Nguyen
 
Surfacing External Data Through Magnolia
Surfacing External Data Through MagnoliaSurfacing External Data Through Magnolia
Surfacing External Data Through MagnoliaSeanMcTex
 
Oracle - Program with PL/SQL - Lession 16
Oracle - Program with PL/SQL - Lession 16Oracle - Program with PL/SQL - Lession 16
Oracle - Program with PL/SQL - Lession 16Thuan Nguyen
 
Oracle - Program with PL/SQL - Lession 03
Oracle - Program with PL/SQL - Lession 03Oracle - Program with PL/SQL - Lession 03
Oracle - Program with PL/SQL - Lession 03Thuan Nguyen
 
Ruby on Rails testing with Rspec
Ruby on Rails testing with RspecRuby on Rails testing with Rspec
Ruby on Rails testing with RspecBunlong Van
 
Oracle - Program with PL/SQL - Lession 07
Oracle - Program with PL/SQL - Lession 07Oracle - Program with PL/SQL - Lession 07
Oracle - Program with PL/SQL - Lession 07Thuan Nguyen
 
Optimizing Magento by Preloading Data
Optimizing Magento by Preloading DataOptimizing Magento by Preloading Data
Optimizing Magento by Preloading DataIvan Chepurnyi
 
Oracle - Program with PL/SQL - Lession 12
Oracle - Program with PL/SQL - Lession 12Oracle - Program with PL/SQL - Lession 12
Oracle - Program with PL/SQL - Lession 12Thuan Nguyen
 
Oracle - Program with PL/SQL - Lession 01
Oracle - Program with PL/SQL - Lession 01Oracle - Program with PL/SQL - Lession 01
Oracle - Program with PL/SQL - Lession 01Thuan Nguyen
 
Php Unit With Zend Framework Zendcon09
Php Unit With Zend Framework   Zendcon09Php Unit With Zend Framework   Zendcon09
Php Unit With Zend Framework Zendcon09Michelangelo van Dam
 
Ch2(working with forms)
Ch2(working with forms)Ch2(working with forms)
Ch2(working with forms)Chhom Karath
 
Oracle - Program with PL/SQL - Lession 11
Oracle - Program with PL/SQL - Lession 11Oracle - Program with PL/SQL - Lession 11
Oracle - Program with PL/SQL - Lession 11Thuan Nguyen
 
Activity lesson
Activity lessonActivity lesson
Activity lessonMax Friel
 
Apex 5 plugins for everyone version 2018
Apex 5 plugins for everyone   version 2018Apex 5 plugins for everyone   version 2018
Apex 5 plugins for everyone version 2018Alan Arentsen
 

Mais procurados (20)

RSpec User Stories
RSpec User StoriesRSpec User Stories
RSpec User Stories
 
シックス・アパート・フレームワーク
シックス・アパート・フレームワークシックス・アパート・フレームワーク
シックス・アパート・フレームワーク
 
Oracle - Program with PL/SQL - Lession 17
Oracle - Program with PL/SQL - Lession 17Oracle - Program with PL/SQL - Lession 17
Oracle - Program with PL/SQL - Lession 17
 
Surfacing External Data Through Magnolia
Surfacing External Data Through MagnoliaSurfacing External Data Through Magnolia
Surfacing External Data Through Magnolia
 
Oracle - Program with PL/SQL - Lession 16
Oracle - Program with PL/SQL - Lession 16Oracle - Program with PL/SQL - Lession 16
Oracle - Program with PL/SQL - Lession 16
 
Oracle - Program with PL/SQL - Lession 03
Oracle - Program with PL/SQL - Lession 03Oracle - Program with PL/SQL - Lession 03
Oracle - Program with PL/SQL - Lession 03
 
Ruby on Rails testing with Rspec
Ruby on Rails testing with RspecRuby on Rails testing with Rspec
Ruby on Rails testing with Rspec
 
Msql
Msql Msql
Msql
 
Oracle - Program with PL/SQL - Lession 07
Oracle - Program with PL/SQL - Lession 07Oracle - Program with PL/SQL - Lession 07
Oracle - Program with PL/SQL - Lession 07
 
Optimizing Magento by Preloading Data
Optimizing Magento by Preloading DataOptimizing Magento by Preloading Data
Optimizing Magento by Preloading Data
 
Triggers and Stored Procedures
Triggers and Stored ProceduresTriggers and Stored Procedures
Triggers and Stored Procedures
 
Oracle - Program with PL/SQL - Lession 12
Oracle - Program with PL/SQL - Lession 12Oracle - Program with PL/SQL - Lession 12
Oracle - Program with PL/SQL - Lession 12
 
PHPSpec BDD Framework
PHPSpec BDD FrameworkPHPSpec BDD Framework
PHPSpec BDD Framework
 
Oracle - Program with PL/SQL - Lession 01
Oracle - Program with PL/SQL - Lession 01Oracle - Program with PL/SQL - Lession 01
Oracle - Program with PL/SQL - Lession 01
 
Php Unit With Zend Framework Zendcon09
Php Unit With Zend Framework   Zendcon09Php Unit With Zend Framework   Zendcon09
Php Unit With Zend Framework Zendcon09
 
Ch2(working with forms)
Ch2(working with forms)Ch2(working with forms)
Ch2(working with forms)
 
Oracle - Program with PL/SQL - Lession 11
Oracle - Program with PL/SQL - Lession 11Oracle - Program with PL/SQL - Lession 11
Oracle - Program with PL/SQL - Lession 11
 
Activity lesson
Activity lessonActivity lesson
Activity lesson
 
Apex 5 plugins for everyone version 2018
Apex 5 plugins for everyone   version 2018Apex 5 plugins for everyone   version 2018
Apex 5 plugins for everyone version 2018
 
Tuning SGA
Tuning SGATuning SGA
Tuning SGA
 

Destaque

Investor Seminar in San Francisco March 7th, 2015
Investor Seminar in San Francisco March 7th, 2015Investor Seminar in San Francisco March 7th, 2015
Investor Seminar in San Francisco March 7th, 2015Joe Pryor
 
Our Friends the Utils: A highway traveled by wheels we didn't re-invent.
Our Friends the Utils: A highway traveled by wheels we didn't re-invent. Our Friends the Utils: A highway traveled by wheels we didn't re-invent.
Our Friends the Utils: A highway traveled by wheels we didn't re-invent. Workhorse Computing
 
Perly Parallel Processing of Fixed Width Data Records
Perly Parallel Processing of Fixed Width Data RecordsPerly Parallel Processing of Fixed Width Data Records
Perly Parallel Processing of Fixed Width Data RecordsWorkhorse Computing
 
Low and No cost real estate marketing plan for Enid Oklahoma
Low and No cost real estate marketing plan for Enid OklahomaLow and No cost real estate marketing plan for Enid Oklahoma
Low and No cost real estate marketing plan for Enid OklahomaJoe Pryor
 

Destaque (6)

Investor Seminar in San Francisco March 7th, 2015
Investor Seminar in San Francisco March 7th, 2015Investor Seminar in San Francisco March 7th, 2015
Investor Seminar in San Francisco March 7th, 2015
 
Our Friends the Utils: A highway traveled by wheels we didn't re-invent.
Our Friends the Utils: A highway traveled by wheels we didn't re-invent. Our Friends the Utils: A highway traveled by wheels we didn't re-invent.
Our Friends the Utils: A highway traveled by wheels we didn't re-invent.
 
Perly Parallel Processing of Fixed Width Data Records
Perly Parallel Processing of Fixed Width Data RecordsPerly Parallel Processing of Fixed Width Data Records
Perly Parallel Processing of Fixed Width Data Records
 
A-Walk-on-the-W-Side
A-Walk-on-the-W-SideA-Walk-on-the-W-Side
A-Walk-on-the-W-Side
 
Clustering Genes: W-curve + TSP
Clustering Genes: W-curve + TSPClustering Genes: W-curve + TSP
Clustering Genes: W-curve + TSP
 
Low and No cost real estate marketing plan for Enid Oklahoma
Low and No cost real estate marketing plan for Enid OklahomaLow and No cost real estate marketing plan for Enid Oklahoma
Low and No cost real estate marketing plan for Enid Oklahoma
 

Semelhante a Object Exercise

Simple Ways To Be A Better Programmer (OSCON 2007)
Simple Ways To Be A Better Programmer (OSCON 2007)Simple Ways To Be A Better Programmer (OSCON 2007)
Simple Ways To Be A Better Programmer (OSCON 2007)Michael Schwern
 
Perl web frameworks
Perl web frameworksPerl web frameworks
Perl web frameworksdiego_k
 
Curscatalyst
CurscatalystCurscatalyst
CurscatalystKar Juan
 
Beijing Perl Workshop 2008 Hiveminder Secret Sauce
Beijing Perl Workshop 2008 Hiveminder Secret SauceBeijing Perl Workshop 2008 Hiveminder Secret Sauce
Beijing Perl Workshop 2008 Hiveminder Secret SauceJesse Vincent
 
Turbogears Presentation
Turbogears PresentationTurbogears Presentation
Turbogears Presentationdidip
 
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
 
Impacta - Show Day de Rails
Impacta - Show Day de RailsImpacta - Show Day de Rails
Impacta - Show Day de RailsFabio Akita
 
Capistrano 2 Rocks My World
Capistrano 2 Rocks My WorldCapistrano 2 Rocks My World
Capistrano 2 Rocks My WorldGraeme Mathieson
 
Real World Optimization
Real World OptimizationReal World Optimization
Real World OptimizationDavid Golden
 
Ten modules I haven't yet talked about
Ten modules I haven't yet talked aboutTen modules I haven't yet talked about
Ten modules I haven't yet talked aboutacme
 
PHP tips and tricks
PHP tips and tricks PHP tips and tricks
PHP tips and tricks Damien Seguy
 
Curso Symfony - Clase 2
Curso Symfony - Clase 2Curso Symfony - Clase 2
Curso Symfony - Clase 2Javier Eguiluz
 
Exploiting the newer perl to improve your plugins
Exploiting the newer perl to improve your pluginsExploiting the newer perl to improve your plugins
Exploiting the newer perl to improve your pluginsMarian Marinov
 
Writing Modular Command-line Apps with App::Cmd
Writing Modular Command-line Apps with App::CmdWriting Modular Command-line Apps with App::Cmd
Writing Modular Command-line Apps with App::CmdRicardo Signes
 
Evolving Software with Moose
Evolving Software with MooseEvolving Software with Moose
Evolving Software with MooseDave Cross
 
Good Evils In Perl
Good Evils In PerlGood Evils In Perl
Good Evils In PerlKang-min Liu
 
Your own (little) gem: building an online business with Ruby
Your own (little) gem: building an online business with RubyYour own (little) gem: building an online business with Ruby
Your own (little) gem: building an online business with RubyLindsay Holmwood
 

Semelhante a Object Exercise (20)

Simple Ways To Be A Better Programmer (OSCON 2007)
Simple Ways To Be A Better Programmer (OSCON 2007)Simple Ways To Be A Better Programmer (OSCON 2007)
Simple Ways To Be A Better Programmer (OSCON 2007)
 
PHP Unit Testing
PHP Unit TestingPHP Unit Testing
PHP Unit Testing
 
Perl web frameworks
Perl web frameworksPerl web frameworks
Perl web frameworks
 
Curscatalyst
CurscatalystCurscatalyst
Curscatalyst
 
Beijing Perl Workshop 2008 Hiveminder Secret Sauce
Beijing Perl Workshop 2008 Hiveminder Secret SauceBeijing Perl Workshop 2008 Hiveminder Secret Sauce
Beijing Perl Workshop 2008 Hiveminder Secret Sauce
 
Turbogears Presentation
Turbogears PresentationTurbogears Presentation
Turbogears Presentation
 
Capistrano2
Capistrano2Capistrano2
Capistrano2
 
Good Evils In Perl (Yapc Asia)
Good Evils In Perl (Yapc Asia)Good Evils In Perl (Yapc Asia)
Good Evils In Perl (Yapc Asia)
 
Impacta - Show Day de Rails
Impacta - Show Day de RailsImpacta - Show Day de Rails
Impacta - Show Day de Rails
 
Capistrano 2 Rocks My World
Capistrano 2 Rocks My WorldCapistrano 2 Rocks My World
Capistrano 2 Rocks My World
 
Real World Optimization
Real World OptimizationReal World Optimization
Real World Optimization
 
Ten modules I haven't yet talked about
Ten modules I haven't yet talked aboutTen modules I haven't yet talked about
Ten modules I haven't yet talked about
 
PHP tips and tricks
PHP tips and tricks PHP tips and tricks
PHP tips and tricks
 
Curso Symfony - Clase 2
Curso Symfony - Clase 2Curso Symfony - Clase 2
Curso Symfony - Clase 2
 
Exploiting the newer perl to improve your plugins
Exploiting the newer perl to improve your pluginsExploiting the newer perl to improve your plugins
Exploiting the newer perl to improve your plugins
 
Writing Modular Command-line Apps with App::Cmd
Writing Modular Command-line Apps with App::CmdWriting Modular Command-line Apps with App::Cmd
Writing Modular Command-line Apps with App::Cmd
 
Evolving Software with Moose
Evolving Software with MooseEvolving Software with Moose
Evolving Software with Moose
 
Good Evils In Perl
Good Evils In PerlGood Evils In Perl
Good Evils In Perl
 
Object Trampoline
Object TrampolineObject Trampoline
Object Trampoline
 
Your own (little) gem: building an online business with Ruby
Your own (little) gem: building an online business with RubyYour own (little) gem: building an online business with Ruby
Your own (little) gem: building an online business with Ruby
 

Mais de Workhorse Computing

Wheels we didn't re-invent: Perl's Utility Modules
Wheels we didn't re-invent: Perl's Utility ModulesWheels we didn't re-invent: Perl's Utility Modules
Wheels we didn't re-invent: Perl's Utility ModulesWorkhorse Computing
 
Paranormal statistics: Counting What Doesn't Add Up
Paranormal statistics: Counting What Doesn't Add UpParanormal statistics: Counting What Doesn't Add Up
Paranormal statistics: Counting What Doesn't Add UpWorkhorse Computing
 
The $path to knowledge: What little it take to unit-test Perl.
The $path to knowledge: What little it take to unit-test Perl.The $path to knowledge: What little it take to unit-test Perl.
The $path to knowledge: What little it take to unit-test Perl.Workhorse Computing
 
Generating & Querying Calendar Tables in Posgresql
Generating & Querying Calendar Tables in PosgresqlGenerating & Querying Calendar Tables in Posgresql
Generating & Querying Calendar Tables in PosgresqlWorkhorse Computing
 
Hypers and Gathers and Takes! Oh my!
Hypers and Gathers and Takes! Oh my!Hypers and Gathers and Takes! Oh my!
Hypers and Gathers and Takes! Oh my!Workhorse Computing
 
BSDM with BASH: Command Interpolation
BSDM with BASH: Command InterpolationBSDM with BASH: Command Interpolation
BSDM with BASH: Command InterpolationWorkhorse Computing
 
BASH Variables Part 1: Basic Interpolation
BASH Variables Part 1: Basic InterpolationBASH Variables Part 1: Basic Interpolation
BASH Variables Part 1: Basic InterpolationWorkhorse Computing
 
The W-curve and its application.
The W-curve and its application.The W-curve and its application.
The W-curve and its application.Workhorse Computing
 
Keeping objects healthy with Object::Exercise.
Keeping objects healthy with Object::Exercise.Keeping objects healthy with Object::Exercise.
Keeping objects healthy with Object::Exercise.Workhorse Computing
 
Perl6 Regexen: Reduce the line noise in your code.
Perl6 Regexen: Reduce the line noise in your code.Perl6 Regexen: Reduce the line noise in your code.
Perl6 Regexen: Reduce the line noise in your code.Workhorse Computing
 
Neatly Hashing a Tree: FP tree-fold in Perl5 & Perl6
Neatly Hashing a Tree: FP tree-fold in Perl5 & Perl6Neatly Hashing a Tree: FP tree-fold in Perl5 & Perl6
Neatly Hashing a Tree: FP tree-fold in Perl5 & Perl6Workhorse Computing
 

Mais de Workhorse Computing (20)

Wheels we didn't re-invent: Perl's Utility Modules
Wheels we didn't re-invent: Perl's Utility ModulesWheels we didn't re-invent: Perl's Utility Modules
Wheels we didn't re-invent: Perl's Utility Modules
 
mro-every.pdf
mro-every.pdfmro-every.pdf
mro-every.pdf
 
Paranormal statistics: Counting What Doesn't Add Up
Paranormal statistics: Counting What Doesn't Add UpParanormal statistics: Counting What Doesn't Add Up
Paranormal statistics: Counting What Doesn't Add Up
 
The $path to knowledge: What little it take to unit-test Perl.
The $path to knowledge: What little it take to unit-test Perl.The $path to knowledge: What little it take to unit-test Perl.
The $path to knowledge: What little it take to unit-test Perl.
 
Unit Testing Lots of Perl
Unit Testing Lots of PerlUnit Testing Lots of Perl
Unit Testing Lots of Perl
 
Generating & Querying Calendar Tables in Posgresql
Generating & Querying Calendar Tables in PosgresqlGenerating & Querying Calendar Tables in Posgresql
Generating & Querying Calendar Tables in Posgresql
 
Hypers and Gathers and Takes! Oh my!
Hypers and Gathers and Takes! Oh my!Hypers and Gathers and Takes! Oh my!
Hypers and Gathers and Takes! Oh my!
 
BSDM with BASH: Command Interpolation
BSDM with BASH: Command InterpolationBSDM with BASH: Command Interpolation
BSDM with BASH: Command Interpolation
 
Findbin libs
Findbin libsFindbin libs
Findbin libs
 
Memory Manglement in Raku
Memory Manglement in RakuMemory Manglement in Raku
Memory Manglement in Raku
 
BASH Variables Part 1: Basic Interpolation
BASH Variables Part 1: Basic InterpolationBASH Variables Part 1: Basic Interpolation
BASH Variables Part 1: Basic Interpolation
 
Effective Benchmarks
Effective BenchmarksEffective Benchmarks
Effective Benchmarks
 
Metadata-driven Testing
Metadata-driven TestingMetadata-driven Testing
Metadata-driven Testing
 
The W-curve and its application.
The W-curve and its application.The W-curve and its application.
The W-curve and its application.
 
Keeping objects healthy with Object::Exercise.
Keeping objects healthy with Object::Exercise.Keeping objects healthy with Object::Exercise.
Keeping objects healthy with Object::Exercise.
 
Perl6 Regexen: Reduce the line noise in your code.
Perl6 Regexen: Reduce the line noise in your code.Perl6 Regexen: Reduce the line noise in your code.
Perl6 Regexen: Reduce the line noise in your code.
 
Smoking docker
Smoking dockerSmoking docker
Smoking docker
 
Getting Testy With Perl6
Getting Testy With Perl6Getting Testy With Perl6
Getting Testy With Perl6
 
Neatly Hashing a Tree: FP tree-fold in Perl5 & Perl6
Neatly Hashing a Tree: FP tree-fold in Perl5 & Perl6Neatly Hashing a Tree: FP tree-fold in Perl5 & Perl6
Neatly Hashing a Tree: FP tree-fold in Perl5 & Perl6
 
Neatly folding-a-tree
Neatly folding-a-treeNeatly folding-a-tree
Neatly folding-a-tree
 

Último

CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilV3cube
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 

Último (20)

CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 

Object Exercise

  • 1. Object::Exercise Keeping your objects healthy. Steven Lembark  <slembark@cheetahmail.com>
  • 2. Objects Need Exercise ● Both in development and “normal” use. – Error & regression testing. – Benchmarking and capacity planning. – Setup for normal operations. – Bulk data processing. ● This is a description of how the  Object::Execute harness evolved.
  • 3. Common Execution Cycle ● Run a command. ● Check $@, optionally aborting if it is set. ● Compare the return value to a known one or  run a code snipped to evaluate it, optionally  aborting on a mismatched value. ● During development, set a breakpoint if the  operation or comparison fails. ● Run the next command...
  • 4. The Cycle is Repetitive ● We've all seen – or written – code like this: my $object = Some::Class­>construct( @argz ); my $sanity = ''; $sanity = $object­>first_method( @more_argz ); 'HASH' eq ref $sanity or die “Not a hashref...”; $sanity = $object­>second_part( @even_more_argz ); $DB::single = 1 unless $sanity; $sanity­>[0] eq 'foobar' or die “No foobar...” $sanity = $object­>third_part( @yet_more_argz ); $DB::single = 1 if 'ARRAY' ne ref $sanity; 'ARRAY' eq ref $sanity or die “Not an arrayref”; $sanity = $object­>third_part( 'a' .. 'z' );
  • 5. Replacing the Cruft ●  This is a good example MJD's “Red Flags”: – Most of the code is framework, probably updated  via cut+paste. – You can easily spend more time writing and  debugging the framework code than the module  itself. – It is difficult to generate the code so you end up  having to code it all manuall. ● The trick to cleaning it up is abstracting out  the framework and leaving the data.
  • 6. A better way: Lazy Exercise ● Being lazy means not doing something the  computer can do for you or having to do it  yourself more than once. ● Perl's OO handles this gracefully by allowing  $object­>$name( @argz ) notation: I can  store the method calls as data instead of  hard­coding them. ● This allows storing the operations as data  instead of code.
  • 7. Using Object::Exercise ● O::E is basically a “little language” for object  execution – a very little language. ● Instructions are either arrayref's that are  executed by the object or a small­ish set of  text instructions that control the harness. ● Upcoming improvement is allowing  Plugin::Installer to easily add add your own  instructions.
  • 8. Object Execution: Arrayref ● The most common thing you'll do is call a  method, possibly checking the results. ● To run a method and ignore anything other  than failure use: [ $method => @argz ] (i.e., a  method and list of arguments). ● $method can be either text or a coderef,  which allows dispatching to local handler  code.
  • 9. Pass 1: Storing Operations ● The previous code could be reduced to a list of  arrayref's each one with a method name (or  subref) and some arguments. ● All I need then is an object: sub pass1 { my $obj = shift; for( @_ ) { my $method = shift @$_; $obj­>$method( @$_ ) } }
  • 10. Dealing With Failure ● Adding an eval{} allows checking the results  and stopping execution on a failure: sub pass2 { my $obj = shift; for( @_ ) { my $method = shift @$_; eval { $obj­>$method( @$_ ) }; print $@ if $@; $DB::single = 1 if $@; 0 } }
  • 11. Taking a Closure Look ● Setting $DB::single = 1 in the Perl debugger  starts a breakpoint. ● $DB::single = 1 if $debug sets a breakpoint  conditional on $debug being perly­true. ● Adding “our $debug” means it can be  localized to true if $@ is set or or cmp_deeply  returns false. ● This allows “&$cmd” to stop right before the  method is called.
  • 12. sub pass3 { my $obj = shift; for( @_ ) { my ( $method, @argz ) = @$_; my $cmd = sub{ $obj­>$method( @argz ) }; eval { &$cmd }; if( $@ ) { print $@; $DB::single = 1; 0 # &$cmd here re­runs } } }
  • 13. Adding A Breakpoint ● I wanted to stop execution before dispatching  the method call on re­runs. But “&$cmd”  immediately runs the code. ● Adding a breakpoint to the closure allows  stopping before the method is called:
  • 14. our $debug = ''; sub pass3 { my $obj = shift; for( @_ ) { my ( $method, @argz ) = @$_; my $cmd = sub { $DB::single = 1 if $debug; $obj­>$method( @argz ) }; eval { &$cmd }; if( $@ ) { print $@; local $debug = 1; # &$cmd stops before the method. $DB::single  = 1;  0 # harness execution stops here. } } }
  • 15. Getting Testy About It ● Usually you will want to  check the results of  execution.  ● Instead of a single arrayref, I nested two of  them: one with the method the other  expected results: [ [ $method => @argz ], [ comparison value ], ]
  • 16. How methods are executed ● If there is data for comparison then the  return is saved, otherwise it can be ignored. for( @_ ) {     my ( $operation, $compare )     = 'ARRAY' eq ref $_­>[0] ? @{ $_ } : ( $_ );               ...               if( $@ )     { # set $debug, $DB::single = 1.     }     elsif( ! cmp_deeply $result, $compare, $message )     { # set $debug, $DB::single = 1.     }     elsif( $verbose )     { print ...     } }
  • 17. sub pass3 { my $obj = shift; for( @_ ) { my ( $method, @argz ) = @$_; my $cmd = sub { $DB::single = 1 if $debug; $obj­>$method( @argz ); }; eval { &$cmd }; if( $@ ) { print $@; local $debug = 1; # &$cmd stops at $obj­>method() $DB::single  = 1; 0 # execution stops here  } } }
  • 18. Doing it your way... ● Q: What if the execution requires more than  just $obj­>$method( @argz )? ● A: Use a coderef as the method. ● It will be called as $obj­>$coderef with a sanity check  for $@ afterward. This can be handy for updating the  object's internals as operations progress. [ [ make_hash => ( 'a' .. 'z' ) ], [ { 'a' .. 'z' } ] ], [ [ $reset_object_params, qw( verbose 1 ) ], [ ...
  • 19. Control statements ● There is a [very] little language for  controlling execution. ● These are non­arrayref entries in the list of  operations: – 'break' & 'continue' set $debug to true/false. – 'verbose' & 'quiet' set $verbose for log messages.
  • 20. The Result: Object::Exercise ● An early version is on CPAN. ● Supports benchmark mode (no comparison,  full speed execution w/ Benchmark &  :hireswallclock. ● Few more control statements. ● Plenty of POD.