ni tutorial 8910 en

5
1/5 www.ni.com 1. 2. 3. 4. 1. 2. 3. 4. 5. 6. 7. 8. Introduction to Scripting in Perl, Python and Tcl Publish Date: Feb 02, 2012 Overview The ability to re-use existing code while migrating to a new test software framework can save developers both time and money. Both NI TestStand test management software and NI LabVIEW give users the ability to directly call and integrate with code written in a variety of languages such as Perl, Python and Tcl. This article examines the basics of three o f the commonly used scripting languages – Perl, Python and Tcl. This is Part 1 o f a three-part series on calling scripting languages from TestStand with a focus on the needs of engineers and scientists. In this first part we will discuss the basic syntax of Perl, Python and Tcl and illustrate how to write simple scripts in these languages.  Table of Contents Introduction Running Scripts Basic Syntax Next Steps 1. Introduction What is a scripting language? Scripting languages are programming language s that are typically written using high-level programming constructs, which makes them easy to learn. While there is no fixed definition of wh at constitutes a scripting language, some of the common distinguishing traits of these languages include: Interpreted: Scripting languages are typically converted into machine level code during runtime by an interpreter, rather than being compiled into an executable before running. While this leads to a performance hit as each line has to be interpreted on the fly, it makes for easier portability between systems. Typeless: Variables can be used to hold a ny type of data without having to explicitly declare their type. While this can make it easy to run into typecasting errors, it makes the language ea sier to learn and can improve readability of the script. Native Complex Types: Most scripting languages also natively provide certain complex data types like strings, arrays, lists and hashes. Garbage Collection: Most scripting languages automate garbage collection (freeing of memory used by data). This can reduce the likelihood of memory leaks occurring. What do scripting language names mean? Perl: Stands for Practical Extraction and Report Language Python: Named after the BBC show “Monty Python’s Flying Circus” Tcl: Stands for Tool Command Language  2. Running Scripts Installing an Interpreter The first step in running a script is downloading an interpreter that will take your script and convert it into ma chine level language. For our tutorials and examples, we used ActiveState’s free interpreters for Perl, Python and Tcl: Perl: (version 5.10)  ActivePerl Python: (version 2.6)  ActivePython Tcl: (version 8.5)  ActiveTcl Setting up the Interpreter One of the benefits of using ActiveState’s interpreters is that the installers will automatically set up the computer’s Path environment variable so that you can call your scripts from any directory. In addition, they will also set up file associations with the file extension (.pl, .py, .tcl) with the ir interpreters so that you can run the script directly by calling its filename. One caveat to be a ware of is that, by defa ult, ActiveTcl will associate .tcl files with a windows shell interpreter - wish - rather than a command line interpreter. In order to run your Tcl scripts directly from the command line, you need to modify the .tcl file association to point to the Tcl command line interpreter - tclsh. The following direction will step you through pointing the .tcl file association to the Tcl command line interpreter. Open Windows Explorer Click Tools»Folder Options Switch to the tab File Types On the listbox, scroll down to the extension TCL and select it Registered file types The details defaults to Opens with Wish Application Click the button Change On the dialog, click and select the ActiveTcl command line interpreter, tclsh Opens With Browse This defaults to: C:\Tcl\Bin\tclsh<version_number>.exe Click the button on the dialog OK Open With  

Upload: yuvaraja

Post on 01-Jun-2018

219 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: NI Tutorial 8910 En

8/9/2019 NI Tutorial 8910 En

http://slidepdf.com/reader/full/ni-tutorial-8910-en 1/51/5 www.ni.c

1.

2.

3.

4.

1.

2.

3.

4.

5.

6.

7.

8.

Introduction to Scripting in Perl, Python and Tcl

Publish Date: Feb 02, 2012

Overview

The ability to re-use existing code while migrating to a new test software framework can save developers both time and money. Both NI TestStand test management software and NI LabVIEW

give users the ability to directly call and integrate with code written in a variety of languages such as Perl, Python and Tcl.

This article examines the basics of three o f the commonly used scripting languages – Perl, Python and Tcl. This is Part 1 o f a three-part series on calling scripting languages from TestStand wit

focus on the needs of engineers and scientists. In this first part we will discuss the basic syntax of Perl, Python and Tcl and illustrate how to write simple scripts in these languages.

 

Table of Contents

Introduction

Running Scripts

Basic Syntax

Next Steps

1. Introduction

What is a scripting language?

Scripting languages are programming languages that are typically written using high-level programming constructs, which makes them easy to learn. While there is no fixed definition of whatconstitutes a scripting language, some of the common distinguishing traits of these languages include:

Interpreted: Scripting languages are typically converted into machine level code during runtime by an interpreter, rather than being compiled into an executable before running. While this le

to a performance hit as each line has to be interpreted on the fly, it makes for easier portability between systems.

