README
1NAME
2 Eval::WithLexicals - pure perl eval with persistent lexical variables
3
4SYNOPSIS
5 # file: bin/tinyrepl
6
7 #!/usr/bin/env perl
8
9 use strictures 1;
10 use Eval::WithLexicals;
11 use Term::ReadLine;
12 use Data::Dumper;
13 use Getopt::Long;
14
15 GetOptions(
16 "plugin=s" => \my @plugins
17 );
18
19 $SIG{INT} = sub { warn "SIGINT\n" };
20
21 { package Data::Dumper; no strict 'vars';
22 $Terse = $Indent = $Useqq = $Deparse = $Sortkeys = 1;
23 $Quotekeys = 0;
24 }
25
26 my $eval = @plugins
27 ? Eval::WithLexicals->with_plugins(@plugins)->new
28 : Eval::WithLexicals->new;
29
30 my $read = Term::ReadLine->new('Perl REPL');
31 while (1) {
32 my $line = $read->readline('re.pl$ ');
33 exit unless defined $line;
34 my @ret; eval {
35 local $SIG{INT} = sub { die "Caught SIGINT" };
36 @ret = $eval->eval($line); 1;
37 } or @ret = ("Error!", $@);
38 print Dumper @ret;
39 }
40
41 # shell session:
42
43 $ perl -Ilib bin/tinyrepl
44 re.pl$ my $x = 0;
45 0
46 re.pl$ ++$x;
47 1
48 re.pl$ $x + 3;
49 4
50 re.pl$ ^D
51 $
52
53METHODS
54 new
55 my $eval = Eval::WithLexicals->new(
56 lexicals => { '$x' => \1 }, # default {}
57 in_package => 'PackageToEvalIn', # default Eval::WithLexicals::Scratchpad
58 context => 'scalar', # default 'list'
59 prelude => 'use warnings', # default 'use strictures 1'
60 );
61
62 eval
63 my @return_value = $eval->eval($code_to_eval);
64
65 lexicals
66 my $current_lexicals = $eval->lexicals;
67
68 $eval->lexicals(\%new_lexicals);
69
70 in_package
71 my $current_package = $eval->in_package;
72
73 $eval->in_package($new_package);
74
75 context
76 my $current_context = $eval->context;
77
78 $eval->context($new_context); # 'list', 'scalar' or 'void'
79
80 prelude
81 Code to run before evaling code. Loads strictures by default.
82
83 my $current_prelude = $eval->prelude;
84
85 $eval->prelude(q{use warnings}); # only warnings, not strict.
86
87 with_plugins
88 my $eval = Eval::WithLexicals->with_plugins("HintPersistence")->new;
89
90 Construct a class with the given plugins. Plugins are roles located
91 under a package name like "Eval::WithLexicals::With*".
92
93 Current plugins are:
94
95 * HintPersistence
96
97 When enabled this will persist pragams and other compile hints
98 between evals (for example the strict and warnings flags in effect).
99 See Eval::WithLexicals::WithHintPersistence for further details.
100
101AUTHOR
102 Matt S. Trout <mst@shadowcat.co.uk>
103
104CONTRIBUTORS
105 David Leadbeater <dgl@dgl.cx>
106
107 haarg - Graham Knop (cpan:HAARG) <haarg@cpan.org>
108
109COPYRIGHT
110 Copyright (c) 2010 the Eval::WithLexicals "AUTHOR" and "CONTRIBUTORS" as
111 listed above.
112
113LICENSE
114 This library is free software and may be distributed under the same
115 terms as perl itself.
116
117