Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

can someone explain Devel::REPL::Plugin::LexEnv?

by tospo (Hermit)
on Jul 11, 2012 at 11:46 UTC ( #981089=perlquestion: print w/replies, xml ) Need Help??
tospo has asked for the wisdom of the Perl Monks concerning the following question:

Maybe it's obvious to others but I don't get what Devel::REPL::Plugin::LexEnv is for and what the example code is actually doing.
I would like to understand the REPL plugins to see if I can get a bit more out of this really useful tool but the documentation for most of them is a bit sparse.
Thanks for your help!
  • Comment on can someone explain Devel::REPL::Plugin::LexEnv?

Replies are listed 'Best First'.
Re: can someone explain Devel::REPL::Plugin::LexEnv?
by Corion (Pope) on Jul 11, 2012 at 12:05 UTC

    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.

      OK, thanks, then I guess this plugin is loaded by default because when I start the Devel::REPL shell, I do get a lexical environment already. Thanks for the clarification!

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://981089]
Approved by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2017-11-24 06:33 GMT
Find Nodes?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:

    Results (345 votes). Check out past polls.