File Coverage

lib/Rex/Shared/Var.pm
Criterion Covered Total %
statement 73 73 100.0
branch 6 8 75.0
condition n/a
subroutine 15 15 100.0
pod 1 1 100.0
total 95 97 97.9


line stmt bran cond sub pod time code
1             #
2             # (c) Jan Gehring
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 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     64   596257 use v5.12.5;
  64         328  
49 64     63   377 use warnings;
  63         202  
  63         2761  
50              
51             our $VERSION = '1.14.2.2'; # TRIAL VERSION
52              
53 63     63   3472 use Symbol;
  63         6370  
  63         4781  
54              
55             require Exporter;
56 63     63   437 use base qw(Exporter);
  63         156  
  63         5975  
57 63     63   438 use vars qw(@EXPORT);
  63         167  
  63         20253  
58              
59             @EXPORT = qw(share);
60              
61             =head2 share
62              
63             Share the passed list of variables across Rex tasks. Should be used in a C 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             sub share {
81 82     82 1 2361 my @vars = @_;
82 82         340 my ( $package, $file, $line ) = caller;
83              
84 82         210 my ( $sigil, $symbol );
85 82         234 for my $var (@vars) {
86              
87 162 50       1171 if ( ( $sigil, $symbol ) = ( $var =~ /^([\$\@\%\*\&])(.+)/ ) ) {
88 162         789 my $ref_to_symbol = qualify_to_ref $symbol, $package;
89 162         4542 $symbol = "${package}::$symbol";
90              
91 162 100       736 if ( $sigil eq "\$" ) {
    100          
    50          
92 55     18   3166 eval "use Rex::Shared::Var::Scalar;";
  18     18   128  
  18     6   36  
  18     6   70  
  18     6   104  
  18     6   42  
  18     6   98  
  6         52  
  6         18  
  6         20  
  6         34  
  6         22  
  6         18  
  6         62  
  6         16  
  6         24  
  6         38  
  6         16  
  6         32  
  6         42  
  6         12  
  6         32  
93 55         639 tie ${ *{$ref_to_symbol} }, "Rex::Shared::Var::Scalar", $symbol;
  55         76  
  55         292  
94 55         175 *{$ref_to_symbol} = \${ *{$ref_to_symbol} };
  55         914549  
  55         73  
  55         121  
95             }
96             elsif ( $sigil eq "\@" ) {
97 63     63   4464 eval "use Rex::Shared::Var::Array;";
  63     32   240  
  63         884  
  64         3642  
  32         6291  
  32         204  
  32         518  
98 64         959 tie @{ *{$ref_to_symbol} }, "Rex::Shared::Var::Array", $symbol;
  64         146  
  64         518  
99 64         172 *{$ref_to_symbol} = \@{ *{$ref_to_symbol} };
  64         1971  
  64         149  
  64         200  
100             }
101             elsif ( $sigil eq "\%" ) {
102 43         2444 eval "use Rex::Shared::Var::Hash;";
103 43         502 tie %{ *{$ref_to_symbol} }, "Rex::Shared::Var::Hash", $symbol;
  43         64  
  43         259  
104 43         64 *{$ref_to_symbol} = \%{ *{$ref_to_symbol} };
  43         2288789  
  43         67  
  43         105  
105             }
106             }
107              
108             }
109             }
110              
111             1;