Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl6 REPL usage

Is it possible to have (Rakudo) Perl6 execute some code before dropping you into the REPL? Like python does with "python -i ".

For instance, I want to load up some modules and maybe read a side file and build some data structures from that side file before dropping into the REPL and letting the user do the things they need to do on the data structure, using the REPL as a user interface.

This is similar but different than Start REPL with definitions loaded from file though answers to this question might satisfy that one. The basic case is that, at the end of execution of any program, instead of exiting, the interpreter leaves the user at the REPL. Aside from providing a nifty, built-in, Perl6-based user interface for interactive programs, it also provides a good tool from which to debug code that otherwise exits with an error.

edit:

Selecting Zoffix's solution as the correct (so far) one as it is the only one that satisfies all requirements as stated. Here's hoping this capability gets added to the compiler or language spec.

like image 461
Calcite Avatar asked May 01 '18 16:05

Calcite


1 Answers

You can load modules with the -M switch.

$ perl6 -MJSON::Tiny

To exit type 'exit' or '^D'
> to-json Array.new: 1,2,3.Str
[ 1, 2, "3" ]
> 

If you want to run other code, currently you have to put it into a module first.

$ mkdir lib
$ echo 'our $bar = 42' > lib/foo.pm6
$ perl6 -Ilib -Mfoo

To exit type 'exit' or '^D'
> $bar
42
> 
like image 163
Brad Gilbert Avatar answered Nov 16 '22 12:11

Brad Gilbert