lib/Variable/Eject.pm

filter: Statementstmt 88.8% 80 / 90 Branchbran 64.2% 9 / 14 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 86.6% 13 / 15 Podpod 0.0% 0 / 8 totaltotal 78.0% 103 / 132 SCAR 23.4
CC
10
Cov
84%
extract_args
4.7
process
4.0
inject
2.0
CRAP 10.4
« blib/lib/Variable/Eject.pm ↑ Summary
1 package Variable::Eject;
2  
3 =head1 NAME
4  
5 Variable::Eject - Eject variables from hash to current namespace
6  
7 =head1 VERSION
8  
9 Version 0.02
10  
11 =cut
12  
13 5 our $VERSION = '0.02';
14  
15 =head1 SYNOPSIS
16  
17 use Variable::Eject;
18  
19 my $hash = {
20 scalar => 'scalar value',
21 array => [1..3],
22 hash => { my => 'value' },
23 };
24  
25 # Now, eject vars from hash
26 eject(
27 $hash => $scalar, @array, %hash,
28 );
29  
30 # Let's look
31 say $scalar;
32 say @array;
33 say keys %hash;
34  
35 # Let's modify (source will be modified)
36 $scalar .= ' modified';
37 shift @array;
38 $hash{another} = 1;
39  
40 =head1 EXPORT
41  
42 A list of functions that can be exported. You can delete this section
43 if you don't export anything, such as for a purely object-oriented module.
44  
45 =head1 FUNCTIONS
46  
47 =head2 eject ( $source_hash => $scalar, @array, %hash ... );
48  
49 =cut
50  
51 5 ▶use uni::perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
52 5 m{
53 use strict;
54 use warnings;
55 }x;
56 5 ▶use Devel::Declare ();
Subroutine BEGIN: called CC=1 SCAR=0.0
57 5 ▶use Lexical::Alias ();
Subroutine BEGIN: called CC=1 SCAR=0.0
58  
59 5 our @CARP_NOT = qw(Devel::Declare);
60 5 our $SUBNAME = 'eject';
61  
62 sub import{
63 5 ▶ my $class = shift;
Subroutine import: called CC=1 SCAR=0.0
64 5 my $caller = caller;
65 5 Devel::Declare->setup_for(
66 $caller,
67 { $SUBNAME => { const => \&parse } }
68 );
69 {
70 5 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
71 5 ▶ *{$caller.'::'.$SUBNAME } = sub (@_) { warn "this shouldn't be called - report your case to author\n" };
Subroutine __ANON__: not called CC=1 SCAR=0.4
72 }
73 }
74  
75  
76 sub parse {
77 4 ▶ my $parser = Variable::Eject->new($_[1]);
Subroutine parse: called CC=2 SCAR=6.9
Pod undocumented
78 4 ▶ return if $parser->get_word() ne $SUBNAME;
Branch: 1 branch
BranchTrueFalseTotal
if $parser->get_word ne $Variable::Eject::SUBNAME 3 1 4
79 1 $parser->process();
80 }
81  
82 package # hide
83 Variable::Eject;
84  
85 5 ▶use uni::perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
86 5 our @CARP_NOT = qw(Variable::Eject Devel::Declare);
87  
88 sub DEBUG () { 0 }
89  
90 sub new {
91 4 ▶ my ($class, $offset) = @_;
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
92 #print STDERR "new called at $offset\n" if DEBUG;
93 4 bless \$offset, $class;
94 }
95 sub whereami {
96 0 ▶ my $self = shift;
Subroutine whereami: not called CC=1 SCAR=6.9
Pod undocumented
97 0 my $line = Devel::Declare::get_linestr;
98 0 warn "..>".substr($line,$$self);
99 }
100 sub process {
101 1 ▶ my $self = shift;
Subroutine process: called CC=4 SCAR=13.9
Pod undocumented
102 1 $self->whereami if DEBUG;
103 1 $$self+=Devel::Declare::toke_move_past_token($$self);
104 1 $self->whereami if DEBUG;
105 1 $self->skip_spaces();
106 1 my $args = $self->extract_args();
107 1 $args =~ s/(\r|\n)//go;
108 1 my @args = split /\s*(?:,|=>)\s*/, $args;
109 1 ▶ @args > 1 or croak( 'Usage: '.$Variable::Eject::SUBNAME.'( $source_hash => $scalar, @array, %hash, ... )' );
Branch: 1 branch
BranchTrueFalseTotal
@args > 1 or croak('Usage: ' . $Variable::Eject::SUBNAME . '( $source_hash => $scalar, @array, %hash, ... )') 0 1 1
110 1 my $from = shift @args;
111 #warn "Have args $args: $from => [ @args ]";
112 1 my $inj;
113 1 for (@args) {
114 #warn "arg = >$_<\n";
115 1 s{(?:^\s+|\s+$)}{}sg; # ' $var ' => '$var'
116 1 my $type = substr($_,0,1,'');
117 1 s{^\s+}{}s; # ' { var } ' => '{ var }'
118 1 s{^\s*\{?\s*|\s*\}?\s*$}{}sg;
119 #$_ = '{'.$_.'}' unless m/^\{.+\}$/;
120 1 ▶ if ($type eq '$') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq '$') { } 1 0 1
121 1 $inj .= 'Lexical::Alias::alias( '.$from.'->{'.$_.'} => my $'.$_.' );';
122 #$inj .= 'Lexical::Alias::alias( '.$from.'->'.$_.' => my $'.$_.' );';
123 } else {
124 0 $inj .= 'Lexical::Alias::alias( '.$type.'{'.$from.'->{'.$_.'}} => my '.$type.$_.' );';
125 #$inj .= 'Lexical::Alias::alias( '.$type.'{'.$from.'->'.$_.'} => my '.$type.$_.' );';
126 }
127 #warn "$inj";
128 }
129 1 $self->whereami if DEBUG;
130 1 $self->inject("() if 0; $inj");
131 1 return;
132 }
133  
134 sub get_word {
135 4 ▶ my $self = shift;
Subroutine get_word: called CC=2 SCAR=6.9
Pod undocumented
136  
137 4 print STDERR "get_word called at $$self\n" if DEBUG;
138  
139 4 ▶ if (my $len = Devel::Declare::toke_scan_word($$self, 1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $len = Devel::Declare::toke_scan_word($$self, 1)) 1 3 4
140 1 return substr(Devel::Declare::get_linestr(), $$self, $len);
141 }
142 3 return '';
143 }
144  
145 sub skip_spaces {
146 1 ▶ my $self = shift;
Subroutine skip_spaces: called CC=1 SCAR=0.0
Pod undocumented
147  
148 1 print STDERR "skip_spaces called at $$self\n" if DEBUG;
149  
150 1 $$self += Devel::Declare::toke_skipspace($$self);
151 }
152  
153 sub extract_args {
154 1 ▶ my $self = shift;
Subroutine extract_args: called CC=4 SCAR=15.4
Pod undocumented
155  
156 1 print STDERR "extract_args called at $$self\n" if DEBUG;
157  
158 1 my $linestr = Devel::Declare::get_linestr();
159 1 ▶ if (substr($linestr, $$self, 1) eq '(') {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($linestr, $$self, 1) eq '(') { } 1 0 1
160 1 my $length = Devel::Declare::toke_scan_str($$self);
161 1 my $proto = Devel::Declare::get_lex_stuff();
162 1 Devel::Declare::clear_lex_stuff();
163  
164 1 $linestr = Devel::Declare::get_linestr();
165 1 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($length < 0 or $$self + $length > length $linestr) 0 1 1
Condition33%1/3
$length < 0 or $$self + $length > length $linestr
l!l&&r!l&&!r
001
MC/DC0%0/2
$length < 0 or $$self + $length > length $linestr
$length < 0 $$self + $length > length $linestr
Truth table33%1/3
A or B
A= $length < 0B= $$self + $length > length $linestr
ABresult
1X1
011
000
166 $length < 0
167 ||
168 $$self + $length > length($linestr)
169 ){
170 0 require Carp;
171 0 Carp::croak("Unbalanced text supplied for assert");
172 }
173 1 substr($linestr, $$self, $length) = '';
174 1 Devel::Declare::set_linestr($linestr);
175  
176 1 return $proto;
177 } else {
178 0 croak "Can't use '.$Variable::Eject::SUBNAME.' without brackets. Use '.$Variable::Eject::SUBNAME.'(...)";
179 }
180 0 return '';
181 }
182  
183 sub inject{
184 1 ▶ my ($self, $inject) = @_;
Subroutine inject: called CC=2 SCAR=7.1
Pod undocumented
185  
186 1 print STDERR "inject called at $$self for '$inject'\n" if DEBUG;
187  
188 1 my $linestr = Devel::Declare::get_linestr;
189 1 ▶ if ($$self > length($linestr)){
Branch: 1 branch
BranchTrueFalseTotal
if ($$self > length $linestr) 0 1 1
190 0 croak("Parser tried to inject data outside program source, stopping");
191 }
192 1 substr($linestr, $$self, 0) = $inject;
193 1 Devel::Declare::set_linestr($linestr);
194 1 $$self += length($inject);
195 }
196  
197  
198 =head1 AUTHOR
199  
200 Mons Anderson, C<< <mons at cpan.org> >>
201  
202 =head1 BUGS
203  
204 Please report any bugs or feature requests to C<bug-variable-eject at rt.cpan.org>, or through
205 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Variable-Eject>. I will be notified, and then you'll
206 automatically be notified of progress on your bug as I make changes.
207  
208 =head1 COPYRIGHT & LICENSE
209  
210 Copyright 2009 Mons Anderson, all rights reserved.
211  
212 This program is free software; you can redistribute it and/or modify it
213 under the same terms as Perl itself.
214  
215 =cut
216  
217 5 1; # End of Variable::Eject
« blib/lib/Variable/Eject.pm ↑ Summary