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   559155 use v5.12.5;
  64         307  
49 64     63   356 use warnings;
  63         160  
  63         2720  
50              
51             our $VERSION = '1.14.2.3'; # TRIAL VERSION
52              
53 63     63   3544 use Symbol;
  63         5817  
  63         4632  
54              
55             require Exporter;
56 63     63   425 use base qw(Exporter);
  63         137  
  63         5452  
57 63     63   442 use vars qw(@EXPORT);
  63         152  
  63         21320  
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 2447 my @vars = @_;
82 82         331 my ( $package, $file, $line ) = caller;
83              
84 82         242 my ( $sigil, $symbol );
85 82         236 for my $var (@vars) {
86              
87 162 50       1193 if ( ( $sigil, $symbol ) = ( $var =~ /^([\$\@\%\*\&])(.+)/ ) ) {
88 162         677 my $ref_to_symbol = qualify_to_ref $symbol, $package;
89 162         4417 $symbol = "${package}::$symbol";
90              
91 162 100       1274 if ( $sigil eq "\$" ) {
    100          
    50          
92 55     18   3304 eval "use Rex::Shared::Var::Scalar;";
  18     18   158  
  18     6   44  
  18     6   82  
  18     6   120  
  18     6   48  
  18     6   152  
  6         38  
  6         18  
  6         20  
  6         38  
  6         12  
  6         36  
  6         70  
  6         14  
  6         28  
  6         38  
  6         16  
  6         24  
  6         42  
  6         16  
  6         24  
93 55         693 tie ${ *{$ref_to_symbol} }, "Rex::Shared::Var::Scalar", $symbol;
  55         73  
  55         306  
94 55         102 *{$ref_to_symbol} = \${ *{$ref_to_symbol} };
  55         902067  
  55         65  
  55         132  
95             }
96             elsif ( $sigil eq "\@" ) {
97 63     63   3955 eval "use Rex::Shared::Var::Array;";
  63     32   239  
  63         936  
  64         3736  
  32         6092  
  32         187  
  32         520  
98 64         977 tie @{ *{$ref_to_symbol} }, "Rex::Shared::Var::Array", $symbol;
  64         162  
  64         488  
99 64         189 *{$ref_to_symbol} = \@{ *{$ref_to_symbol} };
  64         1885  
  64         124  
  64         214  
100             }
101             elsif ( $sigil eq "\%" ) {
102 43         2976 eval "use Rex::Shared::Var::Hash;";
103 43         521 tie %{ *{$ref_to_symbol} }, "Rex::Shared::Var::Hash", $symbol;
  43         62  
  43         293  
104 43         103 *{$ref_to_symbol} = \%{ *{$ref_to_symbol} };
  43         2383457  
  43         65  
  43         103  
105             }
106             }
107              
108             }
109             }
110              
111             1;