Eval::WithLexicals(3)
π NAME π
Okay so bestie, this is the Eval::WithLexicals(3) page from the User Contributed Perl Documentation, and it's literally about Eval::WithLexicals β pure perl eval with persistent lexical variables. No cap. Periodt. π¦
πΈ SYNOPSIS πΈ
Here's the whole vibe in code, exactly as the page shows it:
# file: bin/tinyrepl
#!/usr/bin/env perl
use strictures 1;
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';
$Terse = $Indent = $Useqq = $Deparse = $Sortkeys = 1;
$Quotekeys = 0;
}
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$ ');
exit unless defined $line;
my @ret; eval {
local $SIG{INT} = sub { die "Caught SIGINT" };
@ret = $eval->eval($line); 1;
} or @ret = ("Error!", $@);
print Dumper @ret;
}
# shell session:
$ perl -Ilib bin/tinyrepl
re.pl$ my $x = 0;
0
re.pl$ ++$x;
1
re.pl$ $x + 3;
4
re.pl$ ^D
$
π METHODS π
Okay bestie, here are the methods. Pay attention, this is the useful stuff. β¨
π new π
Makes you a fresh Eval::WithLexicals object, and the options are literally right here:
my $eval = Eval::WithLexicals->new(
lexicals => { '$x' => \1 }, # default {}
in_package => 'PackageToEvalIn', # default Eval::WithLexicals::Scratchpad
context => 'scalar', # default 'list'
prelude => 'use warnings', # default 'use strictures 1'
);
Those comments are the defaults, so you know exactly what you're getting. π
π¦ eval π¦
Slap in the code you wanna eval, and catch the return value(s) like this:
my @return_value = $eval->eval($code_to_eval);
This is the method that actually evals. Good.
π lexicals π
Getter and setter for the lexicals, bestie:
my $current_lexicals = $eval->lexicals;
$eval->lexicals(\%new_lexicals);
π in_package π
Getter and setter for the package you're evaling in:
my $current_package = $eval->in_package;
$eval->in_package($new_package);
π context π
Getter and setter for context. Valid values are 'list', 'scalar' or 'void' β the original comment says it, so here it is exactly:
my $current_context = $eval->context;
$eval->context($new_context); # 'list', 'scalar' or 'void'
π§ prelude π§
Code to run before evaling code. Loads strictures by default. (Yes, that's the whole point.)
my $current_prelude = $eval->prelude;
$eval->prelude(q{use warnings}); # only warnings, not strict.
π with_plugins π
Builds a class with the plugins you give it:
my $eval = Eval::WithLexicals->with_plugins("HintPersistence")->new;
Plugins are roles located under a package name like Eval::WithLexicals::With*.
Current plugins are:
HintPersistenceβ when enabled this will persist pragams and other compile hints between evals (for example the strict and warnings flags in effect). SeeEval::WithLexicals::WithHintPersistencefor further details.
π AUTHOR π
Matt S. Trout <mst@shadowcat.co.uk>
πΈ CONTRIBUTORS πΈ
- David Leadbeater
<dgl@dgl.cx> - haarg - Graham Knop (cpan:HAARG)
<haarg@cpan.org>
π COPYRIGHT π
Copyright (c) 2010 the Eval::WithLexicals "AUTHOR" and "CONTRIBUTORS" as listed above.
π LICENSE π
This library is free software and may be distributed under the same terms as perl itself.
Sign-off: this whole thing is the Eval::WithLexicals(3) page from perl v5.32.1, dated 2022-07-22 β bye bestie, go eval something cute! πβ¨
π¦ where this page comes from
- epel 9: perl-Eval-WithLexicals 1.003006-17.el9 Β· GPL+ or Artistic
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms