From: David Leadbeater Date: Tue, 11 Jan 2011 00:16:36 +0000 (+0000) Subject: Configurable prelude X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=3044b1dac308a6691f4f50d2e5732e2eacbf9527;p=p5sagit%2FEval-WithLexicals.git Configurable prelude Add an attribute 'prelude' which defaults to 'use strictures 1;' so use of strictures is configurable. --- diff --git a/Changes b/Changes index b2e997b..eee8387 100644 --- a/Changes +++ b/Changes @@ -1,3 +1,5 @@ + - Make prelude configurable, so strictures can be optional + 1.1.0 2011-01-11 25:51:00 - Add a #line directive so it's clearer where errors occurred (DGL) - Filter out lexicals called '&' since these are anon subs (MSTROUT) diff --git a/lib/Eval/WithLexicals.pm b/lib/Eval/WithLexicals.pm index 31703e6..fab265c 100644 --- a/lib/Eval/WithLexicals.pm +++ b/lib/Eval/WithLexicals.pm @@ -25,6 +25,10 @@ has in_package => ( is => 'rw', default => quote_sub q{ 'Eval::WithLexicals::Scratchpad' } ); +has prelude => ( + is => 'rw', default => quote_sub q{ 'use strictures 1;' } +); + sub eval { my ($self, $to_eval) = @_; local *Eval::WithLexicals::Cage::current_line; @@ -32,7 +36,8 @@ sub eval { local *Eval::WithLexicals::Cage::grab_captures; my $setup = Sub::Quote::capture_unroll('$_[2]', $self->lexicals, 2); my $package = $self->in_package; - local our $current_code = qq!use strictures 1; + my $prelude = $self->prelude; + local our $current_code = qq!${prelude} ${setup} sub Eval::WithLexicals::Cage::current_line { package ${package}; @@ -155,6 +160,7 @@ Eval::WithLexicals - pure perl eval with persistent lexical variables lexicals => { '$x' => \1 }, # default {} in_package => 'PackageToEvalIn', # default Eval::WithLexicals::Scratchpad context => 'scalar', # default 'list' + prelude => 'use warnings', # default 'use strictures 1' ); =head2 eval @@ -179,6 +185,14 @@ Eval::WithLexicals - pure perl eval with persistent lexical variables $eval->context($new_context); # 'list', 'scalar' or 'void' +=head2 prelude + +Code to run before evaling code. Loads L by default. + + my $current_prelude = $eval->prelude; + + $eval->prelude(q{use warnings}); # only warnings, not strict. + =head1 AUTHOR Matt S. Trout