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