1.35k likes | 1.36k Views
Learn all about Perl, a powerful scripting language perfect for text processing and system administration. Discover its syntax, variables, and how to run Perl scripts. With practical examples and tips for beginners.
E N D
What is Perl? • Practical Extraction and Report Language • A scripting language which is both relatively simple to learn and yet remarkably powerful.
Introduction to Perl Perl is often described as a cross between shell programming and the C programming language. Smalltalk (objects) C (numbers) Shell programming (text) C++ (numbers, objects) Perl (text, numbers) Java (objects)
Introduction to Perl • A “glue” language. Ideal for connecting things together, such as a GUI to a number cruncher, or a database to a web server. • Has replaced shell programming as the most popular programming language for text processing and Unix system administration. • Runs under all operating systems (including Windows). • Open source, many libraries available (e.g. database, internet) • Extremely popular for CGI and GUI programming.
Why use Perl ? • It is easy to gain a basic understanding of the language and start writing useful programs quickly. • There are a number of shortcuts which make programming ‘easier’. • Perl is popular and widely used, especially for system administration and WWW programming.
Why use Perl? • Perl is free and available on all computing platforms. • Unix/Linux, Windows, Macintosh, Palm OS • There are many freely available additions to Perl (‘Modules’). • Most importantly, Perl is designed to understand and manipulate text.
Where to find help! • http://www.perl.com • http://www.perl.org
#!/usr/bin/perl #This script prints a friendly greeting to the screen print “Hello World\n”; Your first Perl script • Scripts are first “compiled” and then “executed” in the order in which the lines of code appear • You can write a script with any text editor. The only rule is that it must be saved as plain text.
Running Perl Scripts • Perl 5 is installed on our CS system. • Run from the command line: palazzi% which perl /usr/bin/perl palazzi$ perl hello.pl Hello world! • You can run the script directly if you make the script executable, and the first line uses ‘hash-bang’ notation: palazzi% chmod +x hello.pl palazzi% hello.pl #!/usr/bin/perl -w print "Hello world!\n";
Basic Syntax • The -w option tells Perl to produce extra warning messages about potential dangers. Always use this option- there is never (ok, rarely) a good reason not to. #!/usr/bin/perl -w • White space doesn't matter in Perl (like C++), except for #!/usr/bin/perl -wwhich must start from column 1 on line 1.
Basic Syntax • 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 multi-line comments: /* */
Perl Example • Back to our “Hello World” program: palazzi% hello.pl #!/usr/bin/perl -w # This is a simple Hello World! Program. print "Hello world!\n"; • The print command sends the string to the screen, and “\n“ adds a new line. • You can optionally add parentheses: print(Hello world!\n);
First Script Line by Line # This script prints a friendly greeting to the screen • This is a Perl ‘comment’. Anything you type after a pound sign (#) is not interpreted by the compiler. These are notes to yourself or a future reader. Comments start at the ‘#’ and end at a carriage return • #!/usr/bin/perl is NOT a comment (note this exception)
First Script Line by Line print “Hello World!\n”; • This is a Perl ‘statement’, or line of code • ‘print’ is a function - one of many • “Hello World!\n” is a string of characters • note the ‘\n’ is read as a single character meaning ‘newline’ • The semicolon ‘;’ tells the interpreter that this line of code is complete.
Many ways to do it! # welcome.pl print ( "1. Welcome to Perl!\n" ); print "2. Welcome to Perl!\n" ; print "3. Welcome ", "to ", "Perl!\n"; print "4. Welcome "; print "to Perl!\n"; print "5. Welcome to Perl!\n"; print "6. Welcome\n to\n\n Perl!\n"; 1. Welcome to Perl! 2. Welcome to Perl! 3. Welcome to Perl! 4. Welcome to Perl! 5. Welcome to Perl! 6. Welcome to Perl!
System Calls • You can use Perl to execute shell commands, just as if you were typing them on the command line. • Syntax: • `command` # note that ` is the ‘backtick’ character, not the single quote ‘
A script which uses a system call #!/usr/bin/perl $directory_listing = `ls -l .`; print $directory_listing; • Note we are now using a ‘variable’ to hold the results of our system call
What is a variable? • A named container for a single value • can be text or number • sometimes called a ‘scalar’ • A scalar variable has the following rules • Must start with a dollar sign ($) • Must not start with a number • Must not contain any spaces • May contain ‘a’ through ‘Z’, any number character, or the ‘_’ character
Basic Types • Scalars, Lists and Hashes: • $cents=123; • @home=(“kitchen”, ”living room”, “bedroom”); • %days=( “Monday”=>”Mon”, “Tuesday”=>”Tues”); • All variable names are case sensitive.
Scalars • Denoted by ‘$’. Examples: • $cents=2; • $pi=3.141; • $chicken=“road”; • $name=`whoami`; • $foo=$bar; • $msg=“My name is $name”; • In most cases, Perl determines the type (numeric vs. string) on its own, and will convert automatically, depending on context. (eg, printing vs. multiplying)
Scalar variable names • These are valid names • $variable • $this_is_a_place_for_my_stuff • $Xvf34_B • These are invalid names • $2 • $another place for my stuff • $push-pull • $%percent
Variable name tips • Use descriptive names • $sequence is much more informative than $x • $sequence1 is ok. $sequence_one is fine too • Avoid using names that look like functions • $print is probably bad (it will work!) • Try to avoid single letter variable names • $a and $b are used for something else • Experienced programmers will often use $i and $j as ‘counters’ for historical reasons.
Operators . acts on strings only, ! on both strings and numbers, the rest on numbers only.
A Perl calculator #!/usr/bin/perl $value_one = shift; #Takes the first argument from the command line $value_two = shift; #Takes the next argument from the command line $sum = $value_one + $value_two; $difference = $value_one - $value_two; $product = $value_one * $value_two; $ratio = $value_one / $value_two; $power = $value_one ** $value_two; print "The sum is: $sum\n"; print "The difference is: $difference\n"; print "The product is: $product\n"; print "The ratio is: $ratio\n"; print "The first number raised to the power of the second number is: $power\n"; print ("I could have also written the sum as:", $value_one + $value_two, "\n”);
Quoting • When printing, use escapes (backslash) to print special characters: • print “She said \”Nortel cost \$$cost \@ $time\”.” • Output: She said “Nortel cost $0.01 @ 10:00”. • Special chars: $,@,%,&,” • Use single quotes to avoid interpolation: • print ‘My email is bhecker@acm.org. Please send me $’; • (Now you need to escape single quotes.) • Another quoting mechanism: qq() and q() • print qq(She said “Nortel cost \$$cost \@ $time”.); • print q(My email is bhecker@acm.org. Please send me $); • Useful for strings full of quotes.
Backquotes: Command Substitution • You can use command substitution in Perl like in shell scripts: $ whoami bhecker #!/usr/bin/perl -w $user = `whoami`; chomp($user); $num = `who | wc -l`; chomp($num); print "Hi $user! There are $num users logged on.\n"; $ test.pl Hi bhecker! There are 6 users logged on. • Command substitution will usually include a new line, so use chomp().
Backquote Example #!/usr/local/bin/perl -w $dir = `pwd`; chomp($dir); $big = `ls -l | sort +4 | tail -1 | cut -c55-70`; chomp($big); $nline = `wc -l $big | cut -c6-8`; # NOTE: Backquotes # interpolate. 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.
Quotes and more Quotes - Recap • There is a fine distinction between double quoted strings and single quoted strings: • print “$variable\n” # prints the contents of $variable and then a newline • print ‘$variable\n’ # prints the string $variable\n to the screen • Single quotes treat all characters as literal (no characters are special) • You can always specify a character to be treated literally in a double quoted string: • print “I really want to print a \$ character\n”;
Even more options • the qq operator • print qq[She said “Hi there, $stranger”.\n] ; #same as • print “She said \”Hi there, $stranger\”.\n” ; • qq means change the character used to denote the string • Almost any non-letter character can be used, best to pick one not in your string • print qq$I can print this string\n$; • print qq^Or I can print this string\n^; • print qq &Or this one\n&; • perl thinks that if you use a ‘(‘, ‘[‘, or ‘{‘ to open the string, you mean to use a ‘)’, ‘]’, or ‘}’ to close it
What is Truth? • A question debated by man since before cave art. • A very defined thing in PERL. • Something is FALSE if: • a) it evaluates to zero • b) it evaluates to ‘’ (empty string) • c) it evaluates to an empty list (@array = “”) • d) the value is undefined (ie. uninitialized variable) • Everything else is TRUE
Numeric Comparison Operators • Do not confuse ‘=‘ with ‘==‘ !!!! • <=> is really only useful when using the ‘sort’ function
String (Text) Comparison Operators • cmp is really only useful when using the ‘sort’ function
What did you mean? • To make your life ‘easier’, Perl has only one data type for both strings (characters) and numbers. • When you use something in numeric context, Perl treats it like a number. • $y = ‘2.0’ + ‘1’; # $y contains ‘3’ • $y = ‘cat’ + 1; # $y contains ‘1’ • When you use something in string context, perl treats it like a string. • $y = ‘2.0’ . ‘1’; # $y contains ‘2.01’ • In short, be careful what you ask for!!
More Truth • Statements can also be TRUE or FALSE, and this is generally logical • a) 1 == 2 - false • b) 1 !=2 - true • c) ‘dog’ eq ‘cat’ - false • d) (1+56) <= (2 * 100) – true • e) (1-1) – false! - evaluates to zero • f) ‘0.0’ - true! Tricky. • g) ‘0.0’ + 0 - false! Even trickier.
Functions • Functions are little bundles of Perl code with names. They exist to make it easy to do routine operations • Most functions do what you think they do, to find out how they work type: • perldoc -f function_name
A Perl Idiom - if • if is a function which does something if a condition is true. • print “Number is 2” if ($number == 2); • Of course, there is also a function that does the opposite - unless • print “Number isn’t 2” unless ($number == 2); • You don’t ever need to use unless, unless you want to... • print “Number isn’t 2” if ($number != 2);
More about if • A frequent Perl construction is the if/elsif/else construct • if (something){ do something } • elsif (something else) { do something } • else { do the default thing } • The block of code associated with the first true condition is executed. • Note: elsif, not elseif
Control flow if ($foo==10) { print “foo is ten\n”; } print “foo is ten” if ($foo==10); if ($today eq “Tuesday”) { print “Class at four.\n”; } elsif ($today eq “Friday”) { print “See you at the bar.\n”; } else { print “What’s on TV?\n”; }
Control flow You’ve already seen a while loop. for loops are just like C: for ($i=0; $i<10; $i++) { print “i is $I\n”; }
A brief Diversion • Get into the habit of using the -w flag • mnemonic (Warn me when weird) • Enables more strict error checking • Will warn you when you try to compare strings numerically, for example. • Usage • command line: ‘perl -w script.pl’ • even more diversion: ‘perl -c script.pl’ compiles but does not run script.pl • Or line: #!/usr/bin/perl -w
Input Data STDIN Any program STDOUT STDERR Output Data Concepts to know
Data flow • Unless you say otherwise: • Data comes in through STDIN (Standard IN) • Data goes out through STDOUT (Standard Out) • Errors go to STDERR (Standard Error) • Error code contained in a ‘magic’ variable $!
User Input • Use <STDIN> to get input from the user: #!/usr/bin/perl -w print "Enter name: "; $name = <STDIN>; chomp ($name); print "How many pens do you have? "; $number = <STDIN>; chomp($number); print "$name has $number pen!\n"; $ test.pl Enter name: Barbara Hecker How many pens do you have? one Barbara Hecker has one pen.
User Input • <STDIN> grabs one line of input, including the new line character. So, after: $name = <STDIN>; if the user typed “Barbara Hecker[ENTER]”, $name will contain: “Barbara Hecker\n”. • To delete the new line, the chomp() function takes a scalar variable, and removes the trailing new line if present. • A shortcut to do both operations in one line is: chomp($name = <STDIN>);
Numerical Example #!/usr/bin/perl -w print "Enter height of rectangle: "; $height = <STDIN>; print "Enter width of rectangle: "; $width = <STDIN>; $area = $height * $width; print "The area of the rectangle is $area\n"; $ test.pl Enter height of rectangle: 10 Enter width of rectangle: 5 The area of the rectangle is 50 $ test.pl Enter height of rectangle: 10.1 Enter width of rectangle: 5.1 The area of the rectangle is 51.51
An idiom - while • while a condition is true, do a block of statements • If you really want to know... The opposite of while is until • The most common use of while is for reading and acting on lines of data from a file
Usage of while #while_count.pl while ($val < 5){ print “$val\n”; $val++; } • while the condition is true ($val is less than 5), do something (print $val) • ‘++’? Same at C/C++