Showing posts with label tcl.pm demo createcommand perl tcl interaction. Show all posts
Showing posts with label tcl.pm demo createcommand perl tcl interaction. Show all posts

Friday, July 30, 2010

Tcl.pm Perl <> Tcl Interaction.

I don't really like to work in Tcl, but a lot of the devices I use for work have Tcl API's. Here's some demo's of how I use perl to interact with the Tcl Api's of these devices, using the Tcl.pm module from cpan:

http://search.cpan.org/~vkon/Tcl-0.98/Tcl.pm


Using a perl subroutine from within tcl code:

########################################################
test.pl:
########################################################

#!/usr/bin/perl

use Tcl;

sub log_out {
print "Hello, @_!\n";
}

#create a function to be used called logOut in tcl but have it reference the perl log_out function
$interp = new Tcl;
$interp->CreateCommand("logOut", \&log_out,undef,undef,'1');
$interp->Eval("source test.tcl");

########################################################
test.tcl:
########################################################

puts "calling logOut"

logOut "FRED";

########################################################
output:
########################################################

calling logOut
Hello, FRED!



#########################################################
Sharing Variables between Tcl <> perl:
#########################################################

#########################################################
test2.pl
#########################################################

use Tcl;
use strict;
use warnings;

my $scalar = '';
my $name = '';

print "Examples manipulating TCL variables\n";

# Initiate a new tclsh instance
my $interp = Tcl->new;

# bind the tcl and perl variables together
tie($scalar, 'Tcl::Var', $interp, 'tclscalar');

#set value in tcl get it from tcl and perl
$interp->SetVar ('tclscalar', '45');
$interp->Eval('puts "$tclscalar, Got from TCL"');
print "$scalar Got from Perl\n";

#set value in perl get it from tcl and perl
$scalar = '50';
$interp->Eval('puts "$tclscalar, Got from TCL"');
print "$scalar Got from Perl\n";

#########################################################
output:
#########################################################

Examples manipulating TCL variables
45, Got from TCL
45 Got from Perl
50, Got from TCL
50 Got from Perl