When implemented naively, a REPL has no lexical environment, or rather, all lexical variables declared for one iteration of REP are lost when the next Loop starts. A naive REPL would look like this:
#!perl -w
use strict;
while (1) {
print ">";
my $statement = <>; # Read
print "---\n";
my @res= eval $statement; # Eval
if( $@ ) {
print "ERROR: $@\n";
} else {
print Dumper \@res; # Print
print "\n";
};
};
The usage could be as follows:
>my $name='World'; "Hello $name"
---
$VAR1 = [
'Hello World'
];
>
If you try to be a good citizen and create a lexical variable in one statement:
> my $name = 'World';
---
$VAR1 = [
'World'
];
... and then try to print the value of $name in the next statement:
> "Hello $name";
---
ERROR: Global symbol "$name" requires explicit package name at (eval 4
+) line 1, <> line 4.
you will find that Perl complains about $name not being declared. This happens because everything you create lexically is thrown away after the eval part of the REPL has finished. The technical term is "the scope of the eval statement has been left". If you want to learn more about scope, see Coping With Scoping
The mentioned plugin seems to provide one lexical scope across multiple iterations of the loop. |