0

I have a variable defined in a perl script.

How to access that same variable with the same value as defined in a tcl script which is present in the same directory as where the perl script is present

1
  • Is the tcl script started from the perl script? Commented Feb 14, 2014 at 9:34

2 Answers 2

2

You could write it out to a config file in the perl script and then have the tcl script read the value in from that config file.

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

1 Comment

YAML would be a good choice that both languages can read/write: perl using perhaps YAML::Tiny, tcl using the yaml package from tcllib. This is a good answer if the data to be shared is a more complex data structure.
2

It's very hard to know the best way to answer this without knowing the exact relationship of the Perl and Tcl programs to each other. It might be that the best way to pass a copy of the value is as an argument to the call of the other script (that's trivial in both languages) or as an environment variable. Or maybe you could store the value in a shared file; storing it in a database (e.g., in SQLite) is really just a grown-up version of this. Or you could go to quite a bit of effort and graft the two languages together so that it's really a single shared value that both languages can see at the same time. (That takes a lot more effort.)

The two easy ways are below; if they look pretty similar to you, that's because it's the same pattern in reverse.

The easy ways: Perl → Tcl

On the Perl side:

my $theResult = `tclsh script.tcl "$theArgument"`;

On the Tcl side (in script.tcl):

set theArgument [lindex $argv 0]
# .... process ....
puts $theResult

The easy ways: Tcl → Perl

On the Tcl side:

set theResult [exec perl script.pl $theArgument]

On the Perl side (in script.pl):

my $theArgument = $ARGV[0];
# .... process ....
print "$result\n";

2 Comments

added some quotes to the perl->tcl example: the string in backticks in perl is processed by /bin/sh, so you have to worry about shell quoting.
Plus if I was really serious about it, I'd be looking at using Perl's lower-level fork/execve support (so as to avoid the trip through the shell and all its pitfalls). But that introduces a lot more complexity — or a library that I don't know — and makes my central point (the general similarity) less clear.

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.