Perl, A Hardware Language

Embed Size (px)

Citation preview

  • 8/2/2019 Perl, A Hardware Language

    1/53

    PerlA Scripting language

    Larry Wall

  • 8/2/2019 Perl, A Hardware Language

    2/53

    ProgrammingWhat does an Engineer need?

    Occasionally, an Engineer will have to write aprogram!

    What types of problems might require

    programming?

    Generate complex data streams as input to another

    problem

    Parse/collect information out of large data files

    Write a program that runs other programs in a

    regression test

  • 8/2/2019 Perl, A Hardware Language

    3/53

    Many programs are throw away code use once to

    complete a task, then forget about it.

    Need to become very familiar with a favorite

    programming language, and use it enough to

    become time efficient.

    Convert data files from one format to another

  • 8/2/2019 Perl, A Hardware Language

    4/53

    Desirable Features of a Programming

    Language for throw-away code

    Powerful get a lot done with a little code

    Flexible be able to do many different types of

    tasks

    GUIs, string processing, program control,number crunching , etc.

    Well documented

    Large user base so external libraries, examples

    readily available

  • 8/2/2019 Perl, A Hardware Language

    5/53

    Portable be able to run on different systems

    Readily available (free is the best!)

  • 8/2/2019 Perl, A Hardware Language

    6/53

    C, C++, Fortran are traditional compiled languages

    Pros

    High performance code

    Portable between systems

    Free C, C++, Fortran compilers from the FreeSoftware Foundation

  • 8/2/2019 Perl, A Hardware Language

    7/53

    Cons

    Usually have to write a lot of code to get even simple

    tasks done

    Must compile source code first on target system

    before execution.

    GUI interfaces are Operating System dependent

    Support for scripting (i.e, controlled execution of

    other programs) is minimal and Operating Systemdependent

    Best for large, complex tasks -- but may not be best

    choice for simple tasks (i.e, < 100 lines of code)

  • 8/2/2019 Perl, A Hardware Language

    8/53

    UNIX Shell Scripting Languages

    All UNIX shells (csh, bash, ksh, etc) support a

    scripting language

    Pros

    Built in to shell, always available for useIdeal for scripting duties (control of other programs)

    Cons

    Fairly primitive features, no powerful operators or

    data handling featuresVery slowat least 100x slower than compiled code

    No GUI capabilities

    Only useful for Unix applications

  • 8/2/2019 Perl, A Hardware Language

    9/53

    Java Portable object-oriented programming language

    ProsPowerful data structures, functions

    Portable between Unix/Windows

    Free development environment

    Powerful GUI buildingUseful for Web page enhancement

    Decent performance (about 10x less than compiled

    C/C++)

    ConsLimited scripting, string processing

    Object-oriented programming model is possible

    overkill for simple throw-away programs

  • 8/2/2019 Perl, A Hardware Language

    10/53

    Now coming to Perl

  • 8/2/2019 Perl, A Hardware Language

    11/53

    OUTLINE

    Introduction

    The start of Perl

    Perl overview

    Regular expression

    Example: Automatic batch execution

  • 8/2/2019 Perl, A Hardware Language

    12/53

    INTRODUCTION

    Perl Practical Extract & Report Language

    Created by Larry Wall

    Fill in the gap between the high level languageand the low level language

    High Level Language

    (Ex: Shell)Low Level Language

    (Ex: C, C++)Perl

  • 8/2/2019 Perl, A Hardware Language

    13/53

    WHATCAN PERLDO?

    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

  • 8/2/2019 Perl, A Hardware Language

    14/53

    WHATCAN PERLDO? (CONTI.)

    b9.vC432.valu2.v

    b9_t.vC432_t.valu2_t.v

    Perl

    >verilog b9.v b9_t.v > b9.rlt>verilog C432.v C432_t.v > C432.rlt>verilog alu2.v alu2_t.v > alu2.rlt

    Automatic batch execution

    Designs

    Testbenchs

    Automatic execution

  • 8/2/2019 Perl, A Hardware Language

    15/53

    15

    WHATCAN PERLDO? (CONTI.)

    module alu2(a, b, c)input a;input b;..endmodule

    Point Incr Path---------------------------------------------------input external delay 0.0000 0.0000

    fa (in) 0.0000 0.0000 fvlg_inst1/Y (NAND2XL) 0.0478

    0.0478 r

    c (out) 0.0000 0.1115 rdata arrival time 0.1115

    module top_tb;

    alu2 a1(input[0], input[1],);

    #1.1115; clk = 1; input =`INPUT_NUM'bx;

    #1.1115; clk = 0; input =inputVector;

    endmodule

    Perl

    Design (.v)

    Timing report by Synopsys (.timing)

    Testbench (.v)

  • 8/2/2019 Perl, A Hardware Language

    16/53

    THESTARTOF PERL

    #! /usr/bin/perl

    print Hello World;

    int main(int argc, char* argv[]){

    printf( Hello World);

    return 0;

    }

    A simple program: Hello world!

  • 8/2/2019 Perl, A Hardware Language

    17/53

    PERLOVERVIEW

    Similar to C

    Basic overview:

    Variables Useful data structures

    Array

    Hash Control structures

  • 8/2/2019 Perl, A Hardware Language

    18/53

    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)

    Dont need to declare variables before use them

  • 8/2/2019 Perl, A Hardware Language

    19/53

    STRINGORNUMERIC ?

    Identified by operator Numeric operators: < + >, < - >, < * >, < />

    String operators: < . >, < x >, < eq >

    hello . world =

    helloworld

    hello x 3 = hellohellohello

    helloeqhello => true

    Ex:

    $numA = 10, $numB = 12$numA + $numB = 22

    $numA . $numB = 1022

  • 8/2/2019 Perl, A Hardware Language

    20/53

    LIST & ARRAY

    List: the union of data Ex: (11, hello, 12)

    Array: the variable where stores a list

    Ex: $myArray[0] == 11

    $myArray[1] == hello$myArray[2] == 12

    @myArray == ( 11, hello, 12)

    myArray

    11

    hello

    12

    0

    1

    2

  • 8/2/2019 Perl, A Hardware Language

    21/53

    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

  • 8/2/2019 Perl, A Hardware Language

    22/53

    HASH

    $hash_variable{$some_key} Ex: $myHash{foo} = 11;

    $myHash{25.4} = hello;

    print $myHash{foo} # 11

    %hash_var = (key, value, key, value,)

    %myHash = (foo, 11, 25.4, hello)

    myHash

    11

    hello

    foo

    25.4

  • 8/2/2019 Perl, A Hardware Language

    23/53

    Operators

    Perl uses all the usual C arithmetic operators:$a = 1 + 2; # Add 1 and 2 and store in $a$a = 3 - 4; # Subtract 4 from 3 and store in $a$a = 5 * 6; # Multiply 5 and 6

    $a = 7 / 8; # Divide 7 by 8 to give 0.875$a = 9 ** 10; # Nine to the power of 10$a = 5 % 2; # Remainder of 5 divided by 2++$a; # Increment $a and then return it

    $a++; # Return $a and then increment it--$a; # Decrement $a and then return it$a--; #Return $a and then decrement it

  • 8/2/2019 Perl, A Hardware Language

    24/53

    and for strings Perl has the following among

    others:$a = $b . $c; # Concatenate $b and $c$a = $b x $c; # $b repeated $c times

  • 8/2/2019 Perl, A Hardware Language

    25/53

    Assigning Perl Values

    To assign values Perl includes

    $a = $b; # Assign $b to $a$a += $b; # Add $b to $a$a -= $b; # Subtract $b from $a$a .= $b; # Append $b onto $a ($a, $b

    contain strings)

    Note that when Perl assigns a value with$a = $b it makes a copy of $b and then assigns thatto $a. Therefore the next time you change $b it willnot alter $a.

  • 8/2/2019 Perl, A Hardware Language

    26/53

    Interpolation of Variable Names

    The following code prints apples and pears using

    concatenation (note the use ofthe . (period) as thestring concatenation operator).

    $a = 'apples';$b = 'pears';print $a . ' and ' . $b;

    It would be nicer to include only one string in thefinal print statement, but the line

    print '$a and $b';

    prints literally $a and $b which isn't very helpful. Insteadwe can use double quotesin place of the single quotes:

  • 8/2/2019 Perl, A Hardware Language

    27/53

    print "$a and $b";

    The double quotes force interpolation of any codes,

    including interpretingvariables. Other codes that areinterpolated include special characters such asnewline and tab. The code\n is a newline and \t isa tab.

  • 8/2/2019 Perl, A Hardware Language

    28/53

    Array Variables

    A slightly more interesting kind of variable is thearray variable which is a listof scalars (i.e numbersand strings). Array variables have the same format asscalar variables except that they are prefixed by an @

    symbol. The statement

    @food = ("apples", "pears", "eels");@music = ("whistle", "flute");

    assigns a three element list to the array variable @foodand a two element list to the array variable @music.

  • 8/2/2019 Perl, A Hardware Language

    29/53

    The array is accessed by using indices starting from 0,and square brackets are used to specify the index. The

    expression

    $food[2]

    returns eels. Notice that the@ has changed to a $because eels is a scalar.

  • 8/2/2019 Perl, A Hardware Language

    30/53

    Array Assignments

    As in all of Perl, the same expression in a differentcontext can produce a different result. The firstassignment below explodes the @music variable sothat it is equivalent to the second assignment.

    @moremusic = ("organ", @music, "harp");@moremusic = ("organ", "whistle", "flute", "harp");

    This should suggest a way of adding elements to anarray. A neater way of adding elements is to use thestatement

    push(@food, "eggs");

  • 8/2/2019 Perl, A Hardware Language

    31/53

    which pushes eggs onto the end of the array @food. Topush two or more itemsonto the array use one of thefollowing forms:

    push(@food, "eggs", "lard");

    push(@food, ("eggs", "lard"));push(@food, @morefood);

    The push function returns the length of the new list.

  • 8/2/2019 Perl, A Hardware Language

    32/53

    More List Manipulation

    @food = ("eels", "lard", "bread" );

    It is possible to assign an array to a scalar variable.

    Examples are:$f = @food; # $f = length of @food, here = 3.$g = $#food; # $g = last index of @food, here = 2

    The assignment:$f = @food;

    turns the value of $f into a string that are the members of@food with a space between each element.

  • 8/2/2019 Perl, A Hardware Language

    33/53

    The assignment below assigns $g the last item in the

    list @food:$g = $food[$#food]; # $g = $food[2] = bread;

    To remove the last item from a list and return it use thepop function. Note that after this assignment, $g =bread, and @food only has 2 elements left.

    $g = pop(@food);

  • 8/2/2019 Perl, A Hardware Language

    34/53

    File Handling

    Here is a simple perl program which does the same asthe UNIX cat command on a certain file.

    #!/usr/bin/perl#

    # Program to open the password file,read it in,# print it, and close it again.

    $file = '/etc/passwd'; # Name the fileopen(INFO, $file); # Open the file@lines = ; # Read it into an arrayclose(INFO); # Close the file

    print @lines; # Print the array

  • 8/2/2019 Perl, A Hardware Language

    35/53

    More File Handling

    The open statement can also specify a file for

    output and for appending as well as for input. Todo this, prefix the filenamewith a > for output and a >> for appending:

    open(INFO, $file); # Open for inputopen(INFO, ">$file"); # read onlyopen(INFO, ">>$file"); # appendingopen(INFO, "

  • 8/2/2019 Perl, A Hardware Language

    36/53

    To open the standard input (usually the keyboard) and

    standard output (usually the screen) respectively do:

    open(INFO, '-'); # Open standard input

    open(INFO, '>-'); # Open standard output

  • 8/2/2019 Perl, A Hardware Language

    37/53

    foreach Control Structure

    To go through each line of an array or other list-like

    structure (such as lines in a file) Perl uses theforeach structure. This has the form:foreach $morsel (@food) # Visit each item in

    turn# and call it $morsel

    {print "$morsel\n"; # Print the item

    print "Yum yum\n"; # That was nice}

  • 8/2/2019 Perl, A Hardware Language

    38/53

    Testing

    The next few structures rely on a test being true orfalse. In Perl any non-zero number and non-emptystring is counted as true. The number zero, zero byitself in a string, and the empty string are counted as

    false. Here are some tests on numbers and strings.

    a == $b # Is $a numerically equal to $b?

    # Beware: Don't use the = operator.$a != $b # Is $a numerically unequal to $b?$a eq $b # Is $a string-equal to $b?$a ne $b # Is $a string-unequal to $b?

  • 8/2/2019 Perl, A Hardware Language

    39/53

    You can also use logical and, or and not:

    ($a && $b) # Is $a and $b true?($a || $b) # Is either $a or $b true?!($a) # is $a false?

  • 8/2/2019 Perl, A Hardware Language

    40/53

    for Control Structure

    Perl has a for structure that mimics that of C. Ithas the formfor (initialise; test; inc){

    first_action;second_action;etc }

    First of all the statement initialise is executed. Thenwhile test is true the blockof actions is executed. Aftereach time the block is executed inc takes place. Here isan example for loop to print out the numbers 0 to 9.

  • 8/2/2019 Perl, A Hardware Language

    41/53

    for ($i = 0; $i < 10; $i++) # Start with $i = 0# Do it while $i < 10# Increment $i before

    repeating{

    print "$i\n";

    }

  • 8/2/2019 Perl, A Hardware Language

    42/53

    while

    Here is a program that reads some input from thekeyboard and won't continue until it gets the correctpassword

    #!/usr/bin/perl

    print "Password? "; # Ask for input$a = ; # Get inputchop $a; # Remove the newline at endwhile ($a ne "fred") # While input is wrong...

    {print "sorry. Again? "; # Ask again$a = ; # Get input againchop $a; # Chop off newline again

    }

  • 8/2/2019 Perl, A Hardware Language

    43/53

    do - untilHere is a version of the previous program that uses ado until control structure:#!/usr/local/bin/perldo{

    "Password? "; # Ask for input$a = ; # Get input chop$a; # Chop off newline}

    while ($a ne "fred") # Redo while wrong input

  • 8/2/2019 Perl, A Hardware Language

    44/53

    if, elsif statements

    Of course Perl also allows if/then/else statements.These are of the following form:

    if ($a) {

    print "The string is not empty\n";} else {

    print "The string is empty\n";}

  • 8/2/2019 Perl, A Hardware Language

    45/53

    Regular Expressions

    A regular expression is contained in slashes, andmatching occurs with the =~ operator. The followingexpression is true if the string the appears in variable$ sentence.

    $sentence =~ /the/

    The RE is case sensitive, so if $sentence = "Thequick brown fox"; then the above match will be

    false. The operator !~ is used for spotting anonmatch. In the above example

    $sentence !~ /the/

    is true because the string the does not appear in $sentence.

  • 8/2/2019 Perl, A Hardware Language

    46/53

    SubroutinesPerl allows the user to define their own functions,

    called subroutines. Theymay be placed anywhere inyour program but it's probably best to put them all atthe beginning or all at the end. A subroutine has theform

    sub mysubroutine {print "Not a very interesting routine\n";print "This does the same thing every time\n";}The above subroutine ignores any parameters passed to it. All of thefollowing will work to call this subroutine. Notice that a subroutine is calledwith an & character in front of the name:&mysubroutine; # Call the subroutine&mysubroutine($_); # Call it with a parameter&mysubroutine(1+2, $_); # Call it with two parameters

  • 8/2/2019 Perl, A Hardware Language

    47/53

    Passing Parameters to Subroutines

    When a subroutine is called any parameters are passed

    as a list in the special@_ list array variable. Thefollowing subroutine merely prints out the list that itwas called with. It is followed by a couple of examples ofits use.

    sub printargs {print "@_\n";

    }&printargs("perly", "king"); # Example prints

    "perly king"&printargs("frog", "and", "toad"); # Prints

    "frog and toad"

  • 8/2/2019 Perl, A Hardware Language

    48/53

    Pros

    Powerful data structures, functionsPortable between Unix/WindowsFree development environmentDecent performance (about 10x less than compiled

    C/C++)Many public libraries available for tasks such as

    HTML processing and data base accessExtremely large user basethe scripting language of

    choice under Unix

  • 8/2/2019 Perl, A Hardware Language

    49/53

    Cons

    No GUI building capabilitiesCode may be unreadable after you write it!

    Python

    Tcl / Tk

  • 8/2/2019 Perl, A Hardware Language

    50/53

    CONCLUSION:

    AS WE ARE USING THIS SCRIPTING LANGUAGE IN

    REAL TIME VLSI INDUSTRIES, WE HAVE TO BE

    EFFICENT IN THIS, OTHER THAN COMPILEING

    LANGUAGES

  • 8/2/2019 Perl, A Hardware Language

    51/53

    Perl Documentation

    Active Perl installation has best documentation

    C:\Perl\html\index.html

    General Perl sites http://www.perl.com , http://www.perl.org,

    http://www.activeperl.com

    Definitive textbook for PerlProgramming Perl3rd edition, L. Wall,

    T. Christiansen, R. SchwarzPublished by OReilly, $35 on Amazon.com

    Online Perl tutorials http://www.comp.leeds.ac.uk/Perl/start.html

    (all of these initial notes are copied from the above tutorial)

  • 8/2/2019 Perl, A Hardware Language

    52/53

  • 8/2/2019 Perl, A Hardware Language

    53/53