XML/Axk/Language.pm

filter: Statementstmt 42.2% 19 / 45 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 85.7% 6 / 7 Podpod n/a totaltotal 25.2% 25 / 99 CC 18 SCAR 54.2
CC
18
Cov
23%
import
299.8
BEGIN
1.0
BEGIN
1.0
CRAP 225.1
↑ Summary XML/Axk/Core.pm »
1 # XML::Axk::Language - common definitions for axk language modules (X::A::Ln).
2  
3 package XML::Axk::Language;
4 2 ▶use XML::Axk::Base ':all';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Import::Into;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use vars;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 ▶use XML::Axk::Vars::Scalar;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use XML::Axk::Vars::Array;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 #use Devel::StackTrace;
12  
13 # Registry mapping XALn package names to arrayrefs of the script parameters
14 # (SPs) for those packages.
15 #our %SP_Registry = ();
16  
17 # Export ========================================================= {{{1
18 sub import {
19 0 ▶ my $class = shift;
Subroutine import: not called CC=18 SCAR=57.0
20 0 my $lang = caller;
21 0 ▶ if($class ne __PACKAGE__) { # do I need this?
Branch: 1 branch
BranchTrueFalseTotal
if ($class ne __PACKAGE__) 0 0 0
22 0 confess(__PACKAGE__ . " initializer called from class $class");
23 }
24  
25 #say "XALanguage run from $target:\n", Devel::StackTrace->new->as_string;
26  
27 # Set up the registry
28 #$SP_Registry{$lang} = [] unless exists $SP_Registry{$lang};
29 #my $lrRegistry = $SP_Registry{$lang};
30  
31 0 ▶ my %opts = ( $#_>0 && $#_ ? @_ : () ); # even number of args => options
Branch: 1 branch
BranchTrueFalseTotal
$#_ > 0 && $#_ ? : 0 0 0
Condition0%0/3
$#_ > 0 and $#_
!ll&&!rl&&r
000
MC/DC0%0/2
$#_ > 0 and $#_
$#_ > 0 $#_
Truth table0%0/3
A and B
A= $#_ > 0B= $#_
ABresult
0X0
100
111
32  
33 0 my (@sps, $drUpdater); # option values for sp, updater
34 0 my $sandbox; # _AxkSandbox in the target, if any
35  
36 # sp: populate the registry with the given variables.
37 0 ▶ if(exists $opts{sp} && $opts{sp}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts{"sp"} and $opts{"sp"}) 0 0 0
Condition0%0/3
exists $opts{"sp"} and $opts{"sp"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $opts{"sp"} and $opts{"sp"}
exists $opts{"sp"} $opts{"sp"}
Truth table0%0/3
A and B
A= exists $opts{"sp"}B= $opts{"sp"}
ABresult
0X0
100
111
38 0 ▶ croak "Need an arrayref for the SPs" unless ref $opts{sp} eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $opts{"sp"} eq "ARRAY" 0 0 0
39 0 @sps = @{$opts{sp}};
40 } #`sp` option
41  
42 # updater: the function that updates the SPs each record
43 0 ▶ if(exists $opts{updater} && $opts{updater}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts{"updater"} and $opts{"updater"}) 0 0 0
Condition0%0/3
exists $opts{"updater"} and $opts{"updater"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $opts{"updater"} and $opts{"updater"}
exists $opts{"updater"} $opts{"updater"}
Truth table0%0/3
A and B
A= exists $opts{"updater"}B= $opts{"updater"}
ABresult
0X0
100
111
44 0 ▶ croak "Need a code ref for the SPs" unless ref $opts{updater} eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $opts{"updater"} eq "CODE" 0 0 0
45 # TODO permit methods?
46 0 $drUpdater = $opts{updater};
47 } #`sp` option
48  
49 # target: mark the given axk_script (if any)
50 0 ▶ if(exists $opts{target} && $opts{target}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts{"target"} and $opts{"target"}) 0 0 0
Condition0%0/3
exists $opts{"target"} and $opts{"target"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $opts{"target"} and $opts{"target"}
exists $opts{"target"} $opts{"target"}
Truth table0%0/3
A and B
A= exists $opts{"target"}B= $opts{"target"}
ABresult
0X0
100
111
51  
52 0 my $target = $opts{target};
53 0 my $sandboxname = "${target}::_AxkSandbox";
54  
55 #say "Loading $varname with $lang";
56 0 do {
57 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
58 #croak "A script can only use one language" if defined $$varname;
59  
60 0 ▶ $sandbox = $$sandboxname if defined $$sandboxname;
Branch: 1 branch
BranchTrueFalseTotal
if defined $$sandboxname 0 0 0
61 };
62  
63 # Inject the script parameters
64 0 ▶ if(@sps) {
Branch: 1 branch
BranchTrueFalseTotal
if (@sps) 0 0 0
65 0 vars->import::into($target, @sps); # Create the SP vars...
66  
67 0 ▶ if($sandbox) { # ... then link them to storage in the core.
Branch: 1 branch
BranchTrueFalseTotal
if ($sandbox) 0 0 0
68 0 $sandbox->allocate_sps($lang, @sps);
69 0 $sandbox->inject_var($lang, $_) for @sps;
70 }
71 }
72  
73 # Set the updater.
74 0 ▶ if($sandbox && $drUpdater) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sandbox and $drUpdater) 0 0 0
Condition0%0/3
$sandbox and $drUpdater
!ll&&!rl&&r
000
MC/DC0%0/2
$sandbox and $drUpdater
$sandbox $drUpdater
Truth table0%0/3
A and B
A= $sandboxB= $drUpdater
ABresult
0X0
100
111
75 0 $sandbox->set_updater($lang, $drUpdater);
76 }
77  
78 } #`target` option
79  
80 } #import()
81  
82 # }}}1
83 2 1;
84 # === Documentation ===================================================== {{{2
85  
86 =pod
87  
88 =encoding UTF-8
89  
90 =head1 NAME
91  
92 XML::Axk::Language - base language code for the axk XML processor
93  
94 =head1 VERSION
95  
96 Version 0.01
97  
98 =head1 USAGE
99  
100 When implementing a language:
101  
102 require XML::Axk::Language;
103 sub import {
104 XML::Axk::Language->import( target => caller, sp => [qw($foo @bar)],
105 updater => sub { ... } );
106 }
107  
108 If all you need is the registry:
109  
110 use XML::Axk::Language ();
111 # Then do something with @XML::Axk::Language::SP_Registry.
112  
113 =head1 OPTIONS
114  
115 C<target>: the name of the package to load the script parameters (SPs) into
116  
117 C<sp>: the list of script parameters to load.
118  
119 C<updater>: a code ref to a function that will update the SPs based on the data
120 of a record. C<&updater> is called with a hashref of the SPs as the first
121 parameter, and key/value pairs of the record data as the remaining parameters.
122 C<&updater> should directly update the hashref, not replace it.
123  
124 =head1 LICENSE AND COPYRIGHT
125  
126 Copyright (c) 2018 Christopher White. All rights reserved.
127  
128 This program is free software; you can redistribute it and/or modify it
129 under the terms of the the Artistic License (2.0). Details are in the LICENSE
130 file accompanying this distribution.
131  
132 =cut
133  
134 # }}}2
135 # vi: set ts=4 sts=4 sw=4 et ai fo-=ro foldmethod=marker foldlevel=1: #
↑ Summary XML/Axk/Core.pm »