1
votes

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

TCL script

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  
    }
}

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.

1
Overall, it looks good to me. If I were you I would try with full paths for everything: both the perl binary, as well as anything that may come in the $options variable. - James

1 Answers

2
votes

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


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
}

Then you can do:

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

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).