Author Topic: Embedding PERL in tools  (Read 3886 times)

thomaskilian

  • Guest
Embedding PERL in tools
« on: December 02, 2004, 03:02:06 am »
Hi PERLies,
I wonder if someone comes up with a better solution? To call Perl automation from within EA I use the following construct:
Command: C:\Programme\Sparx Systems\EA\Code Samples\PERL\test.bat
Arguments: <perlfile>, "package=$p", "diagram=$d", "element=$e", "file=$f"
where <perlfile> is the name of the perl file containing my code.
test.bat looks like
Code: [Select]
@echo off
perl "C:\Programme\Sparx Systems\EA\Code Samples\PERL\%1.pl" "%2" "%3" "%4" "%5" "%6" "%7" "%8" %9"
pause

In my called perl files I use a class EAparms.pm
Code: [Select]
package EAparms;

use strict;
use Win32::OLE;

sub new {
 my $self = {};
 my $parms = {};
 my $atFile = 0;

 foreach my $v (@ARGV) {
#    print $v, "\n";
   if ($atFile) {
     if ($v) { $parms->{$1} .= " " . $v;}
   } elsif ($v =~ /(.*)=(.*)/) {
     $parms->{$1} = $2;
     $atFile = ($1 eq 'file');
   }
 }

 $self->{MODEL} = Win32::OLE->new('EA.Repository', \&OleQuit) or die "oops\n";
 $self->{MODEL}->OpenFile($parms->{file}) or die "File not found";
 $self->{DIAGRAM} = $self->{MODEL}->GetDiagramByID ($parms->{diagram})
   if (defined ($parms->{diagram}));
 $self->{PACKAGE} = $self->{MODEL}->GetPackageByID ($parms->{package})
   if (defined ($parms->{package}));
 my $i = 0;

 foreach my $e (split /,/, $parms->{element}) {
   $self->{ELEMENT}->[$i++] = $self->{MODEL}->GetElementByID ($e);
 }
 $self->{ELEMENTS} = $i;
#  print "'", $parms->{file}, "'";

 bless $self;
}

#########################################################
# some cleanup for EA
#
sub OleQuit {
   my $self = shift;
   $self->Exit();
}

1;

which returns the opened ea file and possible diagrams/elements. My main problem was getting the filename right. This version works with filenames that have at highest single blanks embedded. I would not get it to work with a filename containg double blanks.

Any comment appreciated  :)

Fugazy

  • EA Novice
  • *
  • Posts: 1
  • Karma: +0/-0
  • YoMan
    • View Profile
Re: Embedding PERL in tools
« Reply #1 on: December 02, 2004, 12:27:03 pm »
I tried it and it works wonderfully, thanks for the tip
Just remove the double quotes in your bat file and you are set for the file name (you already have the quotes in the EA  tools configuration and it seems that the double double quotes kills each other

ie use
perl "C:\Programme\Sparx Systems\EA\Code Samples\PERL\%1.pl" %2 %3 %4 %5 %6 %7 %8 %9

 :D
« Last Edit: December 02, 2004, 12:28:29 pm by fugazy »

Oscar

  • EA User
  • **
  • Posts: 65
  • Karma: +0/-0
    • View Profile
Re: Embedding PERL in tools
« Reply #2 on: December 05, 2004, 11:16:37 pm »
Thomas,

The problem comes from the communication between DOS and Perl.
Any parameter with one or more blanks is split in two or more ARGV's.

I've been playing with your perl-script and the following  setup works:

EA tools:
Code: [Select]
command: C:\test.pl
arguments: "file=$f" "package=$p" "diagram=$d" "element=$e"


(I have ".pl" files associated with perl so I can directly invoke a script)

Test.pl just creates a new EAparms-object.  The script for EAparms looks like:

Code: [Select]

package EAparms;

use strict;
use Win32::OLE;

sub new {
 my $self = {};
 my $parms = {};

 foreach my $v (@ARGV) {
#    print ">", $v , "<\n";
   if ($v =~ /(.*)=(\w.*)/) {
      $parms->{$1} = $2;
   }
 }

#  print "file: '", $parms->{file}, "'\n" if (defined ($parms->{file}));
#  print "package: '", $parms->{package}, "'\n" if (defined ($parms->{package}));
#  print "diagram: '", $parms->{diagram}, "'\n" if (defined ($parms->{diagram}));
#  print "element(s): '", $parms->{element}, "'\n" if (defined ($parms->{element}));
   
 $self->{MODEL} = Win32::OLE->new('EA.Repository', \&OleQuit) or die "Oops\n";
 $self->{MODEL}->OpenFile($parms->{file}) or die "File not found";
 $self->{DIAGRAM} = $self->{MODEL}->GetDiagramByID ($parms->{diagram})
   if (defined ($parms->{diagram}));
 $self->{PACKAGE} = $self->{MODEL}->GetPackageByID ($parms->{package})
   if (defined ($parms->{package}));

 my $i = 0;
 foreach my $e (split /,/, $parms->{element}) {
   $self->{ELEMENT}->[$i++] = $self->{MODEL}->GetElementByID ($e);
 }
 $self->{ELEMENTS} = $i;

 bless $self;
}

#########################################################
# some cleanup for EA
#
sub OleQuit {
   my $self = shift;
   $self->Exit();
}

1;


If you want the <prevfile>-functionality you could replace test.bat with a test.pl that invokes the wanted perl-file.

HTH,
Oscar

ps
I'm no "Perlie". my last use of Perl was way back in the previous century.
« Last Edit: December 06, 2004, 04:13:46 am by Oscar »

thomaskilian

  • Guest
Re: Embedding PERL in tools
« Reply #3 on: December 06, 2004, 02:19:26 am »
fugazy, thanks for the hint. I was probably thinking too complicated ;)

Oscar,
what you suggested I tried at first. But unfortunately "test.pl parm1" doesn't pass "parm1" to @ARGV :P. Instead I had to call "perl test.pl parm1" (which you can't easily do with the tools menu) in order to access "parm1". So I worked out the above solution. I use W2K/ActiveState Perl.  (I would also not vote for being called a "Perlie" but I have to admit that it has some advantages.)