Richard Clamp > Class-Delay-0.02 > Class::Delay



Annotate this POD

View/Report Bugs
Module Version: 0.02   Source  


Class::Delay - delay method dispatch until a trigerring event


 package PrintOut;
 sub write {
    my $self = shift;
    print "printing: ", @_, "\n";
 sub flush {
    print "flushed\n";

 package DelayedPrint;
 use base 'PrintOut';
 use Class::Delay
    methods => [ 'write' ],
    release => [ 'flush' ];

 package main;

 DelayedPrint->write( "we'll write this later" ); # won't get through
                                                  # to PrintOuts 'write' yet
 DelayedPrint->write( "this too" );
 DelayedPrint->flush;  # all of the queued call are dispatched
 DelayedPrint->write( "this won't be delayed" );


Class::Delay provides a mechanism for the delay of method dispatch until a triggering method is called.

You simply define a proxy class, and then call on the module to set up a set of methods that will defer.


The use statement takes the following options when generate the proxying behaviour.


An array reference naming the methods to delay until a trigger event.


What a delayed method will return, defaults to 1.


An array reference naming the methods to ise as triggering events.


A subroutine that will be passed the all the delayed messages as Class::Delay::Message objects. The routine should return these in the order that the messagages are to be deilvered.

For example, if we want to ensure our trigger event arrives before the rest of the events we may do something like this:

 use Class::Delay
    methods => [qw( foo bar baz )],
    release => [qw( quux squirkle )],
    reorder => sub { sort { $b->is_trigger <=> $a->is_trigger } @_ };

An extended example of this module is in Mariachi::DBI which uses the module to delay database setup until the final of the database is known.


Richard Clamp <>


Copyright (C) 2003 Richard Clamp. All Rights Reserved.


syntax highlighting: