Declare/DSL.pm

filter: Statementstmt 85.7% 12 / 14 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod n/a totaltotal 89.4% 17 / 19 CC 1 SCAR 0.6
CC
1
Cov
86%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« Declare.pm ↑ Summary Declare/ZDCF/Encoder/DumpEval.pm »
1 package ZMQ::Declare::DSL;
2 {
3 $ZMQ::Declare::DSL::VERSION = '0.03';
4 }
5  
6 1 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 0 ▶use ZeroMQ ();
Subroutine BEGIN: called CC=1 SCAR=2.6
12  
13 use ZMQ::Declare;
14  
15 require Exporter;
16 our @ISA = qw(Exporter);
17  
18 our @EXPORT = qw(
19 declare_zdcf
20 app
21 context
22 iothreads
23 device
24 name
25 type
26 sock
27 bnd
28 conn
29 option
30 );
31 our %EXPORT_TAGS = (
32 'all' => \@EXPORT,
33 );
34  
35 our $ZDCF;
36 our $App;
37 our $Context;
38 our $Device;
39 our $Socket;
40 # Valid scopes: zdcf, app, context, device, socket
41 our $CurScope;
42  
43 sub _check_scope {
44 my $expected_scope = shift;
45 my $global_state_var_ref = shift;
46 (undef, undef, undef, my $function) = caller(1);
47 $function =~ s/^.*:://;
48  
49 Carp::croak("'$function' outside ZDCF found: Did you fail to call 'declare_zdcf'?")
50 if not defined $CurScope;
51 Carp::croak("Wrongly nested or out-of-place '$function' detected: You cannot nest ZDCF '${function}'s")
52 if ($global_state_var_ref && $$global_state_var_ref)
53 or $CurScope ne $expected_scope;
54 }
55  
56 sub declare_zdcf(&) {
57 local $ZDCF = {version => "1.0"};
58 local $CurScope = 'zdcf';
59 # Clear the various components in this dynamic scope
60 local $App;
61 local $Context;
62 local $Device;
63 local $Socket;
64 $_[0]->();
65 return ZMQ::Declare::ZDCF->new(tree => $ZDCF);
66 }
67  
68 sub app(&) {
69 _check_scope('zdcf', \$App);
70  
71 local $CurScope = 'app';
72 local $App = {};
73 $_[0]->();
74  
75 my $name = delete $App->{name};
76 Carp::croak("Missing app name!") if not defined $name;
77 $ZDCF->{apps}{$name} = $App;
78 }
79  
80 sub context(&) {
81 _check_scope('app' => \$Context);
82  
83 local $Context = {};
84 local $CurScope = 'context';
85 $_[0]->();
86 $App->{context} = $Context;
87 }
88  
89 sub iothreads($) {
90 _check_scope('context');
91 $Context->{iothreads} = $_[0];
92 }
93  
94 sub device(&) {
95 _check_scope('app' => \$Device);
96  
97 local $Device = {};
98 local $CurScope = 'device';
99 $_[0]->();
100  
101 my $name = delete $Device->{name};
102 if (not defined $name) {
103 $name = $App->{name};
104 }
105  
106 Carp::croak("Missing device name!") if not defined $name;
107 $App->{devices}{$name} = $Device;
108 }
109  
110 sub name($) {
111 if (not defined $CurScope) {
112 Carp::croak("Error 'name()' outside app, device, and socket. Did you fail to call 'declare_zdcf'?");
113 }
114 elsif ($CurScope eq 'device') { $Device->{name} = shift }
115 elsif ($CurScope eq 'socket') { $Socket->{name} = shift }
116 elsif ($CurScope eq 'app') { $App->{name} = shift }
117 else { Carp::croak("Error 'name()' outside app, device, and socket") }
118 }
119  
120 sub type($) {
121 if (not defined $CurScope) {
122 Carp::croak("Error 'type()' outside device, and socket. Did you fail to call 'declare_zdcf'?");
123 }
124 elsif ($CurScope eq 'device') { $Device->{type} = shift }
125 elsif ($CurScope eq 'socket') { $Socket->{type} = shift }
126 else { Carp::croak("Error 'type()' outside device, and socket") }
127 }
128  
129 sub sock(&) {
130 _check_scope('device' => \$Socket);
131  
132 local $Socket = {};
133 local $CurScope = 'socket';
134 $_[0]->();
135  
136 my $name = delete $Socket->{name};
137 Carp::croak("Missing socket name!") if not defined $name;
138 $Device->{sockets}{$name} = $Socket;
139 }
140  
141 sub bnd(@) {
142 Carp::croak("Error: bnd (bind) outside socket")
143 if not defined $CurScope or $CurScope ne 'socket';
144 push @{ $Socket->{bind} }, @_;
145 }
146  
147 sub conn(@) {
148 Carp::croak("Error: conn (connect) outside socket")
149 if not defined $CurScope or $CurScope ne 'socket';
150 push @{ $Socket->{connect} }, @_;
151 }
152  
153 sub option(%) {
154 Carp::croak("Error: option() outside socket")
155 if not defined $CurScope or $CurScope ne 'socket';
156 while (@_) {
157 my $k = shift;
158 $Socket->{option}->{$k} = shift;
159 }
160 }
161  
162 1;
163 __END__
« Declare.pm ↑ Summary Declare/ZDCF/Encoder/DumpEval.pm »