1

I'm trying to execute a Perl script from Tcl.

I use eval in Tcl, and it executes the Perl script but doesn't execute the other command in my Tcl file.

Tcl:

eval perl perl_script
puts "Command executed"

Result: the Perl script is executed, but not the puts command. Why is this?

2
  • 1
    I don't know anything about tcl, but this page says eval executes tcl passed to it as an argument rather than launch an external program (much like Perl's eval EXPR executes the Perl code in which EXPR results). I find your claim that this executes the Perl script doubtful, and I think that your problem will be solved if you actually used the correct function to launch a program. Commented Jul 16, 2014 at 16:27
  • In Tcl it's more suitble to use exec to run an external program, but even with exec i got the same behavior. Commented Jul 16, 2014 at 16:30

2 Answers 2

5

In tcl, eval executes its arguments as tcl code, similar to its perl equivalent. But, you do not want this, instead you need to launch the perl interpreter as an external process and ask this interpreter to execute the script passed in its commandline. This is the job of exec in tcl.

exec perl perl_script

Read more about exec @ https://www.tcl.tk/man/tcl/TclCmd/exec.htm

UPDATE

exec will throw an error when the command returns a non-zero exit status. You need wrap exec with a catch to continue with non zero exit status. See WORKING WITH NON-ZERO RESULTS

UPDATE 2

The reason why eval perl script_path manages to successfully launch the perl interpreter is tcl's unknown magic. It is likely that you do not have tcl proc named perl. So, tcl calls the "unknown" proc, which tries to handle this exception intelligently by calling exec on its arguments since it finds an executable in $env(PATH). You can try info body unknown to see how this magic actually works.

Sign up to request clarification or add additional context in comments.

1 Comment

The unknown magic is long, magical, and likely to scare the horses. Don't worry about it too much.
0

Try this:

exec sh -c { perl -ape 's/this/that/' tmp.1 > tmp.11 }

It works for me.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.