File & process IO
FullRead and write files, run subprocesses — real in Raku++; the browser has files but no processes.
Raku++ can read and write files and launch subprocesses, exactly as Rakudo does. The browser playground can't spawn processes, so these examples are shown with their verified output (from the interpreter and --exe) rather than a Run button.
Files are a different story: the WebAssembly build inherits an in-memory filesystem from Emscripten, so the spurt/slurp example below does work in a browser — against a /tmp that lives in the tab's memory, starts empty and vanishes when the page reloads.
Running a subprocess #
run launches an external command; with :out you capture its standard output.
my $r = run "echo", "from a subprocess", :out;
say $r.out.slurp(:close).chomp;from a subprocessWriting and reading a file #
spurt writes a whole string to a path; slurp (or .lines) reads it back. Here a temp file is written, its lines counted and indexed, then removed.
my $f = $*TMPDIR.add("spec-io-demo.txt");
spurt $f, "alpha\nbeta\ngamma\n";
say $f.lines.elems;
say $f.lines[1];
$f.unlink;3
betaNotes #
- File handling:
spurt/slurpfor whole files,openfor a handle,.lines/.wordsto iterate,IO::Pathmethods (.e,.d,.add,.unlink) for paths. run/shellstart subprocesses;:out/:errcapture their streams.$*TMPDIR,$*CWD,$*HOMEareIO::Pathhandles to standard locations.- Subprocesses don't exist in the browser sandbox. Files do, but in an in-memory filesystem that starts empty and is discarded with the page. For real files on a real disk, run the program with the interpreter (
rakupp program.raku) or a compiled binary (rakupp --exe program.raku).