| 1 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | package Decl::EventContext; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 13 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 13 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 13 | ▶ | use Decl::Semantics::Code; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 13 | ▶ | use Text::ParseWords; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 13 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | Decl::EventContext - base class implementing an event context in a declarative structure. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | =head1 VERSION | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | Version 0.01 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 13 | our $VERSION = '0.01'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | Each node in a C<Decl> structure that can respond to events can inherit from this class to get the proper machinery in place. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | =head2 event_context_init() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | Called during object creation to set up fields and such. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | 29 | ▶ | sub event_context_init { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
event_context_init: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 29 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | =head2 event_context() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | Returns $self. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 67 | ▶ | sub event_context { $_[0] } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
event_context: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | =head2 register_event($event, $closure), do ($event) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | Registers and fires closures by name. This is the mechanism used by the 'on' tag in the core semantics. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | This is actually a command-line interface; C<fire> runs the L<Text::ParseWords> C<parse_line> function on its | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | input, and gives the event closure any list elements that come after the first word. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 4 | ▶ | sub register_event { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
register_event: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 4 | my ($self, $event, $closure) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 4 | $self->{e}->{$event} = $closure; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 8 | ▶ | sub do { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
do: called CC=4 SCAR=16.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | 8 | my ($self, $command) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | 8 | my @words = parse_line ('\s+', 0, $command); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 8 | my $event = shift @words; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 8 | my $e = $self->{e}->{$event}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 8 | ▶ | if ($e) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 8 | my $r = eval { &$e($self, @words) }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | 8 | ▶ | print STDERR $@ if $@; # TODO: centralized error handling. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | 8 | return $r; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 0 | ▶ | if ($self->parent) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 0 | my $cx = $self->parent->event_context(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 0 | return ($cx->do($command)); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | =head2 make_event | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | Given the name of a C<Decl> event, finds the code referred to in its callable closure. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | TODO: this is not covered by unit testing! | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | 0 | ▶ | sub make_event { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
make_event: not called CC=9 SCAR=45.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | 0 | my ($self, $item) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | # Does the item have a body or children? Then use Decl::Semantics::Code to build code for it. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | # Note: the flag $is_event registers the item as a named event, if it has a name. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | 0 | Decl::Semantics::Code::build_payload ($item, 1); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | 0 | ▶ | return $item->{sub} if $item->{callable}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | # Does the item have an appropriately named 'on' handler? Then build that and use it. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | # Search up the tree to inherit parents' 'on' handlers. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | 0 | ▶ | for (my $cursor = $item; $cursor; $cursor = $cursor->parent()) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 0 | foreach ($cursor->nodes) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | 0 | ▶ | $_->build if $_->is('on'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | 0 | ▶ | if ($_->is('on') and ($_->name eq $item->name) and $_->can('build') and my $handler = $_->build) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/9
$_->is("on") and $_->name eq $item->name and $_->can("build") and my $handler = $_->build
$_->is("on") and $_->name eq $item->name and $_->can("build")
$_->is("on") and $_->name eq $item->name
MC/DC0%0/4
$_->is("on") and $_->name eq $item->name and $_->can("build") and my $handler = $_->build
$_->is("on")
$_->name eq $item->name
$_->can("build")
my $handler = $_->build
Truth table0%0/5
A and B and C and D
A= $_->is("on")B= $_->name eq $item->nameC= $_->can("build")D= my $handler = $_->build
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | 0 | $self->register_event($item->name, $handler); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | 0 | return $handler; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | # If all else fails, build a stub. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | 0 | ▶ | my $closure = sub { print "event " . $item->name . "\n"; }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | 0 | $self->register_event($item->name, $closure); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | 0 | return $closure; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | =head2 semantics() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | Each event context can return a semantic handler. For example, a form knows that its core semantics are "wx"; a Word document knows that | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | its core semantics are "ms-word", and so on. The semantic handlers are a good place to put common functionality for a given semantic | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | domain, so they're useful in code snippets in a given context. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | The default is to return the core semantics. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | 23 | ▶ | sub semantics { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
semantics: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | 23 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | 23 | $self->root()->semantic_handler('core'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | Michael Roberts, C<< <michael at vivtek.com> >> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | =head1 BUGS | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | Please report any bugs or feature requests to C<bug-decl at rt.cpan.org>, or through | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Decl>. I will be notified, and then you'll | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | automatically be notified of progress on your bug as I make changes. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | Copyright 2010 Michael Roberts. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | This program is free software; you can redistribute it and/or modify it | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | under the terms of either: the GNU General Public License as published | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | by the Free Software Foundation; or the Artistic License. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | See http://dev.perl.org/licenses/ for more information. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | 13 | 1; # End of Decl::EventContext | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||