Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Perl Learning Objectives: 1. To introduce the features provided by Perl 2. To learn the basic Syntax & simple Input/Output control in Perl.

Similar presentations


Presentation on theme: "Introduction to Perl Learning Objectives: 1. To introduce the features provided by Perl 2. To learn the basic Syntax & simple Input/Output control in Perl."— Presentation transcript:

1 Introduction to Perl Learning Objectives: 1. To introduce the features provided by Perl 2. To learn the basic Syntax & simple Input/Output control in Perl 3. To learn how to combine Unix Command in Perl using backquotes

2 COMP111 Lecture 9 / Slide 2 Introduction to Perl Table of Content  Introduction to Perl  Basic Syntax  Perl Example 1  Scalar Variables  Assigning Scalar Variables  Numerical Scalar Variable  User Input  Printing Special Symbol - $  Numerical Example  Backquotes: Command Substitution  Backquote Example 2

3 COMP111 Lecture 9 / Slide 3 Introduction to Perl  Perl is a scripting language that makes manipulation of text, files, and processes easy.  Perl is a cross between shell programming and the C programming language. C (numbers) Shell programming (text) Smalltalk (objects) C++ (numbers, objects) Perl (text, numbers) Java (objects)

4 COMP111 Lecture 9 / Slide 4 Introduction to Perl  Perl provides a more concise and readable way to do many tasks compared to C++ and shell scripts.  Perl has replaced shell programming as the most popular programming language for text processing and Unix system administration.  Perl was originally designed under Unix, but now also runs under all operating systems (including Windows).  Many libraries available (e.g. database, internet)  Perl is also a popular language for CGI (for web server) and GUI programming.