Typeless: Variables can be used to hold any type of data without having to explicitly declare their type. While this can make it easy to run into typecasting errors, it makes the language eas

to learn and can improve readability of the script.

Native Complex Types: Most scripting languages also natively provide certain complex data types like strings, arrays, lists and hashes.

Garbage Collection: Most scripting languages automate garbage collection (freeing of memory used by data). This can reduce the likelihood of memory leaks occurring.

What do scripting language names mean?

Perl: Stands for Practical Extraction and Report Language

Python: Named after the BBC show “Monty Python’s Flying Circus”

Tcl: Stands for Tool Command Language

 

2. Running Scripts

Installing an Interpreter 

The first step in running a script is downloading an interpreter that will take your script and convert it into machine level language.

For our tutorials and examples, we used ActiveState’s free interpreters for Perl, Python and Tcl:

Perl: (version 5.10) ActivePerl

Python: (version 2.6) ActivePython

Tcl: (version 8.5) ActiveTcl

Setting up the Interpreter 

One of the benefits of using ActiveState’s interpreters is that the installers will automatically set up the computer’s Path environment variable so that you can call your scripts from any directory.

addition, they will also set up file associations with the file extension (.pl, .py, .tcl) with the ir interpreters so that you can run the script directly by calling its filename.

One caveat to be aware of is that, by default, ActiveTcl will associate .tcl files with a windows shell interpreter - wish - rather than a command line interpreter. In order to run your Tcl scripts dire

from the command line, you need to modify the .tcl file association to point to the Tcl command line interpreter - tclsh. The following direction will step you through pointing the .tcl file associatio

to the Tcl command line interpreter.

Open Windows Explorer 

Click Tools»Folder Options

Switch to the tabFile Types

On the listbox, scroll down to the extension TCL and select itRegistered file types

The details defaults toOpens with Wish Application

Click the buttonChange

On the dialog, click and select the ActiveTcl command line interpreter, tclshOpens With Browse

This defaults to:

C:\Tcl\Bin\tclsh<version_number>.exe

Click the button on the dialogOK Open With

 

Page 2: NI Tutorial 8910 En

8/9/2019 NI Tutorial 8910 En

http://slidepdf.com/reader/full/ni-tutorial-8910-en 2/52/5 www.ni.c

9. Make sure that the TCL extension is now associated with . Click the button.Tclsh Application Apply

NOTE: If you use an interpreter other than ActivePerl, ActivePython and ActiveTcl, you may have to manually set up your Path environment variable to point to the interpreter’s executable. For 

instructions on how to do this on Windows XP, refer to .Microsoft KnowledgeBase 310519: How To Manage Environment Variables in Windows XP

Executing a Script

The typical syntax for running a script from the command line is:<interpreter.exe> <script_filename > <command_line_arguments>

However, since we already associated the script file extensions with the interpreters, we can also directly run the script from the command line using:

<script_filename> <command_line_arguments>

To make sure that your interpreter installed properly, you can try to run the following scripts from the command line.

Perl:

Download hello_world.pl

Run using: perl.exe HelloWorld.pl

Python:

Download hello_world.py

Run using: python.exe HelloWorld.py

Tcl:

Download hello_world.tcl

Run using: tclsh<version_number>.exe HelloWorld.tcl

Editing Scripts

While you can edit a script file in any text editor like notepad, or any text based Development Environment like or Microsoft Visual Studio, we wrote our examples in ActiveStaLabWindows/CVI

free Integrated Development Environment, (version 5.0). We chose Komodo Edit because it’s built in features (autocomplete, calltips, syntax coloring and checking) support theseKomodo Edit

three scripting languages amongst others.

3. Basic Syntax

The following section walks you through the basic syntax used in Perl, Python and Tcl. You can download the entire sample here:

scripting_basics.zip

Declaring and Using Scalar Variables

Perl

#Create a numeric variable

my $myNumber = 5;

#Create a string

my $myString = "Hello World";

#Create a new numeric and assign the original to it

my $myNewNumber = $myNumber;

 

Python

#Create a numeric variable

myNumber = 5;

#Create a string

myString = "Hello World";

#Create a new numeric and assign the original to it

myNewNumber = myNumber;

 

Tcl

#Create a numeric variable

set myNumber 5;

#Create a string

set myString "Hello World";

#Create a new numeric and assign the original to it

set myNewNumber $myNumber;

Page 3: NI Tutorial 8910 En

8/9/2019 NI Tutorial 8910 En

http://slidepdf.com/reader/full/ni-tutorial-8910-en 3/53/5 www.ni.c

 

Displaying output to the Standard Output (STDOUT)

Perl

#Print a string to the screen

print "Hello World\n";

#Print a string and a number 

my $sum = 5;

print "The sum is: ", $sum, "\n\n";

 

Python

#Print a string to the screen

print "Hello World";

#Print a string and a number 

sum = 5;

