SlideShare a Scribd company logo
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.

More Related Content

What's hot

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
 

What's hot (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
 

Viewers also liked

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
 

Viewers also liked (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
 

Similar to 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
 
Curscatalyst
CurscatalystCurscatalyst
CurscatalystKar Juan
 
Perl web frameworks
Perl web frameworksPerl web frameworks
Perl web frameworksdiego_k
 
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
 

Similar to 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
 
Curscatalyst
CurscatalystCurscatalyst
Curscatalyst
 
Perl web frameworks
Perl web frameworksPerl web frameworks
Perl web frameworks
 
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
 

More from 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
 

More from 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
 

Recently uploaded

Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Tobias Schneck
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonDianaGray10
 
Measures in SQL (a talk at SF Distributed Systems meetup, 2024-05-22)
Measures in SQL (a talk at SF Distributed Systems meetup, 2024-05-22)Measures in SQL (a talk at SF Distributed Systems meetup, 2024-05-22)
Measures in SQL (a talk at SF Distributed Systems meetup, 2024-05-22)Julian Hyde
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersSafe Software
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesThousandEyes
 
SOQL 201 for Admins & Developers: Slice & Dice Your Org’s Data With Aggregate...
SOQL 201 for Admins & Developers: Slice & Dice Your Org’s Data With Aggregate...SOQL 201 for Admins & Developers: Slice & Dice Your Org’s Data With Aggregate...
SOQL 201 for Admins & Developers: Slice & Dice Your Org’s Data With Aggregate...CzechDreamin
 
Exploring UiPath Orchestrator API: updates and limits in 2024 🚀
Exploring UiPath Orchestrator API: updates and limits in 2024 🚀Exploring UiPath Orchestrator API: updates and limits in 2024 🚀
Exploring UiPath Orchestrator API: updates and limits in 2024 🚀DianaGray10
 
Custom Approval Process: A New Perspective, Pavel Hrbacek & Anindya Halder
Custom Approval Process: A New Perspective, Pavel Hrbacek & Anindya HalderCustom Approval Process: A New Perspective, Pavel Hrbacek & Anindya Halder
Custom Approval Process: A New Perspective, Pavel Hrbacek & Anindya HalderCzechDreamin
 
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Product School
 
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...Product School
 
Search and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical FuturesSearch and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical FuturesBhaskar Mitra
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...Elena Simperl
 
Optimizing NoSQL Performance Through Observability
Optimizing NoSQL Performance Through ObservabilityOptimizing NoSQL Performance Through Observability
Optimizing NoSQL Performance Through ObservabilityScyllaDB
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...Product School
 
IESVE for Early Stage Design and Planning
IESVE for Early Stage Design and PlanningIESVE for Early Stage Design and Planning
IESVE for Early Stage Design and PlanningIES VE
 
"Impact of front-end architecture on development cost", Viktor Turskyi
"Impact of front-end architecture on development cost", Viktor Turskyi"Impact of front-end architecture on development cost", Viktor Turskyi
"Impact of front-end architecture on development cost", Viktor TurskyiFwdays
 
Introduction to Open Source RAG and RAG Evaluation
Introduction to Open Source RAG and RAG EvaluationIntroduction to Open Source RAG and RAG Evaluation
Introduction to Open Source RAG and RAG EvaluationZilliz
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...Sri Ambati
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...Product School
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaRTTS
 

Recently uploaded (20)

Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
 
Measures in SQL (a talk at SF Distributed Systems meetup, 2024-05-22)
Measures in SQL (a talk at SF Distributed Systems meetup, 2024-05-22)Measures in SQL (a talk at SF Distributed Systems meetup, 2024-05-22)
Measures in SQL (a talk at SF Distributed Systems meetup, 2024-05-22)
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
 
SOQL 201 for Admins & Developers: Slice & Dice Your Org’s Data With Aggregate...
SOQL 201 for Admins & Developers: Slice & Dice Your Org’s Data With Aggregate...SOQL 201 for Admins & Developers: Slice & Dice Your Org’s Data With Aggregate...
SOQL 201 for Admins & Developers: Slice & Dice Your Org’s Data With Aggregate...
 
Exploring UiPath Orchestrator API: updates and limits in 2024 🚀
Exploring UiPath Orchestrator API: updates and limits in 2024 🚀Exploring UiPath Orchestrator API: updates and limits in 2024 🚀
Exploring UiPath Orchestrator API: updates and limits in 2024 🚀
 
Custom Approval Process: A New Perspective, Pavel Hrbacek & Anindya Halder
Custom Approval Process: A New Perspective, Pavel Hrbacek & Anindya HalderCustom Approval Process: A New Perspective, Pavel Hrbacek & Anindya Halder
Custom Approval Process: A New Perspective, Pavel Hrbacek & Anindya Halder
 
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...
 
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
 
Search and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical FuturesSearch and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical Futures
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
 
Optimizing NoSQL Performance Through Observability
Optimizing NoSQL Performance Through ObservabilityOptimizing NoSQL Performance Through Observability
Optimizing NoSQL Performance Through Observability
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
 
IESVE for Early Stage Design and Planning
IESVE for Early Stage Design and PlanningIESVE for Early Stage Design and Planning
IESVE for Early Stage Design and Planning
 
"Impact of front-end architecture on development cost", Viktor Turskyi
"Impact of front-end architecture on development cost", Viktor Turskyi"Impact of front-end architecture on development cost", Viktor Turskyi
"Impact of front-end architecture on development cost", Viktor Turskyi
 
Introduction to Open Source RAG and RAG Evaluation
Introduction to Open Source RAG and RAG EvaluationIntroduction to Open Source RAG and RAG Evaluation
Introduction to Open Source RAG and RAG Evaluation
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
 

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.