annotate Lib/IMPL/Code/Binding.pm @ 225:a1e868b0fba9

Bindings concept in progress
author sergey
date Fri, 31 Aug 2012 16:41:18 +0400
parents
children b6cde007a175
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
a1e868b0fba9 Bindings concept in progress
sergey
parents:
diff changeset
14 die ArgumentException( vars => 'A reference to an array is required')
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
a1e868b0fba9 Bindings concept in progress
sergey
parents:
diff changeset
28 my $body = eval $code; #$compiler_env->reval($code,'strict');
a1e868b0fba9 Bindings concept in progress
sergey
parents:
diff changeset
29
a1e868b0fba9 Bindings concept in progress
sergey
parents:
diff changeset
30
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
a1e868b0fba9 Bindings concept in progress
sergey
parents:
diff changeset
41 C<IMPL::Code::Binding> - превращает выражения в связыватель
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(
a1e868b0fba9 Bindings concept in progress
sergey
parents:
diff changeset
54 [qw(obj)] => q{ $obj->addresses->[0]->country->code }
a1e868b0fba9 Bindings concept in progress
sergey
parents:
diff changeset
55 );
a1e868b0fba9 Bindings concept in progress
sergey
parents:
diff changeset
56
a1e868b0fba9 Bindings concept in progress
sergey
parents:
diff changeset
57 print $bind->($person);
a1e868b0fba9 Bindings concept in progress
sergey
parents:
diff changeset
58
a1e868b0fba9 Bindings concept in progress
sergey
parents:
diff changeset
59 =end
a1e868b0fba9 Bindings concept in progress
sergey
parents:
diff changeset
60
a1e868b0fba9 Bindings concept in progress
sergey
parents:
diff changeset
61 =head1 DESCRIPTION
a1e868b0fba9 Bindings concept in progress
sergey
parents:
diff changeset
62
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 в конструктор передается выражение связывания, ссылка насписок из параметров
a1e868b0fba9 Bindings concept in progress
sergey
parents:
diff changeset
69 и могут быть переданы связи для копозиции.
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
a1e868b0fba9 Bindings concept in progress
sergey
parents:
diff changeset
79 =head2 C<new($expression,\@vars,\%bindings)>
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