Tuesday, November 19, 2013

Glimpses of scripting languages

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 what
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 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 easier 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

Basic Syntax:
The following section walks you through the basic syntax used in Perl, Python and Tcl.
 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;


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;
#Command line arguments are stored in list sys.argv
4/5 www.ni.com
#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";
elseif (myNumber == 3.14):
print "My Number is Pi";
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 loop
foreach currentElement $myList {
puts "The current element is: $currentElement";
}



With Courtesy,
http://www.ni.com/white-paper/8910/en/


No comments:

Post a Comment