Tutorial of Perl
2006/3/21 Yu-Shih Su
NTHU-CS VLSI/CAD LAB
1
TH
EDA
Outline
Introduction The start of Perl Perl overview Regular expression Example: Automatic batch execution
NTHU-CS VLSI/CAD LAB
2
TH
EDA
Introduction
Perl
- Practical Extract & Report Language - Created by Larry Wall
Fill in the gap between the high level language and the low level language
High Level Language
(Ex: Shell)
Perl
Low Level Language
(Ex: C, C++)
NTHU-CS VLSI/CAD LAB
3
TH
EDA
What can Perl do?
Suitable for a quick-and-dirty program
- Ex: File parser - 30%~70% of the size of the equivalent C program
Powerful in process of strings
- Pattern matching and regular expression
TH EDA
NTHU-CS VLSI/CAD LAB
4
What can Perl do? (conti.)
Automatic batch execution
Designs b9.v C432.v alu2.v …… Testbenchs Automatic execution >verilog b9.v b9_t.v > b9.rlt
Perl
>verilog C432.v C432_t.v > C432.rlt >verilog alu2.v alu2_t.v > alu2.rlt
b9_t.v
C432_t.v alu2_t.v ……
NTHU-CS VLSI/CAD LAB
5
TH
EDA
What can Perl do? (conti.)
Design (.v)
module alu2(a, b, c…) input a; input b; ….. endmodule
Testbench (.v)
module top_tb;
Perl
alu2 a1(input[0], input[1],…); ……… #1.1115; clk = 1; input = `INPUT_NUM'bx; #1.1115; clk = 0; input = inputVector; ……… endmodule
Timing report by Synopsys (.timing)
Point Incr Path --------------------------------------------------input external delay 0.0000 0.0000 f a (in) 0.0000 0.0000 f vlg_inst1/Y (NAND2XL) 0.0478 0.0478 r …………………………… ……… ……… c (out) 0.0000 0.1115 r data arrival time 0.1115
NTHU-CS VLSI/CAD LAB
6
TH
EDA
The start of Perl
A simple program: “Hello world!”
#! /usr/bin/perl print “Hello World”;
int main(int argc, char* argv[]) { printf(“ Hello World”); return 0; }
NTHU-CS VLSI/CAD LAB
7
TH
EDA
Execute the program in Perl
Environment:
- CAD37 and any ICx workstations
The steps of execution: > chmod a+x HolloWorld.pl > HolloWorld.pl
NTHU-CS VLSI/CAD LAB
8
TH
EDA
Outline
Introduction The start of Perl Perl overview Regular expression Example: Automatic batch execution
NTHU-CS VLSI/CAD LAB
9
TH
EDA
Perl overview
Similar to C
Basic overview:
- Variables - Useful data structures
• Array • Hash
- Control structures
NTHU-CS VLSI/CAD LAB
10
TH
EDA
Variables
Can store string and value $number; (in Perl)
- int number; - float number; - char number[50];
strcpy (number, “Alex”); (in C)
- $number = “Alex”; (in Perl)
Don’t need to declare variables before use them
NTHU-CS VLSI/CAD LAB
11
TH
EDA
String or numeric?
Identified by operator
- Numeric operators: < + >, < - >, < * >, < / > - String operators: < . >, < x >, < eq >
• “hello” . “world” = “helloworld” • “hello” x 3 = “hellohellohello” • “hello” eq “hello” => true
Ex:
- $numA = “10”, $numB = “12” $numA + $numB = 22 $numA . $numB = “1022”
TH EDA
NTHU-CS VLSI/CAD LAB
12
List & Array
List: the union of data
- Ex: (11, “hello”, 12)
myArray 11 hello 12 0 1 2
Array: the variable where stores a list
- Ex: $myArray[0] == 11 $myArray[1] == “hello” $myArray[2] == 12 @myArray == ( 11, “hello”, 12)
NTHU-CS VLSI/CAD LAB
13
TH
EDA
Context
Scalar context:
- $numA == 1, $numB == 2 $numA + $numB = 3 > Scalar context
List context:
- @people == (11, “hello”, 12) @abc = @people > List context print @abc; # 11 hello 12
TH EDA
NTHU-CS VLSI/CAD LAB
14
Hash
$hash_variable{$some_key}
- Ex: $myHash{“foo”} = 35; $myHash{“25.4”} = “hello”; - print $myHash{“foo”} # 35
foo 25.4
myHash
11 hello
%hash_var = (key, value, key, value,…)
- %myHash = (“foo”, 35, 25.4, “hello”)
NTHU-CS VLSI/CAD LAB
15
TH
EDA
Control structures
Foreach $var (@array){ }
- Ex: @array == ( 11, “hello”, 12) $var == 11, $var == “hello”, $var == 12
For( $n = x ; $n < $num; $n++) { }
While (something == true) {}
NTHU-CS VLSI/CAD LAB
16
TH
EDA
Outline
Introduction The start of Perl Perl overview Regular expression Example: Automatic batch execution
NTHU-CS VLSI/CAD LAB
17
TH
EDA
Regular expression
Pattern: < /pattern/ >
- Ex: /abba/ #pattern - “acca” doesn’t conform with pattern /abba/
Binding operator: < =~ >
- Ex: $string = “yabba dabba doo”; if ($string =~ /abba/){ print “Conform!”; }
NTHU-CS VLSI/CAD LAB
18
TH
EDA
Regular expression
Quantifier:
- * : zero or more, ex: (abc)* => “ “, “abc”, “abcabc”…. - + : at least one, ex: (abc)+ => “abc”, “abcabc”…. - ? : zero or one, ex: (abc)? => “ “, “abc”
Character class:
- \d : any number [0-9].
• Ex: 3129, 1234
- \w: any word [A-Za-z0-9_].
• Ex: tom, john, tom123
- \s: any space and tab.
NTHU-CS VLSI/CAD LAB
19
TH
EDA
Example
$string = “module alu2(a, b, c);” if( $string =~ /module(\s+)(\w+)\( /) { print $1; print $2; }
Result:
- $1 == “ ”, $2 == “alu2”
NTHU-CS VLSI/CAD LAB
20
TH
EDA
More regular expression
Replace: s/taget pattern/replacer/
- Ex: $name = “alu2.v”; $name =~ s/\.v/\.rst/; # $name == alu2.rst $name =~ s/\.rst//; # $name == alu2
NTHU-CS VLSI/CAD LAB
21
TH
EDA
Outline
Introduction The start of Perl Perl overview Regular expression Example: Automatic batch execution
NTHU-CS VLSI/CAD LAB
22
TH
EDA
Example: Automatic batch execution
#! /usr/bin/perl while(defined($file = )){ $name = $file; $name =~ s/\.v//; chomp($name);
b9.v C432.v b9 C432
alu2.v
……
alu2
……
$command_line = "vcs -R -P $PLI_FILE_TAB $PLI_FILE_C $COMB_PATH/$file ${name}_tb.v $TSMC +acc+3"; $command_line = $command_line . " > $SIM_TIME/${name}.simt"; print $command_line . "\n"; print "$file: Simulation Begin\n"; system $command_line; print "$file: Simulation End\n"; }
NTHU-CS VLSI/CAD LAB
VCS … b9.v b9_tb.v … > b9.simt VCS … C432.v C432_tb.v … > C432.simt VCS … alu2.v alu2_tb.v … > alu2.simt
23
TH
EDA
Reference
Learning Perl (Perl 學習手冊)
Programming Perl (Perl 程式設計)
NTHU-CS VLSI/CAD LAB
24
TH
EDA