1Eval::WithLexicals(3) User Contributed Perl DocumentationEval::WithLexicals(3)
2
3
4

NAME

6       Eval::WithLexicals - pure perl eval with persistent lexical variables
7

SYNOPSIS

9         # file: bin/tinyrepl
10
11         #!/usr/bin/env perl
12
13         use strictures 1;
14         use Eval::WithLexicals;
15         use Term::ReadLine;
16         use Data::Dumper;
17         use Getopt::Long;
18
19         GetOptions(
20           "plugin=s" => \my @plugins
21         );
22
23         $SIG{INT} = sub { warn "SIGINT\n" };
24
25         { package Data::Dumper; no strict 'vars';
26           $Terse = $Indent = $Useqq = $Deparse = $Sortkeys = 1;
27           $Quotekeys = 0;
28         }
29
30         my $eval = @plugins
31          ? Eval::WithLexicals->with_plugins(@plugins)->new
32          : Eval::WithLexicals->new;
33
34         my $read = Term::ReadLine->new('Perl REPL');
35         while (1) {
36           my $line = $read->readline('re.pl$ ');
37           exit unless defined $line;
38           my @ret; eval {
39             local $SIG{INT} = sub { die "Caught SIGINT" };
40             @ret = $eval->eval($line); 1;
41           } or @ret = ("Error!", $@);
42           print Dumper @ret;
43         }
44
45         # shell session:
46
47         $ perl -Ilib bin/tinyrepl
48         re.pl$ my $x = 0;
49         0
50         re.pl$ ++$x;
51         1
52         re.pl$ $x + 3;
53         4
54         re.pl$ ^D
55         $
56

METHODS

58   new
59         my $eval = Eval::WithLexicals->new(
60           lexicals => { '$x' => \1 },      # default {}
61           in_package => 'PackageToEvalIn', # default Eval::WithLexicals::Scratchpad
62           context => 'scalar',             # default 'list'
63           prelude => 'use warnings',       # default 'use strictures 1'
64         );
65
66   eval
67         my @return_value = $eval->eval($code_to_eval);
68
69   lexicals
70         my $current_lexicals = $eval->lexicals;
71
72         $eval->lexicals(\%new_lexicals);
73
74   in_package
75         my $current_package = $eval->in_package;
76
77         $eval->in_package($new_package);
78
79   context
80         my $current_context = $eval->context;
81
82         $eval->context($new_context); # 'list', 'scalar' or 'void'
83
84   prelude
85       Code to run before evaling code. Loads strictures by default.
86
87         my $current_prelude = $eval->prelude;
88
89         $eval->prelude(q{use warnings}); # only warnings, not strict.
90
91   with_plugins
92         my $eval = Eval::WithLexicals->with_plugins("HintPersistence")->new;
93
94       Construct a class with the given plugins. Plugins are roles located
95       under a package name like "Eval::WithLexicals::With*".
96
97       Current plugins are:
98
99       •   HintPersistence
100
101           When enabled this will persist pragams and other compile hints
102           between evals (for example the strict and warnings flags in
103           effect). See Eval::WithLexicals::WithHintPersistence for further
104           details.
105

AUTHOR

107       Matt S. Trout <mst@shadowcat.co.uk>
108

CONTRIBUTORS

110       David Leadbeater <dgl@dgl.cx>
111
112       haarg - Graham Knop (cpan:HAARG) <haarg@cpan.org>
113
115       Copyright (c) 2010 the Eval::WithLexicals "AUTHOR" and "CONTRIBUTORS"
116       as listed above.
117

LICENSE

119       This library is free software and may be distributed under the same
120       terms as perl itself.
121
122
123
124perl v5.36.0                      2023-01-20             Eval::WithLexicals(3)
Impressum