annotate Lib/IMPL/Code/Binding.pm @ 253:0a228a35645c

added application exception class
author sergey
date Mon, 19 Nov 2012 17:54:01 +0400
parents 6d8092d8ce1b
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
225
a1e868b0fba9 Bindings concept in progress
sergey
parents:
diff changeset
1 package IMPL::Code::Binding;
a1e868b0fba9 Bindings concept in progress
sergey
parents:
diff changeset
2 use strict;
a1e868b0fba9 Bindings concept in progress
sergey
parents:
diff changeset
3
a1e868b0fba9 Bindings concept in progress
sergey
parents:
diff changeset
4 use IMPL::require {
a1e868b0fba9 Bindings concept in progress
sergey
parents:
diff changeset
5 Exception => 'IMPL::Exception',
a1e868b0fba9 Bindings concept in progress
sergey
parents:
diff changeset
6 ArgumentException => '-IMPL::ArgumentException'
a1e868b0fba9 Bindings concept in progress
sergey
parents:
diff changeset
7 };
a1e868b0fba9 Bindings concept in progress
sergey
parents:
diff changeset
8
a1e868b0fba9 Bindings concept in progress
sergey
parents:
diff changeset
9 sub new {
a1e868b0fba9 Bindings concept in progress
sergey
parents:
diff changeset
10 my ($self,$expr,$vars) = @_;
a1e868b0fba9 Bindings concept in progress
sergey
parents:
diff changeset
11
a1e868b0fba9 Bindings concept in progress
sergey
parents:
diff changeset
12 $vars ||= [];
a1e868b0fba9 Bindings concept in progress
sergey
parents:
diff changeset
13
230
6d8092d8ce1b *reworked IMPL::Security
sergey
parents: 226
diff changeset
14 die ArgumentException->new( vars => 'A reference to an array is required')
225
a1e868b0fba9 Bindings concept in progress
sergey
parents:
diff changeset
15 unless ref $vars eq 'ARRAY';
a1e868b0fba9 Bindings concept in progress
sergey
parents:
diff changeset
16
a1e868b0fba9 Bindings concept in progress
sergey
parents:
diff changeset
17 m/^\w+$/ or die ArgumentException->new( vars => 'A valid variable name is required', $_ )
a1e868b0fba9 Bindings concept in progress
sergey
parents:
diff changeset
18 foreach @$vars;
a1e868b0fba9 Bindings concept in progress
sergey
parents:
diff changeset
19
a1e868b0fba9 Bindings concept in progress
sergey
parents:
diff changeset
20 my $varnames = join (',', map { "\$$_" } @$vars);
a1e868b0fba9 Bindings concept in progress
sergey
parents:
diff changeset
21
a1e868b0fba9 Bindings concept in progress
sergey
parents:
diff changeset
22 my $code = <<CODE;
a1e868b0fba9 Bindings concept in progress
sergey
parents:
diff changeset
23 sub {
a1e868b0fba9 Bindings concept in progress
sergey
parents:
diff changeset
24 my ($varnames) = \@_;
a1e868b0fba9 Bindings concept in progress
sergey
parents:
diff changeset
25 $text
a1e868b0fba9 Bindings concept in progress
sergey
parents:
diff changeset
26 }
a1e868b0fba9 Bindings concept in progress
sergey
parents:
diff changeset
27 CODE
226
b6cde007a175 Added resource contract
sergey
parents: 225
diff changeset
28 my $method = eval $code; #$compiler_env->reval($code,'strict');
225
a1e868b0fba9 Bindings concept in progress
sergey
parents:
diff changeset
29
226
b6cde007a175 Added resource contract
sergey
parents: 225
diff changeset
30 return $method;
225
a1e868b0fba9 Bindings concept in progress
sergey
parents:
diff changeset
31 }
a1e868b0fba9 Bindings concept in progress
sergey
parents:
diff changeset
32
a1e868b0fba9 Bindings concept in progress
sergey
parents:
diff changeset
33 1;
a1e868b0fba9 Bindings concept in progress
sergey
parents:
diff changeset
34
a1e868b0fba9 Bindings concept in progress
sergey
parents:
diff changeset
35 __END__
a1e868b0fba9 Bindings concept in progress
sergey
parents:
diff changeset
36
a1e868b0fba9 Bindings concept in progress
sergey
parents:
diff changeset
37 =pod
a1e868b0fba9 Bindings concept in progress
sergey
parents:
diff changeset
38
a1e868b0fba9 Bindings concept in progress
sergey
parents:
diff changeset
39 =head1 NAME
a1e868b0fba9 Bindings concept in progress
sergey
parents:
diff changeset
40
226
b6cde007a175 Added resource contract
sergey
parents: 225
diff changeset
41 C<IMPL::Code::Binding> - превращает выражения в процедуру.
225
a1e868b0fba9 Bindings concept in progress
sergey
parents:
diff changeset
42
a1e868b0fba9 Bindings concept in progress
sergey
parents:
diff changeset
43 =head1 SYNOPSIS
a1e868b0fba9 Bindings concept in progress
sergey
parents:
diff changeset
44
a1e868b0fba9 Bindings concept in progress
sergey
parents:
diff changeset
45 =begin code
a1e868b0fba9 Bindings concept in progress
sergey
parents:
diff changeset
46
a1e868b0fba9 Bindings concept in progress
sergey
parents:
diff changeset
47 use IMPL::require {
a1e868b0fba9 Bindings concept in progress
sergey
parents:
diff changeset
48 Binding => 'IMPL::Code::Binding'
a1e868b0fba9 Bindings concept in progress
sergey
parents:
diff changeset
49 }
a1e868b0fba9 Bindings concept in progress
sergey
parents:
diff changeset
50
a1e868b0fba9 Bindings concept in progress
sergey
parents:
diff changeset
51 my $person = DB->SearchPerson({name => 'Peter'})->First;
a1e868b0fba9 Bindings concept in progress
sergey
parents:
diff changeset
52
a1e868b0fba9 Bindings concept in progress
sergey
parents:
diff changeset
53 my $bind = Binding->new(
226
b6cde007a175 Added resource contract
sergey
parents: 225
diff changeset
54 [qw(obj)] =>
b6cde007a175 Added resource contract
sergey
parents: 225
diff changeset
55 q{ $obj->addresses->[0]->country->code }
225
a1e868b0fba9 Bindings concept in progress
sergey
parents:
diff changeset
56 );
a1e868b0fba9 Bindings concept in progress
sergey
parents:
diff changeset
57
a1e868b0fba9 Bindings concept in progress
sergey
parents:
diff changeset
58 print $bind->($person);
a1e868b0fba9 Bindings concept in progress
sergey
parents:
diff changeset
59
a1e868b0fba9 Bindings concept in progress
sergey
parents:
diff changeset
60 =end
a1e868b0fba9 Bindings concept in progress
sergey
parents:
diff changeset
61
a1e868b0fba9 Bindings concept in progress
sergey
parents:
diff changeset
62 =head1 DESCRIPTION
a1e868b0fba9 Bindings concept in progress
sergey
parents:
diff changeset
63
a1e868b0fba9 Bindings concept in progress
sergey
parents:
diff changeset
64 Позвоялет преобразовать выражение в функцию, которую можно будет многократно
a1e868b0fba9 Bindings concept in progress
sergey
parents:
diff changeset
65 использовать для получения значения выражения.
a1e868b0fba9 Bindings concept in progress
sergey
parents:
diff changeset
66
a1e868b0fba9 Bindings concept in progress
sergey
parents:
diff changeset
67 Выражение параметризуется произвольным количеством именованных параметров,
a1e868b0fba9 Bindings concept in progress
sergey
parents:
diff changeset
68 которые будут доступны внутри выражения как переменные. При создании связывателя
226
b6cde007a175 Added resource contract
sergey
parents: 225
diff changeset
69 в конструктор передается выражение связывания, ссылка насписок из параметров.
225
a1e868b0fba9 Bindings concept in progress
sergey
parents:
diff changeset
70
a1e868b0fba9 Bindings concept in progress
sergey
parents:
diff changeset
71 При создания связывателя будет проверен синтаксис, и если в выражении допущена
a1e868b0fba9 Bindings concept in progress
sergey
parents:
diff changeset
72 ошибка, возникнет исключение.
a1e868b0fba9 Bindings concept in progress
sergey
parents:
diff changeset
73
a1e868b0fba9 Bindings concept in progress
sergey
parents:
diff changeset
74 Данный класс не является безопасным при создании связывателей из ненадежных
a1e868b0fba9 Bindings concept in progress
sergey
parents:
diff changeset
75 источников, поскольку внутри будет выполнен C<eval>.
a1e868b0fba9 Bindings concept in progress
sergey
parents:
diff changeset
76
a1e868b0fba9 Bindings concept in progress
sergey
parents:
diff changeset
77 =head1 MEMBERS
a1e868b0fba9 Bindings concept in progress
sergey
parents:
diff changeset
78
226
b6cde007a175 Added resource contract
sergey
parents: 225
diff changeset
79 =head2 C<new(\@vars,$expression)>
225
a1e868b0fba9 Bindings concept in progress
sergey
parents:
diff changeset
80
a1e868b0fba9 Bindings concept in progress
sergey
parents:
diff changeset
81 Возвращает ссылку на процедуру.
a1e868b0fba9 Bindings concept in progress
sergey
parents:
diff changeset
82
a1e868b0fba9 Bindings concept in progress
sergey
parents:
diff changeset
83 =cut