Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 85 lines (68 sloc) 2.475 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
#!/usr/bin/env perl
# vim: ts=2 sw=2 noexpandtab

# This example illustrates implicit and explicit callbacks via plain
# coderefs. Coderef callbacks are clear and concise. They allow
# developers to take advantage of closure tricks, including
# implementing a form of continuation-passing style.
#
# They are less suitable for object-oriented programs. See most other
# forms of Reflex::Callback for more object oriented callbacks.

# Reflex::Callbacks and the Reflex::Callback helper classes will
# abstract callbacks to fulfill the following goals:
#
# 1. A module's consumer decides how it will be called back.
# 2. Module implementations will use a single interface that
# represents the abstract notion of callbacks. Consumers' chosen
# callback implementations will handle the rest.
# 3. Every known form of callback will be supported, so that module
# consumers aren't limited to a single, possibly undesirable
# callback mechanism.
# 4. Common callback mechanisms may be specified by concise,
# contextual syntax.
# 5. All callback mechanisms may be specified by slightly verbose but
# unambiguous syntax.

# Ideally all the eg-*-rcb-*.pl examples will use the identical
# ThingWithCallbacks. That class will have no custom callback logic
# at all.

use warnings;
use strict;
use lib qw(t/lib);

use Test::More tests => 7;

use Reflex::Callbacks qw(cb_coderef);
use ThingWithCallbacks;

# Create a thing that will invoke callbacks.
# This syntax uses contextually specified coderef callbacks.
# Circular reference on $thing_one leaks memory.

my $thing_one;
$thing_one = ThingWithCallbacks->new(
on_event => sub {
pass("contextual callback invoked");
is($_[0], $thing_one, "contextual callback got self");
},
);

$thing_one->run();

# cb_coderef() reduces context sensitivity at the expense of
# verbosity.
# Circular reference on $thing_two leaks memory.

my $thing_two;
$thing_two = ThingWithCallbacks->new(
on_event => cb_coderef(
sub {
is($_[0], $thing_two, "explicit callback got self");
pass("explicit callback invoked");
}
),
);

$thing_two->run();

# cb_coderef is prototyped so it can replace "sub".
# Circular reference on $thing_three leaks memory.

my $thing_three;
$thing_three = ThingWithCallbacks->new(
on_event => cb_coderef {
is($_[0], $thing_three, "explicit callback (no sub) got self");
pass("explicit callback (no sub) invoked");
},
);

$thing_three->run();
pass("object ran to completion");

exit;
Something went wrong with that request. Please try again.