File: HandlerLibrary.pm

package info (click to toggle)
libsub-handlesvia-perl 0.052000-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,740 kB
  • sloc: perl: 9,645; makefile: 2
file content (62 lines) | stat: -rw-r--r-- 1,073 bytes parent folder | download
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
use 5.008;
use strict;
use warnings;

package Sub::HandlesVia::HandlerLibrary;

our $AUTHORITY = 'cpan:TOBYINK';
our $VERSION   = '0.052000';

use Types::Standard qw( Any Item );

sub _type_inspector {
	my ($me, $type) = @_;
	if (!$type or $type == Any or $type == Item) {
		return {
			trust_mutated => 'always',
		};
	}
	
	return { trust_mutated => 'never' };
}

{
	my %cache;
	
	sub get_handler {
		my ($me, $handler_name) = @_;
		$cache{$me} ||= $me->_populate_cache;
		$cache{$me}{$handler_name} ? $me->$handler_name : undef;
	}
	
	sub has_handler {
		my ($me, $handler_name) = @_;
		$cache{$me} ||= $me->_populate_cache;
		exists $cache{$me}{$handler_name};
	}
}

# This is not necessarily an exhaustive list, however if it is non-exhaustive
# then subclasses must override get_handler and has_handler.
#
sub handler_names {
	no strict 'refs';
	@{ $_[0] . '::METHODS' }
}

sub _populate_cache {
	my %hash;
	$hash{$_} = 1 for $_[0]->handler_names;
	\%hash;
}

sub expand_shortcut {
	use Carp;
	Carp::croak( "Not implemented" );
}

sub preprocess_spec {
	return;
}

1;