11

I open raku/rakudo/perl6 thus:

con@V:~/Scripts/perl6$ perl6
To exit type 'exit' or '^D'
> 

Is the above environment called the "interpreter"? I have been searching forever, and I cannot find what it's called.

How can I execute a rakudo script like I would do

source('script.R') in R, or exec(open('script.py').read()) in python3?

To clarify, I would like the functions and libraries in the script to be available in REPL, which run doesn't seem to do.

I'm sure this exists in documentation somewhere but I can't find it :(

con
  • 5,767
  • 8
  • 33
  • 62
  • 1
    Do you just want to execute the script file, or do you want the functions defined in the file available in the REPL? – rajashekar Oct 24 '21 at 08:49
  • @rajashekar I would like the libraries/modules and functions from the script available in REPL – con Oct 24 '21 at 12:23
  • If at the Linux/Unix shell command line, you type `raku` followed by your script name `my_raku_script.p6`, the `rakudo` compiler/interpreter executes the `my_raku_script.p6` for you. If at the Linux/Unix shell command line you type `raku` followed by a return, the `rakudo` compiler/interpreter opens a REPL for you. – jubilatious1 Oct 24 '21 at 20:35
  • 2
    .@con My guess is that [the `repl` function Liz added to Rakudo](https://github.com/rakudo/rakudo/commit/4183cdaf37c842fb74eb5a08c2cf12075d4c55ec) earlier this year would do the trick if you're willing to call it from within your script. But it's not mentioned in the Raku docs (which is fair enough if it's Rakudo specific, which I imagine it is), and I haven't found it elsewhere either (which is not so cool but might be my lack of search skills/patience), and nor have I managed to get it to work. Anyhow, maybe .@Liz will read this and provide an answer... – raiph Oct 24 '21 at 23:05

2 Answers2

7

It's called Read-Eval-Print Loop REPL. You can execute raku scripts direct in the shell: raku filename.raku without REPL. To run code from REPL you can have a look at run (run <raku test.raku> ) or EVALFILE.

The rosettacode page Include a file has some information. But it looks like there is no exact replacement for your R source('script.R') example at the moment.

LuVa
  • 2,288
  • 2
  • 10
  • 20
7

As Valle Lukas has said, there's no exact replacement. However, all usual functions are there to run external programs,

  • shell("raku multi-dim-hash.raku") will run that as an external program.
  • IIRC, source also evaluated the source. So you might want to use require, although symbols will not be imported directly and you'll have to use indirect lookup for that.
  • You can also use EVAL on the loaded module, but again, variables and symbols will not be imported.
jjmerelo
  • 22,578
  • 8
  • 40
  • 86
  • Hi @JJ. ❶ Do you know how to get the `repl` function to work? (The one Liz introduced into Rakudo per my comment on .@con's question.) I tried but failed to get it to work. ❷ If you *can*, am I right to think that, if you run .@con's program (script) and *it* calls the `repl` function from within that program, then global variables/symbols (and perhaps even lexical ones?) *are* accessible in the REPL? ❸ Is this capability re-entrant, i.e. if one starts a REPL, then invokes a program from there that calls `repl` (to get an inner REPL), does the inner REPL function as correctly as the outer one? – raiph Oct 25 '21 at 13:35
  • @raiph I didn't even know it existed... – jjmerelo Oct 25 '21 at 15:16
  • 1
    .@JJ Ah. Hmm. If you can get it working (maybe ask Liz?), and ❷ pans out, then I'd say that that would be a very valuable addition to your answer (and/or it would be great if someone added another answer that describes it). I'm not sure if it's meant to be part of Raku or just Rakudo (I presume the latter), and even if it's just Rakudo, I could well imagine it's not intended to be something that's officially supported, but either way it does seem like a pretty big deal. @con can't be the only one wanting to be able to run a program and get access to its symbols, including ones it imports. – raiph Oct 25 '21 at 18:08