Useful Perl Routines

The goal in this chapter is not to re-write Perl documentation, which can be found at the URL TBC. But some useful lines can be found here.

To write the functions contained in atl_cc.pl, atl_link.pl and atl_exec.pl, which are atl_cc, atl_cpp and atl_link, atl_exec and atl_execdbg, here is a list of very useful function and lines:

Perl variables containing file names:

All backslashes must be repeated.

my $ccPath = " C:\\Tornado\\host\\x86-win32\\bin";

 

The following syntax, equivalent to the previous one, may be preferred:

my $ccPath = " C:/Tornado/host/x86-win32/bin";

 

Perl variables containing quotes or double quotes:

All special characters must be preceded with a backslash.

 my $i = "\"-Ic:\\Tornado\\target\\h\"";

 

Translate a reference to an array $Includes into a scalar variable containing  "-Ipath1 -Ipath2"

My $Includes=shift; #Given as parameter

my $includes = "";

foreach ( @$Includes ) {$includes .= " -I$_" ;}

or

$includes="-I".join(" -I",@$Includes);

 

Some compilers require that if an element of the Includes list contains a white space, the -I option should double-quoted. That would become:

my $includes = "";

foreach ( @$Includes ) {$includes .= " -I\"$_\"" ;}

 

The same algorithm is applicable to the $Defines array.

Use of .ini fields

Any field specified in the tp.ini, tpcpp.ini, ada83.ini or ada95.ini can be retrieved by accessing

$Ini{'FIELDNAME'}

Use of predefined Test RealTime variables

$BINDIR: Contains the bin directory where executable can be found:

<InstallDir>/bin/intel/win32

 

$TARGETDIR: Contains the root directory of the Target Deployment Port, for example:

<InstallDir>/targets/cNewTdp

 

$INTDIR: Contains a intermediate working directory, consisting by default in the concatenation of the workspace directory and the directory name of the chosen Target Deployment Port. This can be modified in the Test RealTime user interface Test Configuration Settings, tab General and Directories. For example, its value could be:

C:\MyTest\TdpDev\TdpDev_myuserId\cNewTdp

Use of environment variables

Any environment variable can be retrieved or set, with the following syntax:

 $ENV{'VARIABLENAME'}="NewValue";  # Set the variable

or

my $value=$ENV{'VARIABLENAME'};  #Read the variable

The environment variable can be set by default in the user's environment, or can have been specified in the .ini file by the syntax

ENV_VARIABLENAME="Value"

 

Run a command

my $ret=SystemP("Command With Args");

or

my $ret=SystemP("Program","With","Args");

 

SystemP prints the command and executes it with a regular Perl command. It returns the exit code of the executed command.

Run a command on Windows, within a DOS tool

my $ret=SystemP("$ENV{'COMSPEC'} /c start /w Command");

 

Test RealTime contains the Dos-Shell path. The /w flags indicated to wait for the end of the execution.

Copy a File

copy("File1.txt","File2.txt");

copy($File1,$File2);

Rename a File

RenameFile("File1.txt","File2.txt");

RenameFile($File1,$File2);

 

Returns 1 on success.

Returns 0 on error.

Suppress a File

unlink("FileName");

unlink($fileName);

Getting the Current Directory

my $pwd=getcwd();

print "Current Directory is $pwd\n";

Changing Directory

my $ret=Chdir("NewDir");

 

Returns 0 on success, anything else on error.

Getting the Dirname/Basename and Extension of a File

my ($dirname,$basename,$extension)=SplitFileName(""C:\\Program Files\\Rational\\TestRealTime\\targets\\cNewTdp\\tp.ini");

 

$basename contains "tp"

$dirname contains "C:\\Program Files\\Rational\\TestRealTime\\targets\\cNewTdp"

$extension contains "ini"

Testing the Existence of a Regular File

if ( -r "FileName )

{

# File Exists

}

Testing the Existence of a Directory

if ( -d "DirName )

{

# Directory Exists

}

Open a File for Read or Write

open (INFILE,"<InFileForRead.txt") || die "Cannot open file for read\n";

open(OUFILE,">OutFileForWrite.txt") || die "Cannot open file for write\n";

while(<INFILE>)

{

     chomp;  #Suppress the new line at the end if any

     print OUFILE "That is a line coming from InFile: ".$_."\n";

}

close(INFILE);close(OUFILE);