如何从 tcl 脚本调用 Perl 脚本 [英] How to Call Perl script from tcl script

查看:92
本文介绍了如何从 tcl 脚本调用 Perl 脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含 4 个 perl 命令的文件,我想从 tcl 打开文件并执行每个 perl 命令.

I have a file with 4 perl commands , I want to open the file from the tcl and execute each perl command.

TCL 脚本

runcmds $file

proc runcmds {file} {

    set fileid [open $file "r"]
    set options [read $fileid]
    close $fileid

    set options [split $options "\n"]    #saperating each commad  with new line

    foreach line $options {
    exec perl $line  
    }
}

执行上述脚本时我收到的错误是 无法打开 perl 脚本/../../: No such file or directory" 使用 -S 来搜索 $PATH .

when executing the above script I am getting the error as "can't open the perl script /../../ : No Such file or directory " Use -S to search $PATH for it.

推荐答案

tl;dr: 您缺少 -e,导致您的脚本被解释为文件名.

tl;dr: You were missing -e, causing your script to be interpreted as a filename.

从 Tcl 内部运行 perl 命令:

To run a perl command from inside Tcl:

proc perl {script args} {
    exec perl -e $script {*}$args
    # or in 8.4: eval [list perl -e $script] $args
}

然后你可以这样做:

puts [perl {
    print "Hello "
    print "World\n"
}]

没错,Tcl 中的任意 perl 脚本.您甚至可以根据需要传入其他参数;通过 @ARGV 从 perl 访问.(您需要显式添加其他选项,例如 -p.)

That's right, an arbitrary perl script inside Tcl. You can even pass in other arguments as necessary; access from perl via @ARGV. (You'll need to add other options like -p explicitly.)

注意这可以传递整个脚本;您不需要将它们分开(并且可能不应该;您可以用单线做很多事情,但它们往往很难维护,而且没有技术上的理由需要它).

Note that this can pass whole scripts; you don't need to split them up (and probably shouldn't; you can do lots with one-liners but they tend to be awful to maintain and there's no technical reason to require it).

这篇关于如何从 tcl 脚本调用 Perl 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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