From: David Leadbeater Date: Wed, 12 Jan 2011 20:28:31 +0000 (+0000) Subject: Document hints plugin X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=3fb66cc7c134ee8da56ac1378d87eb653c7b7619;p=p5sagit%2FEval-WithLexicals.git Document hints plugin Also add a quick --plugin option to tinyrepl for testing purposes. --- diff --git a/Changes b/Changes index eee8387..47bf2b4 100644 --- a/Changes +++ b/Changes @@ -1,4 +1,7 @@ - - Make prelude configurable, so strictures can be optional + - XXX: currently needs Moo from git (c69190f10) + - HintPersistence plugin to persist compile time hints (DGL) + - Support plugins (DGL) + - Make prelude configurable, so strictures can be optional (DGL) 1.1.0 2011-01-11 25:51:00 - Add a #line directive so it's clearer where errors occurred (DGL) diff --git a/bin/tinyrepl b/bin/tinyrepl index 236b081..4b78618 100755 --- a/bin/tinyrepl +++ b/bin/tinyrepl @@ -5,6 +5,12 @@ use Eval::WithLexicals; use Term::ReadLine; use Data::Dumper; +use Getopt::Long; + +GetOptions( + "plugin=s" => \my @plugins +); + $SIG{INT} = sub { warn "SIGINT\n" }; { package Data::Dumper; no strict 'vars'; @@ -12,7 +18,10 @@ $SIG{INT} = sub { warn "SIGINT\n" }; $Quotekeys = 0; } -my $eval = Eval::WithLexicals->new; +my $eval = @plugins + ? Eval::WithLexicals->with_plugins(@plugins)->new + : Eval::WithLexicals->new; + my $read = Term::ReadLine->new('Perl REPL'); while (1) { my $line = $read->readline('re.pl$ '); diff --git a/lib/Eval/WithLexicals.pm b/lib/Eval/WithLexicals.pm index e4f7587..e462063 100644 --- a/lib/Eval/WithLexicals.pm +++ b/lib/Eval/WithLexicals.pm @@ -224,6 +224,25 @@ Code to run before evaling code. Loads L by default. $eval->prelude(q{use warnings}); # only warnings, not strict. +=head2 with_plugins + + my $eval = Eval::WithLexicals->with_plugins("HintPersistence")->new; + +Construct a class with the given plugins. Plugins are roles located under +a package name like C. + +Current plugins are: + +=over 4 + +=item * HintPersistence + +When enabled this will persist pragams and other compile hints between evals +(for example the L and L flags in effect). See +L for further details. + +=back + =head1 AUTHOR Matt S. Trout diff --git a/lib/Eval/WithLexicals/WithHintPersistence.pm b/lib/Eval/WithLexicals/WithHintPersistence.pm index de3f76b..75e0433 100644 --- a/lib/Eval/WithLexicals/WithHintPersistence.pm +++ b/lib/Eval/WithLexicals/WithHintPersistence.pm @@ -1,16 +1,21 @@ package Eval::WithLexicals::WithHintPersistence; use Moo::Role; +use Sub::Quote; +our $VERSION = '1.001000'; # 1.1.0 +$VERSION = eval $VERSION; + +# Used localised our($hints, %hints); -has first_eval => ( +has hints => ( is => 'rw', - default => sub { 1 }, + default => quote_sub q{ {} }, ); -has hints => ( +has _first_eval => ( is => 'rw', - default => sub { {} }, + default => quote_sub q{ 1 }, ); around eval => sub { @@ -46,8 +51,8 @@ around setup_code => sub { my($self) = @_; # Only run the prelude on the first eval, hints will be set after # that. - if($self->first_eval) { - $self->first_eval(0); + if($self->_first_eval) { + $self->_first_eval(0); return $self->prelude; } else { # Seems we can't use the technique of passing via @_ for code in a BEGIN @@ -63,7 +68,7 @@ around capture_code => sub { my($self) = @_; ( q{ sub Eval::WithLexicals::Cage::capture_hints { - no warnings 'closure'; # XXX: can we limit the scope of this? + no warnings 'closure'; my($hints, %hints); BEGIN { $hints = $^H; %hints = %^H; } return q{$^H} => \$hints, q{%^H} => \%hints; @@ -71,4 +76,32 @@ around capture_code => sub { $orig->(@_) ) }; +=head1 NAME + +Eval::WithLexicals::WithHintPersistence - Persist compile hints between evals + +=head1 SYNOPSIS + + use Eval::WithLexicals; + + my $eval = Eval::WithLexicals->with_plugins("HintPersistence")->new; + +=head1 DESCRIPTION + +Persist pragams and other compile hints between evals (for example the +L and L flags in effect). + +Saves and restores the C<$^H> and C<%^H> variables. + +=head1 METHODS + +=head2 hints + + $eval->hints('$^H') + +Returns the internal hints hash, keys are C<$^H> and C<%^H> for the hint bits +and hint hash respectively. + +=cut + 1;