print "The sum is: ", sum, "\n";

Tcl

#Print a string to the screen

puts "Hello World";

#Print a string and a number 

set sum 5;puts "The sum is: $sum \n";

 

Declaring and Using Lists

Perl

#Declare a list with three elements

my @myList = (5, "foo", 3.14);

#The index of the last element is accessed by $#<listname>

print "Size of myList: ", $#myList + 1, "\n";

#Access a single element in the list

print "The second element in the list is: ", $myList[1], "\n\n";

Python

#Declare a list with three elements

myList = [5, "foo", 3.14];

#The length of the list is accessed by len(<listname>)

print "Size of myList: ", len(myList);

#Access a single element in the list

print "The second element in the list is: ", myList[1], "\n";

 

Tcl

#Declare a list with three elements

set myList {5 "foo" 3.14};

#The index of the last element is accessed by llength

puts "Size of myList: [llength $myList]";

#Access a single element in the list

puts "The second element in the list is: [lindex $myList 1] \n"; 

Reading User Input from the Command Line (Command Line Arguments)

Perl

#Command line arguments are stored in list ARGV#Get number of arguments

print "Number of Command Line Arguments: ", $#ARGV + 1, "\n";

#Access individual argument

print "The first Command Line Argument is: ", $ARGV[0], "\n\n";

 

Python

import sys;

 

Page 4: NI Tutorial 8910 En

8/9/2019 NI Tutorial 8910 En

http://slidepdf.com/reader/full/ni-tutorial-8910-en 4/54/5 www.ni.c

#Command line arguments are stored in list sys.argv

#Get number of arguments

print "Number of Command Line Arguments: ", len(sys.argv) - 1;

#Access individual argument

print "The first Command Line Argument is: ", sys.argv[1], "\n";

 

Tcl

#Command line arguments are stored in list $argv and $argc

#Get number of arguments

puts "Number of Command Line Arguments: $argc";

#Access individual argument

puts "The first Command Line Argument is: [lindex $argv 0] \n";

 

Reading User Input from the Standard Input (STDIN)

Perl

#Read a user input from the keyboard (terminates on return key)

print "Enter value, and then press Enter: ";

my $myUserInput = <STDIN>;

 

Python

#Read a user input from the keyboard (terminates on return key)

myUserInput = raw_input("Enter value, and then press Enter: ");

 

Tcl

#Read a user input from the keyboard (terminates on return key)

puts "Enter value, and then press Enter: ";

gets stdin myUserInput;

 

Syntax of Common Conditional Statements

Perl

#if, elseif, else

if ($myNumber == 5)

{  print "My Number is Five \n";

}

elsif ($myNumber == 3.14)

{

  print "My Number is Pi \n";}

else

{

  print "My Number is neither Five nor Pi \n\n"

}

#while loop

while ($myNumber != 0) #could do: until ($myNumber == 0)

{

  print "My Number is: ", $myNumber, "\n";

  $myNumber -= 1;}

print "\n";

#for loop

for ($myNumber = 0; $myNumber < 5; $myNumber++){

  print "My Number is: ", $myNumber, "\n";

}

print "\n";

#foreach loop

foreach my $currentElement (@myList)

{

  print "The current element is: ", $currentElement, "\n";

}

Python

#if, elseif, else

if (myNumber == 5):

  print "My Number is Five";

elif (myNumber == 3.14):

 

Page 5: NI Tutorial 8910 En

8/9/2019 NI Tutorial 8910 En

http://slidepdf.com/reader/full/ni-tutorial-8910-en 5/55/5 www.ni.c

  print "My Number is Pi";

else:

  print "My Number is neither Five nor Pi \n";

 

#while loop

while (myNumber != 0):

  print "My Number is: ", myNumber;

  myNumber -= 1;

print "\n";

#for loop

for myNumber in range (0, 5):

  print "My Number is: ", myNumber;

print "\n";

#foreach loop

for currentElement in myList:  print "The current element is: ", currentElement;

 

Tcl

#if, elseif, else

if {$myNumber == 5} {

  puts "My Number is Five \n";

} elseif {$myNumber == 3.14} {

  puts "My Number is Pi \n";} else {

  puts "My Number is neither Five nor Pi \n\n";

}

#while loop

while {$myNumber != 0} {

  puts "My Number is: $myNumber";

  incr myNumber -1;}

puts "\n";

#for loop

for {set myNumber 0} {$myNumber < 5} {incr myNumber} {

  puts "My Number is: $myNumber";

}

puts "\n";

#foreach loopforeach currentElement $myList {

  puts "The current element is: $currentElement";

}

 

4. Next Steps

Now that we have looked at the basics of these three scripting languages, we are ready to start integrating our scripts into our test sequences in TestStand. Click on the following link to go to t

next article in the series.

Developer Zone Tutorial: Calling Scripting Languages from TestStand