From: David Leadbeater Date: Mon, 4 Jul 2011 20:35:38 +0000 (+0100) Subject: Configurable prelude X-Git-Tag: v1.002000~4 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=73a98f1cea175c7451ab0809e9712c5fe903ca12;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 27cefdf..b177418 100644 --- a/Changes +++ b/Changes @@ -1,3 +1,5 @@ + - Make prelude configurable, so strictures can be optional + 1.1.0 2011-01-11 21: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