首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >捕获并执行多行代码,并在raku中合并结果

捕获并执行多行代码,并在raku中合并结果
EN

Stack Overflow用户
提问于 2019-07-20 17:53:39
回答 1查看 364关注 0票数 6

这是一个标记文档example.md

代码语言:javascript
复制
## 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}

4/5

代码语言:javascript
复制
Rakudo is a compiler for raku programming language. Install it and you're all set to run raku programs!

This code will be evaluated.

```{raku evaluate=TRUE}

说“这是有希望的”;

说$*CWD;

代码语言:javascript
复制
This code will **not** be evaluated.


```{raku evaluate=FALSE}

说“你好世界”;

代码语言:javascript
复制

我想将其转换为example.md,如下所示,其中包含代码和输出。

代码语言:javascript
复制
## 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.

Code:
```{raku evaluate=TRUE}

4/5

代码语言:javascript
复制
Output:

0.8

代码语言:javascript
复制
Rakudo is a compiler for raku programming language. Install it and you're all set to run raku programs!

This code will be evaluated.

Code:
```{raku evaluate=TRUE}

说“这是有希望的”;

说$*CWD;

代码语言:javascript
复制
Output:

这是很有希望的

“C:\User\suman”.IO

代码语言:javascript
复制
This code will **not** be evaluated.

Code:
```{raku evaluate=FALSE}

说“你好世界”;

代码语言:javascript
复制

我想要做的是:

  • 捕获backticks{raku evaluate}backticks之间的代码
  • 如果求值为真,则执行代码。
  • 将代码插入文档并输出到文档中

我想做的是:

  1. 捕获多行代码并计算表达式
代码语言:javascript
复制
my $array= 'example.md'.IO.slurp;

#multiline capture code chunk and evaluate separately
if $array~~/\`\`\`\{raku (.*)\}(.*)\`\`\`/ {
    #the first capture $0 will be evaluate
    if $0~~"TRUE"{
        #execute second capture which is code chunk which is captured in $1
        }else {
       # don't execute code
        };
};
  1. 创建一个temp.p6文件并将代码块$1从上面写入其中
代码语言:javascript
复制
my $fh="temp.p6".IO.spurt: $1;
  1. 如果$0为真,则执行块
代码语言:javascript
复制
my $output= q:x/raku temp.p6/ if $0==TRUE
  1. 在创建中间example.md时,将所有这些集成到最终的example_new.md中。
代码语言:javascript
复制
my $fh-out = open "example_new.md", :w; # Create a new file

# Print out next file, line by line
for "$file.tex".IO.lines -> $line {

    # write output of code to example_new.md

}
$fh-out.close;

# copy
my $io = IO::Path.new("example_new.md");
$io.copy("example.md");

# clean up
unlink("example.md");

# move
$io.rename("example.md");

我被困在了第一步。有什么帮助吗?

EN

回答 1

Stack Overflow用户

发布于 2019-07-20 19:30:00

执行代码和捕获输出有两种方法:

  • 您可以将其写入一个tempfile中,并使用my $result = qqx{perl6 $filename}生成一个单独的进程。
  • 您可以使用EVAL在同一个解释器中执行代码,并使用IO::Capture::Simple捕获STDOUT:
代码语言:javascript
复制
my $re = regex {
    ^^ # logical newline
    '```{perl6 evaluate=' (TRUE|FALSE) '}'
    $<code>=(.*?)
    '```'
}

for $input.match(:global, $re) -> $match {
    if $match[0] eq 'TRUE' {
        use IO::Capture::Simple;
        my $result = capture_stdout {
            use MONKEY-SEE-NO-EVAL;
            EVAL $match<code>;
        }
        # use $result now
    }
}

现在您只需要从match切换到subst,并从要替换的块返回值,然后就完成了。

我希望这能让你知道该怎么做。

票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57127263

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档