Difference between revisions of "Studio:Can I use Perl's Tk package in STRIDE?"

From STRIDE Wiki
Jump to: navigation, search
Line 5: Line 5:
 
Script to launch the Tk script. (Note: The Tk script has to be included in the workspace script files).
 
Script to launch the Tk script. (Note: The Tk script has to be included in the workspace script files).
 
<source lang="perl">
 
<source lang="perl">
use strict;
+
use strict;
use File::Spec;
+
use File::Spec;
 
   
 
   
#Get path and name of perl exectuable           
+
#Get path and name of perl exectuable           
my $perlExe = $^X;
+
my $perlExe = $^X;
if ($perlExe =~ /\.dll$/i)  
+
if ($perlExe =~ /\.dll$/i)  
{
+
{
    my ($vol, $dir, $file) = File::Spec->splitpath($perlExe);
+
  my ($vol, $dir, $file) = File::Spec->splitpath($perlExe);
    $perlExe = File::Spec->catpath($vol, $dir, 'perl.exe');  
+
  $perlExe = File::Spec->catpath($vol, $dir, 'perl.exe');  
}
+
}
 
   
 
   
# get the name and path of the script with the Tk library
+
# get the name and path of the script with the Tk library
my $tkScript= $main::studio->Workspace->Files->Item("tk.pl");
+
my $tkScript= $main::studio->Workspace->Files->Item("tk.pl");
my $tkFullPath = File::Spec->catfile($tkScript->Path, $tkScript->Name);  
+
my $tkFullPath = File::Spec->catfile($tkScript->Path, $tkScript->Name);  
 
   
 
   
#List of arguments for Tk script
+
#List of arguments for Tk script
my @options = ('"option 1"', '"option 2"', '"option 3"', '"option 4"');
+
my @options = ('"option 1"', '"option 2"', '"option 3"', '"option 4"');
 
   
 
   
#Start the script  
+
#Start the script  
my @data = `$perlExe \"$tkFullPath\" @options`;  
+
my @data = `$perlExe \"$tkFullPath\" @options`;  
 
   
 
   
#@data is the array with the return values (through STDOUT)
+
#@data is the array with the return values (through STDOUT)
for (my $i=0; $i <= $#data; $i++)
+
for (my $i=0; $i <= $#data; $i++)
{
+
{
  $main::ascript->MessageBox("$data[$i]", "Element $i");
+
  $main::ascript->MessageBox("$data[$i]", "Element $i");
}
+
}
 
</source>
 
</source>
 
            
 
            
 
And here is the Tk script  
 
And here is the Tk script  
<source lang="perl">
+
<source lang="tcl">
#!c:/perl/bin/perl -w
+
#!c:/perl/bin/perl -w
use strict;
+
use strict;
use Tk;
+
use Tk;
my $mw = MainWindow->new;
+
 
my $fileName;
+
my $mw = MainWindow->new;
FillWindow($mw, 'Choose Script to Run:',@ARGV);
+
my $fileName;
 +
FillWindow($mw, 'Choose Script to Run:',@ARGV);
 
   
 
   
MainLoop;
+
MainLoop;
 
   
 
   
sub FillWindow  
+
sub FillWindow  
{
+
{
    my ($window, $header,@List) = @_;
+
  my ($window, $header,@List) = @_;
    my @selected;
+
  my @selected;
    my $i;
+
  my $i;
    $window->Label(-text => "$header")->pack;
+
  $window->Label(-text => "$header")->pack;
    foreach (@List)
+
  foreach (@List)
    {
+
  {
      push @selected, 0;
+
    push @selected, 0;
      $mw->Checkbutton(-text=>"$_",-variable=>\$selected[$i])->pack;
+
    $mw->Checkbutton(-text=>"$_",-variable=>\$selected[$i])->pack;
      $i++;
+
    $i++;
    }
+
  }
    $mw -> Button(-text=>"Select", -command =>sub
+
  $mw -> Button(-text=>"Select", -command =>sub
 
                 {
 
                 {
 
                     my @selectedOptions;
 
                     my @selectedOptions;
 
                     for (my $i=0; $i <= $#List; $i++)
 
                     for (my $i=0; $i <= $#List; $i++)
 
                     {
 
                     {
                        if ($selected[$i])
+
                      if ($selected[$i])
                        {
+
                      {
                            push @selectedOptions, $List[$i];
+
                        push @selectedOptions, $List[$i];
                        }
+
                      }
                    }
+
                    }
                    print STDOUT join(" \n ", @selectedOptions);
+
                    print STDOUT join(" \n ", @selectedOptions);
                    $mw -> destroy;
+
                    $mw -> destroy;
 
          })->pack;
 
          })->pack;
 
+
 
}  
+
}
</source>           
+
</source>           
  
 
[[Category:Perl Info]]
 
[[Category:Perl Info]]

Revision as of 10:42, 2 October 2008

We recommend running the script which uses the Tk package as a separate script outside of STRIDE Studio, as there is an incompatibility with the Tk package that causes errors during execution. You should not get an error the first time you run the script, but you will get errors during every subsequent execution until you shut down Studio. This is due to a problem with initialization and the Perl engine failing to fully clean up; the Tk engine relies on the cleanup code, but since this only occurs when the engine unloads (i.e., Studio is closed), errors occur during execution.

To run the script using Tk outside of Studio, you can launch it from a script in Studio using the system command. You can also pass arguments between the scripts. Below is an example.

Script to launch the Tk script. (Note: The Tk script has to be included in the workspace script files).

use strict;
use File::Spec;
 
#Get path and name of perl exectuable          
my $perlExe = $^X;
if ($perlExe =~ /\.dll$/i) 
{
  my ($vol, $dir, $file) = File::Spec->splitpath($perlExe);
  $perlExe = File::Spec->catpath($vol, $dir, 'perl.exe'); 
}
 
# get the name and path of the script with the Tk library
my $tkScript= $main::studio->Workspace->Files->Item("tk.pl");
my $tkFullPath = File::Spec->catfile($tkScript->Path, $tkScript->Name); 
 
#List of arguments for Tk script
my @options = ('"option 1"', '"option 2"', '"option 3"', '"option 4"');
 
#Start the script 
my @data = `$perlExe \"$tkFullPath\" @options`; 
 
#@data is the array with the return values (through STDOUT)
for (my $i=0; $i <= $#data; $i++)
{
  $main::ascript->MessageBox("$data[$i]", "Element $i");
}

And here is the Tk script

#!c:/perl/bin/perl -w
use strict;
use Tk;

my $mw = MainWindow->new;
my $fileName;
FillWindow($mw, 'Choose Script to Run:',@ARGV);
 
MainLoop;
 
sub FillWindow 
{
  my ($window, $header,@List) = @_;
  my @selected;
  my $i;
  $window->Label(-text => "$header")->pack;
  foreach (@List)
  {
    push @selected, 0;
    $mw->Checkbutton(-text=>"$_",-variable=>\$selected[$i])->pack;
    $i++;
  }
  $mw -> Button(-text=>"Select", -command =>sub
                 {
                    my @selectedOptions;
                    for (my $i=0; $i <= $#List; $i++)
                    {
                      if ($selected[$i])
                      {
                        push @selectedOptions, $List[$i];
                      }
                    }
                    print STDOUT join(" \n ", @selectedOptions);
                    $mw -> destroy;
 	         })->pack;	

}