lib/Rex/Shared/Var.pm

filter: Statementstmt 100% 77 / 77 Branchbran 75.0% 6 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 15 / 15 Podpod 100% 1 / 1 totaltotal 98.0% 99 / 101 CC 6 SCAR 8.1
CC
6
Cov
98%
share
6.0
BEGIN
1.0
BEGIN
1.0
CRAP 2.3
« lib/Rex/Shared/Var/Common.pm ↑ Summary lib/Rex/Commands/PkgConf.pm »
1 #
2 # (c) Jan Gehring <jan.gehring@gmail.com>
3 #
4  
5 =head1 NAME
6  
7 Rex::Shared::Var - Share variables across Rex tasks
8  
9 =head1 DESCRIPTION
10  
11 Share variables across Rex tasks with the help of Storable, using a C<vars.db.$PID> file in the local directory, where C<$PID> is the PID of the parent process.
12  
13 =head1 SYNOPSIS
14  
15 BEGIN { # put share in a BEGIN block
16 use Rex::Shared::Var;
17 share qw($scalar @array %hash); # share the listed variables
18 }
19  
20 =head1 LIMITATIONS
21  
22 Currently nesting data structures works only if the assignment is made on the top level of the structure, or when the nested structures are also shared variables. For example:
23  
24 BEGIN {
25 use Rex::Shared::Var;
26 share qw(%hash %nested);
27 }
28  
29 # this doesn't work as expected
30 $hash{key} = { nested_key => 42 };
31 $hash{key}->{nested_key} = -1; # $hash{key}->{nested_key} still returns 42
32  
33 # workaround 1 - top level assignments
34 $hash{key} = { nested_key => 42 };
35 $hash{key} = { nested_key => -1 };
36  
37 # workaround 2 - nesting shared variables
38 $nested{nested_key} = 42;
39 $hash{key} = \%nested;
40 $hash{key}->{nested_key} = -1;
41  
42 =head1 METHODS
43  
44 =cut
45  
46 package Rex::Shared::Var;
47  
48 64 ▶use v5.14.4;
Subroutine BEGIN: called CC=1 SCAR=0.0
49 64 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
50  
51 63 our $VERSION = '1.16.1'; # VERSION
52  
53 63 ▶use Symbol;
Subroutine BEGIN: called CC=1 SCAR=0.0
54  
55 63 require Exporter;
56 63 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
57 63 ▶use vars qw(@EXPORT);
Subroutine BEGIN: called CC=1 SCAR=0.0
58  
59 63 @EXPORT = qw(share);
60  
61 =head2 share
62  
63 Share the passed list of variables across Rex tasks. Should be used in a C<BEGIN> block.
64  
65 BEGIN {
66 use Rex::Shared::Var;
67 share qw($error_count);
68 }
69  
70 task 'count', sub {
71 $error_count += run 'wc -l /var/log/syslog';
72 };
73  
74 after_task_finished 'count', sub {
75 say "Total number of errors: $error_count";
76 };
77  
78 =cut
79  
80 82 ▶sub share {
Subroutine share: called CC=6 SCAR=17.9
Pod documented
81 82 my @vars = @_;
82 82 my ( $package, $file, $line ) = caller;
83  
84 82 my ( $sigil, $symbol );
85 82 for my $var (@vars) {
86  
87 162 ▶ if ( ( $sigil, $symbol ) = ( $var =~ /^([\$\@\%\*\&])(.+)/ ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = $var =~ /^([\$\@\%\*\&])(.+)/u) 162 0 162
88 162 my $ref_to_symbol = qualify_to_ref $symbol, $package;
89 162 $symbol = "${package}::$symbol";
90  
91 162 ▶ if ( $sigil eq "\$" ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($sigil eq '$') { } 55 107 162
elsif ($sigil eq '@') { } 64 43 107
elsif ($sigil eq '%') { } 43 0 43
92 55 ▶ eval "use Rex::Shared::Var::Scalar;";
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
93 55 tie ${ *{$ref_to_symbol} }, "Rex::Shared::Var::Scalar", $symbol;
94 55 *{$ref_to_symbol} = \${ *{$ref_to_symbol} };
95 }
96 elsif ( $sigil eq "\@" ) {
97 63 ▶ eval "use Rex::Shared::Var::Array;";
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
98 64 tie @{ *{$ref_to_symbol} }, "Rex::Shared::Var::Array", $symbol;
99 64 *{$ref_to_symbol} = \@{ *{$ref_to_symbol} };
100 }
101 elsif ( $sigil eq "\%" ) {
102 43 eval "use Rex::Shared::Var::Hash;";
103 43 tie %{ *{$ref_to_symbol} }, "Rex::Shared::Var::Hash", $symbol;
104 43 *{$ref_to_symbol} = \%{ *{$ref_to_symbol} };
105 }
106 }
107  
108 }
109 }
110  
111 63 1;
« lib/Rex/Shared/Var/Common.pm ↑ Summary lib/Rex/Commands/PkgConf.pm »