5 COMP111 Lecture 9 / Slide 5 Introduction to Perl  The perl command on our system invokes Perl 4.0, but we want to use the latest version Perl 5.0.  You can run a perl 5.0 script explicitly with perl5: $ cat simple $ perl5 simple $  You can run the script directly if you make the script executable, and the first line is of the following form ( #!/usr/... must start from the first column): $ chmod +x simple $ cat simple #!/usr/local/bin/perl5 -w $ simple $

6 COMP111 Lecture 9 / Slide 6 Basic Syntax (1)  The -w option tells Perl to produce extra warning messages about potential dangers. Use -w in all your Perl programs for now. #!/usr/local/bin/perl5 -w  Whitespace doesn't matter in Perl (like C++), except for #!/usr/local/bin/perl5 -w which must start from column 1 on line 1.

7 COMP111 Lecture 9 / Slide 7 Basic Syntax (2)  All perl statements end in a semicolon ; (like C++)  In Perl, comments begin with # (like shell scripts)  everything after the # to the end of the line is ignored.  # need not be at the beginning of the line.  there are no C++-like multiline comments: /* */

8 COMP111 Lecture 9 / Slide 8 Perl Example 1 (1)  Here is a “ hello world ” Perl program: $ ls -l -rwxr-xr-x 1 horner cs 52 Mar 2 15:50 hello* $ cat hello #!/usr/local/bin/perl5 -w # comment lines start with the # character print "Hello world\n"; $ hello Hello world $  The print command sends the string to the screen, and “ \n “ adds a newline.

9 COMP111 Lecture 9 / Slide 9 Perl Example 1 (2)  You can optionally use parenthesis around the argument in print : print ("Hello world\n"); or, if your prefer the C++ function style: print("Hello world\n");

10 COMP111 Lecture 9 / Slide 10 Scalar Variables  A scalar variable can hold a single number or string (like shell variables), including integers and floating- point numbers (unlike shell variables).  Scalar variables begin with “ $ ” followed by a letter, and then possibly more letters, digits, or underscores. (e.g., $n, $n1, $name, $first_name ).  Scalar variables are case sensitive.

11 COMP111 Lecture 9 / Slide 11 Assigning Scalar Variables  Scalars are assigned using “ = “ $scalar = expression;  To assign a value to a scalar variable: $number = 25; $name = "Bill Clinton";  Unlike shell scripts, use the “ $ ” both when the variable is used and assigned: $ cat test1 #!/usr/local/bin/perl5 -w $number = 25; $name = "Bill Clinton"; print "$number $name\n"; $ test1 25 Bill Clinton $

12 COMP111 Lecture 9 / Slide 12 Numerical Scalar Variables  Internally, all numerical scalar values are stored as floats (so you don ’ t have to worry about integer division in Perl like you do in C++).  Perl supports the usual C++ numerical operations: $a = 25;# $a is now 25 $a += 5; # $a is now 30 $a *= 3; # $a is now 90 $a++; # $a is now 91 --$a; # $a is now 90 $result = ($a + 2) * 3.4; # $result is 312.8

13 COMP111 Lecture 9 / Slide 13 User Input (1)  Use to get input from the user: $ cat test2 #!/usr/local/bin/perl5 -w print "Enter name: "; $name = ; chomp ($name); print "How many girlfriends do you have? "; $number = ; chomp($number); print "$name has $number girlfriends!\n"; $ test2 Enter name: Bill Clinton How many girlfriends do you have? more than you Bill Clinton has more than you girlfriends!

14 COMP111 Lecture 9 / Slide 14 User Input (2)  grabs one line of input, including the newline character. So, after: $name = ; if the user typed “ Bill Clinton[ENTER] ”, $name will contain: “ Bill Clinton\n ”.  To delete the newline, the chomp() function takes a scalar variable, and removes the trailing newline if present. (If there is no newline at the end, it does nothing.)  A shortcut to do both operations in one line is: chomp($name = );

15 COMP111 Lecture 9 / Slide 15 Printing Special Symbol - $  As with the shell scripts, use a backslash before $ if you really want to print the dollar sign: $ cat test4 #!/usr/local/bin/perl5 -w print "Enter amount: "; $cost = ; print "The total is: \$$cost"; $ test4 Enter amount: 18.50 The total is $18.50  No need to use chomp() if the newline on $cost can be used when it is printed.

16 COMP111 Lecture 9 / Slide 16 Numerical Example $ cat test6 #!/usr/local/bin/perl5 -w print "Enter height of rectangle: "; $height = ; print "Enter width of rectangle: "; $width = ; $area = $height * $width; print "The area of the rectangle is $area\n"; $ test6 Enter height of rectangle: 10 Enter width of rectangle: 5 The area of the rectangle is 50 $ test6 Enter height of rectangle: 10.1 Enter width of rectangle: 5.1 The area of the rectangle is 51.51

17 COMP111 Lecture 9 / Slide 17 Backquotes: Command Substitution  You can use command substitution in Perl like in shell scripts: $ whoami clinton $ cat test7 #!/usr/local/bin/perl5 -w $user = `whoami`; chomp($user); $num = `who | wc -l`; chomp($num); print "Hi $user! There are $num users logged on.\n"; $ test7 Hi clinton! There are 6 users logged on.  Command substitution will usually include a newline, so use chomp().

18 COMP111 Lecture 9 / Slide 18 Backquote Example 2 $ cat big1 #!/usr/local/bin/perl5 -w $dir = `pwd`; chomp($dir); $big = `ls -l | sort +4 | tail -1 | cut -c55-70`; chomp($big); $nline = `wc -l $big | cut -c6-8`; chomp($nline); $nword = `wc -w $big | cut -c6-8 `; chomp($nword); $nchar = `wc -c $big | cut -c6-8 `; chomp($nchar); print "The biggest file in $dir is $big.\n"; print "$big has $nline lines, $nword words, $nchar characters.\n " ; $ big1 The biggest file in /homes/horner/111/perl is big1. big1 has 14 lines, 66 words, 381 characters.


Download ppt "Introduction to Perl Learning Objectives: 1. To introduce the features provided by Perl 2. To learn the basic Syntax & simple Input/Output control in Perl."

Similar presentations


Ads by Google