1 package Eval::WithLexicals;
7 our $VERSION = '1.003001'; # 1.3.1
8 $VERSION = eval $VERSION;
10 has lexicals => (is => 'rw', default => quote_sub q{ {} });
13 my %valid_contexts = map +($_ => 1), qw(list scalar void);
16 is => 'rw', default => quote_sub(q{ 'list' }),
19 die "Invalid context type $val - should be list, scalar or void"
20 unless $valid_contexts{$val};
26 is => 'rw', default => quote_sub q{ 'Eval::WithLexicals::Scratchpad' }
30 is => 'rw', default => quote_sub q{ 'use strictures 1;' }
34 my($class, @names) = @_;
36 Moo::Role->create_class_with_roles($class,
37 map "Eval::WithLexicals::With$_", @names);
46 ( qq{ BEGIN { Eval::WithLexicals::Util::capture_list() } } )
50 my ($self, $to_eval) = @_;
51 local *Eval::WithLexicals::Cage::current_line;
52 local *Eval::WithLexicals::Cage::pad_capture;
53 local *Eval::WithLexicals::Cage::grab_captures;
55 my $package = $self->in_package;
56 my $setup_code = join '', $self->setup_code,
57 # $_[2] being what is passed to _eval_do below
58 Sub::Quote::capture_unroll('$_[2]', $self->lexicals, 2);
60 my $capture_code = join '', $self->capture_code;
62 local our $current_code = qq!
64 sub Eval::WithLexicals::Cage::current_line {
68 ;sub Eval::WithLexicals::Cage::pad_capture { }
70 sub Eval::WithLexicals::Cage::grab_captures {
71 no warnings 'closure'; no strict 'vars';
72 package! # hide from PAUSE
73 .q! Eval::WithLexicals::VarScope;!;
74 # rest is appended by Eval::WithLexicals::Util::capture_list, called
75 # during parsing by the BEGIN block from capture_code.
77 $self->_eval_do(\$current_code, $self->lexicals, $to_eval);
78 $self->_run(\&Eval::WithLexicals::Cage::current_line);
82 my($self, $code) = @_;
85 my $ctx = $self->context;
88 } elsif ($ctx eq 'scalar') {
95 %{$self->_grab_captures},
102 my $cap = Eval::WithLexicals::Cage::grab_captures();
103 foreach my $key (keys %$cap) {
104 my ($sigil, $name) = $key =~ /^(.)(.+)$/;
105 my $var_scope_name = $sigil.'Eval::WithLexicals::VarScope::'.$name;
106 if ($cap->{$key} eq eval "\\${var_scope_name}") {
114 my ($self, $text_ref, $lexical, $original) = @_;
116 if ($_[1] eq '/eval_do') {
117 open my $fh, '<', $text_ref;
123 do '/eval_do' or die $@;
127 package # hide from PAUSE
128 Eval::WithLexicals::Util;
130 use B qw(svref_2object);
133 my $pad_capture = \&Eval::WithLexicals::Cage::pad_capture;
134 my @names = grep $_ ne '&', map $_->PV, grep $_->isa('B::PV'),
135 svref_2object($pad_capture)->OUTSIDE->PADLIST->ARRAYelt(0)->ARRAY;
136 $Eval::WithLexicals::current_code .=
137 '+{ '.join(', ', map "'$_' => \\$_", @names).' };'
147 Eval::WithLexicals - pure perl eval with persistent lexical variables
156 use Eval::WithLexicals;
162 "plugin=s" => \my @plugins
165 $SIG{INT} = sub { warn "SIGINT\n" };
167 { package Data::Dumper; no strict 'vars';
168 $Terse = $Indent = $Useqq = $Deparse = $Sortkeys = 1;
173 ? Eval::WithLexicals->with_plugins(@plugins)->new
174 : Eval::WithLexicals->new;
176 my $read = Term::ReadLine->new('Perl REPL');
178 my $line = $read->readline('re.pl$ ');
179 exit unless defined $line;
181 local $SIG{INT} = sub { die "Caught SIGINT" };
182 @ret = $eval->eval($line); 1;
183 } or @ret = ("Error!", $@);
189 $ perl -Ilib bin/tinyrepl
203 my $eval = Eval::WithLexicals->new(
204 lexicals => { '$x' => \1 }, # default {}
205 in_package => 'PackageToEvalIn', # default Eval::WithLexicals::Scratchpad
206 context => 'scalar', # default 'list'
207 prelude => 'use warnings', # default 'use strictures 1'
212 my @return_value = $eval->eval($code_to_eval);
216 my $current_lexicals = $eval->lexicals;
218 $eval->lexicals(\%new_lexicals);
222 my $current_package = $eval->in_package;
224 $eval->in_package($new_package);
228 my $current_context = $eval->context;
230 $eval->context($new_context); # 'list', 'scalar' or 'void'
234 Code to run before evaling code. Loads L<strictures> by default.
236 my $current_prelude = $eval->prelude;
238 $eval->prelude(q{use warnings}); # only warnings, not strict.
242 my $eval = Eval::WithLexicals->with_plugins("HintPersistence")->new;
244 Construct a class with the given plugins. Plugins are roles located under
245 a package name like C<Eval::WithLexicals::With*>.
251 =item * HintPersistence
253 When enabled this will persist pragams and other compile hints between evals
254 (for example the L<strict> and L<warnings> flags in effect). See
255 L<Eval::WithLexicals::WithHintPersistence> for further details.
261 Matt S. Trout <mst@shadowcat.co.uk>
265 David Leadbeater <dgl@dgl.cx>
267 haarg - Graham Knop (cpan:HAARG) <haarg@cpan.org>
271 Copyright (c) 2010 the Eval::WithLexicals L</AUTHOR> and L</CONTRIBUTORS>
276 This library is free software and may be distributed under the same terms