# 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 '{perl6 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) {
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
Perl 6 is a new language different from Perl 5.
## 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.
```{perl6 evaluate=TRUE}
4/5
```
Rakudo is a compiler for Perl 6 code. Install it and you're all set to run Perl 6 programs!
```{perl6 evaluate=TRUE}
say "this is promising";
say $*CWD;
```
This code will **not** be evaluated.
```{perl6 evaluate=FALSE}
say "Hello world";
```