# See https://stackoverflow.com/questions/57127263/capture-and-execute-multiline-code-and-incorporate-result-in-perl-6
use v6;
constant $ticks = '```';
my regex Search {
$ticks '{raku evaluate=' $<evaluate>=(TRUE|FALSE) '}'
$<code>=[<!before $ticks> .]*
$ticks
}
sub Replace ($/) {
"Code:\n" ~ $ticks ~ $<code> ~ $ticks ~
($<evaluate> eq 'TRUE'
?? "\n\n" ~ 'Output:' ~ "\n" ~ $ticks ~ "\n" ~ Evaluate($<code>) ~ $ticks
!! '');
}
sub Evaluate ($code) {
state $code-so-far = ''; use MONKEY; EVAL $code-so-far; $code-so-far ~= $code;
my $out; my $*OUT = $*OUT but role { method print (*@args) { $out ~= @args } }
use MONKEY; my $eval-result = EVAL $code;
$out // $eval-result ~ "\n"
}
spurt
'example_new.md',
slurp('example.md')
.subst: &Search, &Replace, :g;
say slurp 'example_new.md';
## New language
Raku is a new language different from Perl.
## what does it offer
+ Object-oriented programming including generics, roles and multiple dispatch
+ Functional programming primitives, lazy and eager list evaluation, junctions, autothreading and hyperoperators (vector operators)
+ Parallelism, concurrency, and asynchrony including multi-core support
+ Definable grammars for pattern matching and generalized string processing
+ Optional and gradual typing
This code will be evaluated.
```{raku evaluate=TRUE}
my $answer = 42;
```
Rakudo is a compiler for Raku code. Install it and you're all set to run Raku programs!
```{raku evaluate=TRUE}
say "this is promising";
say $answer;
```
This code will **not** be evaluated.
```{raku evaluate=FALSE}
say "Hello world";
```