Eval::WithLexicals(3)

πŸ“¦
version 1.003006 Β· 4 versions
, in epel 9

πŸ’– 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). See Eval::WithLexicals::WithHintPersistence for 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 (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