我可以捕获 RUN-MAIN 中使用的例程的返回值吗? [英] Can I capture the returned value of a routine used in RUN-MAIN?

查看:26
本文介绍了我可以捕获 RUN-MAIN 中使用的例程的返回值吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个脚本来运行从模块导出的子程序,导出的子程序在脚本中作为 MAIN 运行.子例程完成了我想要的一切,只是它返回结果而不是打印结果.

I want a script to run a subroutine exported from a module, with the exported sub to be run as MAIN in the script. The subroutine does all that I want, except that it returns the result instead of printing it.

RUN-MAIN 似乎实现了我的大部分目标,但我不确定如何获取例程的返回值.

RUN-MAIN seems to achieve most of what I'm aiming for, but I'm not sure how to grab the returned value of the routine.

有没有办法可以捕获要打印给 RUN-MAIN 的例程的输出?RUN-MAIN 是处理这类事情的正确方法吗?

Is there a way I can capture the output of the routine given to RUN-MAIN to be printed? Is RUN-MAIN the right approach for this sort of thing?

推荐答案

Redispatch 可以在包装的例程中使用来调用原始例程.say 然后可以用于包装内重新调度的结果.这也将从原始例程中生成用法.

Redispatch can be used within a wrapped routine to call the original. say can then be used on the result of the redispatch within the wrap. This will also generate usage from the original routine.

sub foo (
  $input #= The data we want
) {
  return $input;
}

&foo.wrap( sub (|) { callsame.say } );

RUN-MAIN &foo, Nil;

$ raku filename.raku

Usage:
  filename.raku <input>
  
    <input>    The data we want

这篇关于我可以捕获 RUN-MAIN 中使用的例程的返回值吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