blib/lib/Variable/Expand/AnyLevel.pm

filter: Statementstmt 45.4% 15 / 33 Branchbran 0.0% 0 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 80.0% 4 / 5 Podpod 100% 1 / 1 totaltotal 46.5% 20 / 43 SCAR 20.0
CC
4
Cov
41%
expand_variable
20.0
BEGIN
1.0
BEGIN
1.0
CRAP 7.4
↑ Summary Build »
1 package Variable::Expand::AnyLevel;
2 2 ▶use parent qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 our $VERSION = '0.03';
6 2 our @EXPORT_OK = qw(expand_variable);
7 2 ▶use PadWalker qw(peek_my);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 =head1 NAME
10  
11 Variable::Expand::AnyLevel - expand variables exist at any level.
12  
13 =head1 SYNOPSIS
14  
15 use Variable::Expand::AnyLevel qw(expand_variable);
16 my $value1 = 'aaa';
17 my $value2 = expand_variable('$value1', 0);
18 # $value2 is 'aaa';
19  
20 =head1 DESCRIPTION
21  
22 Variable::Expand::AnyLevel enables to expand variables which exist at any level. (level means same as Carp or PadWalker)
23  
24 =cut
25  
26 =head1 FUNCTIONS
27  
28 =cut
29  
30 =head2 expand_variable($string, $peek_level, $options_href)
31  
32 Expand variable in $string which exists in $peek_level. $peek_level is same as caller().
33  
34 If stringify option specified(it is default) $string is correctly expanded. For example,
35  
36 my $aa = 'aa';
37 my $result = $expand_variable('$aa 123', 0);
38  
39 $result is expanded 'aa 123'
40  
41 If stringify option is set to '0', $string is not expanded.
42  
43 my $aa = 'aa';
44 my $result = $expand_variable('$aa 123', 0, { stringify => '0' });
45  
46 $result is undef.
47  
48  
49 available options are as follows
50  
51 stringify: stringify variable(1) or not(0). default value is 1
52  
53 =cut
54  
55 sub expand_variable {
56 0 ▶ my ($string, $peek_level, $options_href) = @_;
Subroutine expand_variable: not called CC=4 SCAR=30.0
Pod documented
57  
58 0 my $walker = peek_my($peek_level + 1);
59 0 my $value = undef;
60 0 my $variable_gen_code = "sub {\n";
61 0 $variable_gen_code .= " no warnings 'all';\n";
62  
63 0 my %values = ();
64 0 for my $variable_name ( keys %{ $walker } ) {
65 0 my $sigil = substr $variable_name, 0, 1;
66 0 $values{$variable_name} = $walker->{$variable_name};
67 0 $variable_gen_code .= " my $variable_name = ${sigil}{ \$values{ '$variable_name' } };\n";
68 }
69 0 ▶ my $stringify = defined $options_href->{stringify} ? $options_href->{stringify} : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $options_href->{'stringify'} ? : 0 0 0
70 0 ▶ if ( !$stringify ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $stringify) { } 0 0 0
71 0 $variable_gen_code .= " return $string;\n";
72 }
73 else {
74 0 $variable_gen_code .= " return \"$string\";\n";
75 }
76 0 $variable_gen_code .= "}->()\n";
77 #warn $variable_gen_code; use Data::Dumper; warn Dumper(\%values);
78 ## no critic
79 0 eval "\$value = $variable_gen_code";
80 ## use critic
81 0 return $value;
82 }
83  
84  
85  
86  
87 2 1;
88 __END__
↑ Summary Build »