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   566969 use v5.12.5;
  64         342  
49 64     63   330 use warnings;
  63         164  
  63         2982  
50              
51             our $VERSION = '1.14.3'; # VERSION
52              
53 63     63   3656 use Symbol;
  63         5897  
  63         4221  
54              
55             require Exporter;
56 63     63   479 use base qw(Exporter);
  63         130  
  63         5515  
57 63     63   399 use vars qw(@EXPORT);
  63         237  
  63         19461  
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 2433 my @vars = @_;
82 82         325 my ( $package, $file, $line ) = caller;
83              
84 82         243 my ( $sigil, $symbol );
85 82         268 for my $var (@vars) {
86              
87 162 50       1141 if ( ( $sigil, $symbol ) = ( $var =~ /^([\$\@\%\*\&])(.+)/ ) ) {
88 162         614 my $ref_to_symbol = qualify_to_ref $symbol, $package;
89 162         4234 $symbol = "${package}::$symbol";
90              
91 162 100       668 if ( $sigil eq "\$" ) {
    100          
    50          
92 55     18   3211 eval "use Rex::Shared::Var::Scalar;";
  18     18   130  
  18     6   78  
  18     6   104  
  18     6   146  
  18     6   34  
  18     6   72  
  6         38  
  6         12  
  6         24  
  6         36  
  6         12  
  6         22  
  6         60  
  6         12  
  6         24  
  6         40  
  6         14  
  6         24  
  6         38  
  6         14  
  6         22  
93 55         594 tie ${ *{$ref_to_symbol} }, "Rex::Shared::Var::Scalar", $symbol;
  55         67  
  55         290  
94 55         109 *{$ref_to_symbol} = \${ *{$ref_to_symbol} };
  55         879193  
  55         69  
  55         110  
95             }
96             elsif ( $sigil eq "\@" ) {
97 63     63   3824 eval "use Rex::Shared::Var::Array;";
  63     32   249  
  63         840  
  64         3478  
  32         6540  
  32         177  
  32         643  
98 64         1013 tie @{ *{$ref_to_symbol} }, "Rex::Shared::Var::Array", $symbol;
  64         159  
  64         522  
99 64         190 *{$ref_to_symbol} = \@{ *{$ref_to_symbol} };
  64         1959  
  64         104  
  64         205  
100             }
101             elsif ( $sigil eq "\%" ) {
102 43         2739 eval "use Rex::Shared::Var::Hash;";
103 43         517 tie %{ *{$ref_to_symbol} }, "Rex::Shared::Var::Hash", $symbol;
  43         84  
  43         276  
104 43         74 *{$ref_to_symbol} = \%{ *{$ref_to_symbol} };
  43         2324658  
  43         64  
  43         98  
105             }
106             }
107              
108             }
109             }
110              
111             1;