blib/lib/Variable/Eject.pm untested

filter: Statementstmt 0.0% 0 / 104 Branchbran 0.0% 0 / 44 Conditioncond 0.0% 0 / 5 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 176 CC 23 SCAR 41.7
CC
23
Cov
0%
process
156.0
extract_args
42.0
inject
30.0
CRAP 64.7
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 our $VERSION = '0.03';
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 use uni::perl;
52 m{
53 use strict;
54 use warnings;
55 }x;
56 use Devel::Declare;
57 use Lexical::Alias ();
58 use Carp ();
59  
60 our $SUBNAME = 'eject';
61 our @CARP_NOT = qw(Variable::Eject Devel::Declare);
62  
63 BEGIN {
64 if (!defined &Variable::Eject::DEBUG) {
65 if ($ENV{ PERL_VARIABLE_EJECT_DEBUG }) {
66 *DEBUG = sub () { 1 };
67 } else {
68 *DEBUG = sub () { 0 };
69 }
70 }
71 }
72  
73 sub import{
74 my $class = shift;
75 my $caller = caller;
76 Devel::Declare->setup_for(
77 $caller,
78 { $SUBNAME => { const => \&parse } }
79 );
80 {
81 no strict 'refs';
82 *{$caller.'::'.$SUBNAME } = sub (@) { Carp::carp( (0+@_)." <@_> this shouldn't be called - report your case to author\n" ) };
83 }
84 }
85  
86 sub parse {
87 my $parser = Variable::Eject->new($_[1]);
88 $parser->process();
89 }
90  
91 sub new {
92 my ($class, $offset) = @_;
93 bless \$offset, $class;
94 }
95  
96 sub whereami {
97 my $self = shift;
98 my $line = Devel::Declare::get_linestr();
99 warn "..>".substr($line,$$self);
100 }
101  
102 sub process {
103 my $self = shift;
104
105 $self->whereami if DEBUG;
106
107 $$self += Devel::Declare::toke_skipspace($$self);
108
109 if (my $len = Devel::Declare::toke_scan_word($$self, 1)) {
110 my $subname = substr(Devel::Declare::get_linestr(), $$self, $len);
111 warn "Skip subname $subname" if DEBUG;
112 return if $subname ne $SUBNAME;
113 }
114
115 my $move = Devel::Declare::toke_move_past_token($$self);
116 warn "Move past token +$move" if DEBUG;
117 $$self += $move;
118
119 $$self += Devel::Declare::toke_skipspace($$self);
120
121 $self->whereami if DEBUG;
122 $self->skip_spaces();
123
124 my $args = $self->extract_args();
125
126 $args =~ s/(\r|\n)//go;
127 my @args = split /\s*(?:,|=>)\s*/, $args;
128 @args > 1 or croak( 'Usage: '.$Variable::Eject::SUBNAME.'( $source_hash => $scalar, @array, %hash, ... )' );
129 my $from = shift @args;
130 warn "Have args $args: $from => [ @args ]" if DEBUG;
131 my $inj;
132 for (@args) {
133 s{(?:^\s+|\s+$)}{}sg; # ' $var ' => '$var'
134 my $type = substr($_,0,1,'');
135 s{(?:^\s+|\s+$)}{}sg; # ' { var } ' => '{ var }'
136 #s{^\s+}{}s; # ' { var } ' => '{ var }'
137 s{^\s*\{?\s*|\s*\}?\s*$}{}sg;
138 warn "arg = <$type : $_>\n" if DEBUG;
139 #$_ = '{'.$_.'}' unless m/^\{.+\}$/;
140 if ($type eq '$') {
141 $inj .= 'Lexical::Alias::alias( '.$from.'->{'.$_.'} => my $'.$_.' );';
142 } else {
143 $inj .= 'Lexical::Alias::alias( '.$type.'{'.$from.'->{'.$_.'}} => my '.$type.$_.' );';
144 }
145 }
146 warn "$inj" if DEBUG;
147 $self->whereami if DEBUG;
148 $self->inject($inj);
149 return;
150 }
151  
152 sub skip_spaces {
153 my $self = shift;
154 $$self += Devel::Declare::toke_skipspace($$self);
155 }
156  
157 sub extract_args {
158 my $self = shift;
159 warn "extract_args called at $$self\n" if DEBUG;
160 my $linestr = Devel::Declare::get_linestr();
161 if (substr($linestr, $$self, 1) eq '(') {
162 my $length = Devel::Declare::toke_scan_str($$self);
163 my $proto = Devel::Declare::get_lex_stuff();
164 Devel::Declare::clear_lex_stuff();
165 $linestr = Devel::Declare::get_linestr();
166 if (
167 $length < 0
168 ||
169 $$self + $length > length($linestr)
170 ){
171 Carp::croak("Unbalanced text supplied");
172 }
173 warn "<<< '$linestr'\n" if DEBUG;
174 my $hide = '(42) if 0;';
175 substr($linestr, $$self, $length) = $hide;
176 warn ">>> '$linestr'\n" if DEBUG;
177 $$self += length $hide;
178 Devel::Declare::set_linestr($linestr);
179
180 return $proto;
181 } else {
182 Carp::croak "Can't use ".$SUBNAME.' without brackets. Use '.$SUBNAME.'(...)';
183 }
184 return '';
185 }
186  
187 sub inject{
188 my ($self, $inject) = @_;
189
190 warn "inject called at $$self for '$inject'\n" if DEBUG;
191
192 my $linestr = Devel::Declare::get_linestr();
193
194 warn "<<< '$linestr'\n" if DEBUG;
195
196 if ($$self > length($linestr)) {
197 croak("Parser tried to inject data outside program source, stopping");
198 }
199 substr($linestr, $$self, 0) = $inject;
200 warn ">>> '$linestr'\n" if DEBUG;
201
202 Devel::Declare::set_linestr($linestr);
203 $$self += length($inject);
204 }
205  
206  
207 =head1 AUTHOR
208  
209 Mons Anderson, C<< <mons at cpan.org> >>
210  
211 =head1 BUGS
212  
213 Please report any bugs or feature requests to C<bug-variable-eject at rt.cpan.org>, or through
214 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Variable-Eject>. I will be notified, and then you'll
215 automatically be notified of progress on your bug as I make changes.
216  
217 =head1 COPYRIGHT & LICENSE
218  
219 Copyright 2009-2020 Mons Anderson
220  
221 This program is free software; you can redistribute it and/or modify it
222 under the same terms as Perl itself.
223  
224 =cut
225  
226 1; # End of Variable::Eject