Scripting in Perl, Python and Tcl With NI Software

Updated Feb 27, 2026

Environment

Software

  • LabVIEW
  • TestStand
  • LabWindows/CVI

Programming Language

  • Python

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 code written in a variety of languages, including Perl, Python, and Tcl.

 

This article examines the basics of three commonly used scripting languages: Perl, Python, and Tcl. It is Part 1 of 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 discuss the basic syntax of Perl, Python, and Tcl and illustrate how to write simple scripts in these languages.

 

Table of Contents

 

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. Although there is no fixed definition of what constitutes a scripting language, several common traits are generally associated with them.

 

Python is one of the most commonly used scripting languages and is known for its readability and versatility. Perl, which stands for Practical Extraction and Report Language, is another widely used scripting language with strong text‑processing capabilities. Tcl, which stands for Tool Command Language, is also frequently used, particularly for rapid development and automation tasks.

 

Scripting languages are usually interpreted, meaning they are converted into machine‑level code at runtime by an interpreter instead of being compiled into an executable beforehand. Although this can result in a performance overhead since each line is interpreted on the fly, it allows for greater portability across systems.

 

These languages are often typeless, allowing variables to hold any type of data without requiring explicit type declarations. While this flexibility can sometimes lead to typecasting errors, it also makes the languages easier to learn and can improve readability.

 

Another common characteristic is the availability of native complex data types such as strings, arrays, lists, and hashes. Additionally, most scripting languages provide automatic garbage collection, which frees memory used by data structures and helps reduce the likelihood of memory leaks.

[Back to TOC]

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, you can use any of the official open‑source distributions of each language:

[Back to TOC]

Setting up the Interpreter

Most official installers for Python, Perl, and Tcl provide the option to automatically add the interpreter to your system PATH. Enabling this option allows you to run scripts from any directory using commands such as `python`, `perl`, or `tclsh`. In addition, you may need to set or adjust file associations so that script files (e.g., .py, .pl, .tcl) execute with the correct interpreter when double‑clicked.

 

Refer to the official documentation of Python, Perl, and Tcl for detailed instructions on configuring file associations and adding interpreter paths to your operating system.

[Back to TOC]

Executing a Script

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

 

However, once we have already associated the script file extensions with their respective interpreters, we can also run a script directly from the command line using: <script_filename> <command_line_arguments>

 
To verify that your interpreter is installed correctly, try running the following sample scripts. You can either open each script file directly or execute it through the interpreter from the command line. The downloadable sample files are attached at the bottom of this article.
  • Perlperl.exe HelloWorld.pl
  • Python: python.exe HelloWorld.py
  • Tcltclsh<version_number>.exe HelloWorld.tcl

[Back to TOC]

Editing Scripts

You can edit script files using any plain‑text editor. However, using a modern code editor can greatly improve your workflow by providing features such as syntax highlighting, autocomplete, and integrated debugging. Common and well‑supported editors include Visual Studio Code and Notepad++. Most scripting languages also provide their own official or default editors and IDEs, such as Padre, the Perl IDE or IDLE for Python. You can use any of these tools, or whichever editor best fits your workflow, to create and modify script files.

[Back to TOC]

Basic Syntax

The following section walks you through the basic syntax used in Perl, Python, and Tcl. You can download the complete sample at the bottom of this article.



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;

[Back to TOC]

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: " + str(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";

[Back to TOC]

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: " + str(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";

[Back to TOC]


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; #Command line arguments are stored in list sys.argv #Get number of arguments print("Number of Command Line Arguments: " + str(len(sys.argv) - 1)); #Access individual argument print("The first Command Line Argument is: " + str(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";

[Back to TOC]

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;

[Back to TOC]

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): print("My Number is Pi"); else: print("My Number is neither Five nor Pi \n"); #while loop while (myNumber != 0): print("My Number is: " + str(myNumber)); myNumber -= 1; print("\n"); #for loop for myNumber in range (0, 5): print("My Number is: " + str(myNumber)); print("\n"); #foreach loop for currentElement in myList: print("The current element is: " + str(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 loop foreach currentElement $myList { puts "The current element is: $currentElement"; }

[Back to TOC]

Next Steps

Now that we have reviewed the basics of these three scripting languages, we are ready to proceed to: