| 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 | 6 | 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 | 6 | ▶ | use uni::perl; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 52 | 6 | m{ | |||||||||||||||||||||||||||
| 53 | use strict; | ||||||||||||||||||||||||||||
| 54 | use warnings; | ||||||||||||||||||||||||||||
| 55 | }x; | ||||||||||||||||||||||||||||
| 56 | 6 | ▶ | use Devel::Declare; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 57 | 6 | ▶ | use Lexical::Alias (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 58 | 6 | ▶ | use Carp (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 6 | our $SUBNAME = 'eject'; | |||||||||||||||||||||||||||
| 61 | 6 | our @CARP_NOT = qw(Variable::Eject Devel::Declare); | |||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | BEGIN { | ||||||||||||||||||||||||||||
| 64 | 6 | ▶ | if (!defined &Variable::Eject::DEBUG) { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=3 SCAR=12.5
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | 6 | ▶ | if ($ENV{ PERL_VARIABLE_EJECT_DEBUG }) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | 0 | *DEBUG = sub () { 1 }; | |||||||||||||||||||||||||||
| 67 | } else { | ||||||||||||||||||||||||||||
| 68 | 6 | *DEBUG = sub () { 0 }; | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | sub import{ | ||||||||||||||||||||||||||||
| 74 | 6 | ▶ | my $class = shift; | ||||||||||||||||||||||||||
|
Subroutine
import: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 75 | 6 | my $caller = caller; | |||||||||||||||||||||||||||
| 76 | 6 | Devel::Declare->setup_for( | |||||||||||||||||||||||||||
| 77 | $caller, | ||||||||||||||||||||||||||||
| 78 | { $SUBNAME => { const => \&parse } } | ||||||||||||||||||||||||||||
| 79 | ); | ||||||||||||||||||||||||||||
| 80 | { | ||||||||||||||||||||||||||||
| 81 | 6 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 82 | 6 | ▶ | *{$caller.'::'.$SUBNAME } = sub (@) { Carp::carp( (0+@_)." <@_> this shouldn't be called - report your case to author\n" ) }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=0.4
| |||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | sub parse { | ||||||||||||||||||||||||||||
| 87 | 5 | ▶ | my $parser = Variable::Eject->new($_[1]); | ||||||||||||||||||||||||||
|
Subroutine
parse: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 88 | 5 | $parser->process(); | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | sub new { | ||||||||||||||||||||||||||||
| 92 | 5 | ▶ | my ($class, $offset) = @_; | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 93 | 5 | bless \$offset, $class; | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | sub whereami { | ||||||||||||||||||||||||||||
| 97 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
whereami: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 98 | 0 | my $line = Devel::Declare::get_linestr(); | |||||||||||||||||||||||||||
| 99 | 0 | warn "..>".substr($line,$$self); | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | sub process { | ||||||||||||||||||||||||||||
| 103 | 5 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
process: called CC=6 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 5 | $self->whereami if DEBUG; | |||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 5 | $$self += Devel::Declare::toke_skipspace($$self); | |||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 5 | ▶ | if (my $len = Devel::Declare::toke_scan_word($$self, 1)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | 2 | my $subname = substr(Devel::Declare::get_linestr(), $$self, $len); | |||||||||||||||||||||||||||
| 111 | 2 | warn "Skip subname $subname" if DEBUG; | |||||||||||||||||||||||||||
| 112 | 2 | ▶ | return if $subname ne $SUBNAME; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 5 | my $move = Devel::Declare::toke_move_past_token($$self); | |||||||||||||||||||||||||||
| 116 | 5 | warn "Move past token +$move" if DEBUG; | |||||||||||||||||||||||||||
| 117 | 5 | $$self += $move; | |||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 5 | $$self += Devel::Declare::toke_skipspace($$self); | |||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 5 | $self->whereami if DEBUG; | |||||||||||||||||||||||||||
| 122 | 5 | $self->skip_spaces(); | |||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 5 | my $args = $self->extract_args(); | |||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 5 | $args =~ s/(\r|\n)//go; | |||||||||||||||||||||||||||
| 127 | 5 | my @args = split /\s*(?:,|=>)\s*/, $args; | |||||||||||||||||||||||||||
| 128 | 5 | ▶ | @args > 1 or croak( 'Usage: '.$Variable::Eject::SUBNAME.'( $source_hash => $scalar, @array, %hash, ... )' ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 129 | 5 | my $from = shift @args; | |||||||||||||||||||||||||||
| 130 | 5 | warn "Have args $args: $from => [ @args ]" if DEBUG; | |||||||||||||||||||||||||||
| 131 | 5 | my $inj; | |||||||||||||||||||||||||||
| 132 | 5 | for (@args) { | |||||||||||||||||||||||||||
| 133 | 9 | s{(?:^\s+|\s+$)}{}sg; # ' $var ' => '$var' | |||||||||||||||||||||||||||
| 134 | 9 | my $type = substr($_,0,1,''); | |||||||||||||||||||||||||||
| 135 | 9 | s{(?:^\s+|\s+$)}{}sg; # ' { var } ' => '{ var }' | |||||||||||||||||||||||||||
| 136 | #s{^\s+}{}s; # ' { var } ' => '{ var }' | ||||||||||||||||||||||||||||
| 137 | 9 | s{^\s*\{?\s*|\s*\}?\s*$}{}sg; | |||||||||||||||||||||||||||
| 138 | 9 | warn "arg = <$type : $_>\n" if DEBUG; | |||||||||||||||||||||||||||
| 139 | #$_ = '{'.$_.'}' unless m/^\{.+\}$/; | ||||||||||||||||||||||||||||
| 140 | 9 | ▶ | if ($type eq '$') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 141 | 5 | $inj .= 'Lexical::Alias::alias( '.$from.'->{'.$_.'} => my $'.$_.' );'; | |||||||||||||||||||||||||||
| 142 | } else { | ||||||||||||||||||||||||||||
| 143 | 4 | $inj .= 'Lexical::Alias::alias( '.$type.'{'.$from.'->{'.$_.'}} => my '.$type.$_.' );'; | |||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | 5 | warn "$inj" if DEBUG; | |||||||||||||||||||||||||||
| 147 | 5 | $self->whereami if DEBUG; | |||||||||||||||||||||||||||
| 148 | 5 | $self->inject($inj); | |||||||||||||||||||||||||||
| 149 | 5 | return; | |||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | sub skip_spaces { | ||||||||||||||||||||||||||||
| 153 | 5 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
skip_spaces: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 154 | 5 | $$self += Devel::Declare::toke_skipspace($$self); | |||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | sub extract_args { | ||||||||||||||||||||||||||||
| 158 | 5 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
extract_args: called CC=4 SCAR=14.6
Pod
undocumented
| |||||||||||||||||||||||||||||
| 159 | 5 | warn "extract_args called at $$self\n" if DEBUG; | |||||||||||||||||||||||||||
| 160 | 5 | my $linestr = Devel::Declare::get_linestr(); | |||||||||||||||||||||||||||
| 161 | 5 | ▶ | if (substr($linestr, $$self, 1) eq '(') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 162 | 5 | my $length = Devel::Declare::toke_scan_str($$self); | |||||||||||||||||||||||||||
| 163 | 5 | my $proto = Devel::Declare::get_lex_stuff(); | |||||||||||||||||||||||||||
| 164 | 5 | Devel::Declare::clear_lex_stuff(); | |||||||||||||||||||||||||||
| 165 | 5 | $linestr = Devel::Declare::get_linestr(); | |||||||||||||||||||||||||||
| 166 | 5 | ▶ | if ( | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$length < 0 or $$self + $length > length $linestr
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
| |||||||||||||||||||||||||||||
| 167 | $length < 0 | ||||||||||||||||||||||||||||
| 168 | || | ||||||||||||||||||||||||||||
| 169 | $$self + $length > length($linestr) | ||||||||||||||||||||||||||||
| 170 | ){ | ||||||||||||||||||||||||||||
| 171 | 0 | Carp::croak("Unbalanced text supplied"); | |||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||
| 173 | 5 | warn "<<< '$linestr'\n" if DEBUG; | |||||||||||||||||||||||||||
| 174 | 5 | my $hide = '(42) if 0;'; | |||||||||||||||||||||||||||
| 175 | 5 | substr($linestr, $$self, $length) = $hide; | |||||||||||||||||||||||||||
| 176 | 5 | warn ">>> '$linestr'\n" if DEBUG; | |||||||||||||||||||||||||||
| 177 | 5 | $$self += length $hide; | |||||||||||||||||||||||||||
| 178 | 5 | Devel::Declare::set_linestr($linestr); | |||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | 5 | return $proto; | |||||||||||||||||||||||||||
| 181 | } else { | ||||||||||||||||||||||||||||
| 182 | 0 | Carp::croak "Can't use ".$SUBNAME.' without brackets. Use '.$SUBNAME.'(...)'; | |||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||
| 184 | 0 | return ''; | |||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | sub inject{ | ||||||||||||||||||||||||||||
| 188 | 5 | ▶ | my ($self, $inject) = @_; | ||||||||||||||||||||||||||
|
Subroutine
inject: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | 5 | warn "inject called at $$self for '$inject'\n" if DEBUG; | |||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | 5 | my $linestr = Devel::Declare::get_linestr(); | |||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | 5 | warn "<<< '$linestr'\n" if DEBUG; | |||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | 5 | ▶ | if ($$self > length($linestr)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 197 | 0 | croak("Parser tried to inject data outside program source, stopping"); | |||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||
| 199 | 5 | substr($linestr, $$self, 0) = $inject; | |||||||||||||||||||||||||||
| 200 | 5 | warn ">>> '$linestr'\n" if DEBUG; | |||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | 5 | Devel::Declare::set_linestr($linestr); | |||||||||||||||||||||||||||
| 203 | 5 | $$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 | 6 | 1; # End of Variable::Eject | |||||||||||||||||||||||||||