Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

re.pl: Variable not properly scoped inside block

Example:

~ $ re.pl
$ { my $abc = 10 ; $abc }
10
$ $abc
10
$ 

Is this a documented gotcha?

like image 366
dharmatech Avatar asked Feb 15 '26 02:02

dharmatech


1 Answers

This appears to be a bug in Lexical::Persistence, which Devel::REPL uses to manage the lexical environment persisting across multiple evals.

Here's a demonstration of the bug without Devel::REPL. This code incorrectly produces the value of $abc, 10, even though it's in an inner scope.

use strict;
use warnings;
use Lexical::Persistence;

my $environment = Lexical::Persistence->new;
$environment->call(sub {
    my $foo = shift;
    { my $abc = 10 };
    return $foo;
});

print $environment->get_context('_')->{'$abc'};

I've reported a bug against the module, we'll see what happens!

It's also worth noting that Matt Trout (the primary author of Devel::REPL)'s new lexical persistence module, Eval::WithLexicals does not suffer from this problem:

use strict;
use warnings;
use Eval::WithLexicals;

my $environment = Eval::WithLexicals->new;
print $environment->eval('{ my $abc = 10 ; $abc }'), "\n";
print $environment->eval('$abc'), "\n";

produces 10 as expected, then the second eval throws the expected Global symbol "$abc" requires explicit package name error.

like image 186
sartak Avatar answered Feb 16 '26 14:02

sartak



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!