Scripting
Introduction
Last Updated: May 4, 2025In this section you will find the tutorials on Scripting used in various verfication tasks and postprocessing of log files etc.
Perl Tutorial
Last Updated: May 4, 2025Contents Index: — Perl Definition : — Syntax & Variables : — strings : — Arrays : — Associative Arrays : — If & While Loop : — File Input : — Print Output : — Strings and Regular Expressions : — Subroutines : — Running External Programs : — References : — Terse...
What is Perl
Last Updated: May 4, 20251. What Is Perl? Perl is a free, open source programming language created by Larry Wall. Perl aims for adjectives like “practical” and “quick” and not so much words like “structured” or “elegant”. A culture has built up around Perl where people create and give away modules, documentation, sample code and a thousand other useful...
Perl: Syntax And Variable
Last Updated: May 4, 2025Syntax And Variables The simplest Perl variables are “scalar” variables which hold a single string or number. Scalar variable names begin with a dollar sign ($) such as $sum or $greeting. Scalar and other variables do not need to be pre-declared — using a variable automatically declares it as a global variable. Variable names and...
Perl Strings
Last Updated: May 4, 2025Strings Strings constants are enclosed within double quotes (“) or in single quotes (‘). Strings in double quotes are treated specially — special directives like \n (newline) and \x20 (hex 20) are expanded.More importantly, a variable, such as $x, inside a double quoted string is evaluated at run-time and the result is pasted into the...
Perl Arrays
Last Updated: May 4, 2025Arrays Array constants are specified using parenthesis ( ) and the elements are separated with commas. Perl arrays are like lists or collections in other languages since they can grow and shrink, but in Perl they are just called “arrays”. Array variable names begin with the at-sign (@). Unlike C, the assignment operator (=) works...
Perl Associative Arrays
Last Updated: May 4, 2025Associative Arrays/Hash Arrays Hash arrays, also known as “associative” arrays, are a built-in key/value data structure. Hash arrays are optimized to find the value for a key very quickly. Hash array variables begin with a percent sign (%) and use curly braces { } to access the value for a particular key. If there is...
If/While Syntax
Last Updated: May 4, 2025Perl’s control syntax looks like C’s control syntax . Blocks of statements are surrounded by curly braces { }. Statements are terminated with semicolons (;). The parenthesis and curly braces are required in if/while/for forms. There is not a distinct “boolean” type, and there are no “true” or “false” keywords in the language. Instead, the...
File Input
Last Updated: May 4, 2025Variables which represent files are called “file handles”, and they are handled differently from other variables. They do not begin with any special character — they are just plain words. By convention, file handle variables are written in all upper case, like FILE_OUT or SOCK. The file handles are all in a global namespace, so...
Print Output
Last Updated: May 4, 2025Print takes a series of things to print separated by commas. By default, print writes to the STDOUT file handle. print “Woo Hoo\n”; ## print a string to STDOUT$num = 42;$str = ” Hoo”;print “Woo”, $a, ” bbb $num”, “\n”; ...
String Processing with Regular Expressions
Last Updated: May 4, 2025Perl’s most famous strength is in string manipulation with regular expressions. Perl has a million string processing features — we’ll just cover the main ones here. The simple syntax to search for a pattern in a string is…($string =~ /pattern/) ## true if the pattern is found somewhere in the string(“binky” =~ /ink/) ==> TRUE(“binky” =~...
Subroutines
Last Updated: May 4, 2025Perl subroutines encapsulate blocks of code in the usual way. You do not need to define subroutines before they are used, so Perl programs generally have their “main” code first, and their subroutines laid out toward the bottom of the file. A subroutine can return a scalar or an array. $x = Three(); ## call...
Running External Programs
Last Updated: May 4, 2025Perl can be used to invoke other programs and mess with their input and output. The most straightforward way to do this is with the system function which takes a command line string (or an array of strings), and has the operating system try to run it (this makes the most sense in a Unix...
References
Last Updated: May 4, 2025I’m happiest writing Perl code that does not use references because they always give me a mild headache. Here’s the short version of how they work. The backslash operator (\) computes a reference to something. The reference is a scalar that points to the original thing. The ‘$’ dereferences to access the original thing. Suppose...
Terse Perl
Last Updated: May 4, 2025Perl supports a style of coding with very short phrases. For example, many built in functions use the special scalar variable $_ if no other variable is specified. So file reading code…while ($line = ) { print $line;}Can be written as…while () { print;}