1 / 115

What the heck is Perl?

What the heck is Perl?. Perl a computer language designed to scan arbitrary text files, extract information from those text files, and print reports based on that information “Perl” == “Practical Extraction and Report Language” What makes Perl powerful?

dinaa
Télécharger la présentation

What the heck is Perl?

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. What the heck is Perl? • Perl a computer language designed to scan arbitrary text files, extract information from those text files, and print reports based on that information • “Perl” == “Practical Extraction and Report Language” • What makes Perl powerful? • It has sophisticated pattern matching capabilities • Straightforward I/O • It was created, written, developed, and maintained by Larry Wall (lwall@netlabs.com) Perl

  2. Where does Perl stand? • Perl is an interpreted language • Which means it runs slower than a compiled language • BUT it is much easier, and quicker, to develop programs • Some people would call Perl a scripting language • The language is intended to be practical (easy to use, efficient, complete) rather than beautiful (tiny, elegant, minimal) • It is a useful tool that can get the job done Perl

  3. Lots of People Are Using Perl • There are lots of people using Perl and as a result there are lots of libraries that you can get for free • If you can think of an application, chances are you can find the Perl code to do it • This means writing Perl programs to do sophisticated things is easy and does not take long to to. Perl

  4. Is Perl THE tool? • Probably not • Perl is great for munging text data to a different form • Search for updated software, download it, compile, and install • Perl is great if you want it done fast • What about more complicated programming? • You might want to get a bigger hammer!! • There are many Perl packages out there. Perl

  5. Comment Ignored by Interpreter Escape character - newline Print statement A String – a collection of characters Your First Perl Program • # Say Hello • print “Hello World\n”; Execution Order Perl

  6. Comment used by Unix to run Perl Perl - Unix Style • #!/usr/local/bin/perl -w • # Say Hello • print “Hello World\n”; Perl

  7. How To Make It Run Create a text file that contains a Perl program (script) Perl

  8. How To Make It Run Invoke the interpreter to run the program Perl

  9. Should be “print” Sometimes we make misteaks Create the Perl script Perl

  10. Sometimes we make misteaks Run the interpreter Perl

  11. Sometimes we make misteaks Fix the mistake Try again Perl

  12. Where Do You Get Perl? • Lots of good Perl resources on the web • www.perl.org • learn.perl.org • www.perl.com • Most will point you to a source for Perl • Can get source code and compile • Can get binaries Perl

  13. Documentation • Perl comes with documentation that covers just about everything • Normally accessed using perldoc • Active state also has web pages • To get documentation about something you type • perldoc xxxx • Where xxxx is the topic you are interested in • perldoc perl gives an overview of the documentation Perl

  14. Documentation Topics Perl

  15. Scalar Data • A scalar is the simplest kind of data that Perl manipulates • It is either a number or a string of characters • Perl treats numbers and strings nearly interchangeably • Scalar operators operate on scalar data and produce scalar results • Scalar values are stored in scalar variables Perl

  16. Numbers • Internally Perl treats all numbers as double-precision floating-point values • There are no integer values internal to Perl • use integer; will force integer arithmetic • Numeric literals • Exactly what you would expect • 1.25, 7.25e45, -6.5, 12, -2004, 3485 • 0377  Octal literal • 0xff  Hex literal • Lesson don’t start numbers with 0!! Perl

  17. Strings • Strings are sequences of characters • Single-quoted strings • Any character between the quotes (including newline characters) is legal • Only exceptions \’ and \\ • Note ‘\n’ is the string is not a newline!! • Double quoted strings • What you are used to Perl

  18. Double-quoted Escapes Perl

  19. Scalar Variables • Holds a single scalar value • Names begin with a $ • Followed by a letter, and then possibly more letters, or digits, or underscores. • Names are case sensitive • Names can be as long as you like • $i, $line_length, $dna, $xyz Perl

  20. Scalar Assignment • Takes a scalar variable on the left side, and gives it the value of the expression on the right • $a = 17; • $b = $a + 3; • $b = $b + 1; • $a = “Seventeen”; Perl

  21. Assignment is an Expression • Assignment is an expression that refers to the variable on the left hand side of the equals sign • $b = 4 + ( $a = 3 ); • $d = ( $c = 5 ); • $d = $c = 5; Perl

  22. Variable Interpolated • Double-quoted strings are variable interpolated • $a = 6; • $b = “The answer is: $a”; • $perl = “Perl”; • $java = “Java”; • $str = “$perl is different than $java”; Perl

  23. Numeric Operators Perl

  24. Truth Values • Perl does not have a boolean type • There are no boolean constants true and false • Boolean values can be represented by numbers or strings • For numbers 0 is false anything else is true • For strings the empty string or the string “0” is false anything else is true Perl

  25. String Operators Perl

  26. Not Strictly Typed • Perl is not strictly types and will convert numbers to strings when it thinks it is reasonable • $x = 1 . 2; • $x = “1” + “2”; • $x = “10” < “2”; • You have to be careful what you are doing Perl

  27. chop() • Takes a scalar variable and removes the last character • $x = “hello world”; • chop( $x); • chop() changes its argument • Returns the character that was chopped • $x = chop( $x ); • chop() simply returns if given an empty string • Perl generally does not complain Perl

  28. <STDIN> • <STDIN> is a scalar variable that can be used to read data from the keyboard • More correctly from standard input • Reads the next line – includes the newline • Example • $a = <STDIN>; • chop( $a ); • How about this • chop( $a = <STDIN> ); Perl

  29. undef • Variables have the value undef before they are first assigned • The value looks like zero, or an empty string • Some operators return undef when arguments are out of range • <STDIN> returns undef when there are no more lines to read (i.e., we have hit EOF) Perl

  30. Output • Print() can be used to display things on standard output • print “Hello World\n”; Perl

  31. Your Turn • Write a program that computes the circumference of a circle with a radius of 12.5. The circumference is 2 times the radius ( is about 3.141592 or 22/7) • Modify the program to prompt for and read the radius from the keyboard • Write a program that reads a string and a number, and prints the string the number of times indicated by the number on separate lines (you cannot use a loop). Perl

  32. circumference.perl $pi = 22/7; $result = 2*$pi*12.5; print “radius 12.5 is circumference $result\n”; Perl

  33. circumference1.perl print “Enter the radius: “; chop($radius=<STDIN>); $pi = 22/7; $result = 2*$pi*$radius; print “radius $radius is circumference $result\n”; Perl

  34. repeat.perl print ”Enter string to repeat: “; $string=<STDIN>; print “Enter number of times to repeat: “; chop($repeat=<STDIN>); print “The result:\n”,$string x $repeat; Perl

  35. Arrays • An ordered list of scalar data • Each element is a scalar variable or value • Ordered means they have a position • No bounds • Often referred to as lists • Literal representation • (1,2,3) • (“fred”,4.5) • ($a,17) • () Perl

  36. List Constructor Operator • An element of an array can include the list constructor operator • (1..5)  (1,2,3,4,5) • (1.2..5.2)  (1.2,2.2,3.2,4.2,5.2) • (1.3..6.1)  (1.3,2.3,3.3,4.3,5.3) • (‘a’..’d’)  (‘a’,‘b’,‘c’,‘d’) • (5..1)  () Perl

  37. Array Variables • An array variable holds a single array value • Names are the same as scalars except they start with ‘@’ • @numbers • Note $numbers is not related to @numbers • Initial value is () Perl

  38. Assignment • It works for arrays!! • @fred = (1,2,3); • @barney = @fred; • @huh = 1; • @barney = (4,5,@fred,6,7); • Arrays cannot contain arrays Perl

  39. Assignment to Arrays • Check this out • ($a,$b,$c) = (1,2,3); • ($a,$b) = ($b,$a); • ($d,@fred) = ($a,$b,$c); • If the number of values being assigned does not match • Too many  they are simply thrown away • Too few  corresponding values get undef Perl

  40. Length • If an array variable is assigned to a scalar variable, the number assigned is the length of the array • @fred = (1..5); • $length = @fred; • The value of an array assignment is itself an array value • $#fred will give the index value of the last element of fred Perl

  41. Element Access • Array elements are numbered with sequential integers beginning at zero • The first element of @fred is $fred[0] • Note the element is considered a scalar • You can access slices of an array • @fred[0,1]  elements 0 and 1 in fred • @fred[0,1]=@fred[1,0]; • @fred[1,2]=(9,10); • print @fred[0..4]; Perl

  42. push() and pop() • It is easy to use an array as a stack in Perl • push(@stack,$newValue); • Similar to @stack=(@stack,$newValue); • $value = pop(@stack); • Similar to @stack=@stack[0..$#stack-1]; • You can push a list of elements • push(@stack,1,2,3,4); • pop() returns undef if the list is empty Perl

  43. shift() and unshift() • Think push() and pop() from the other end of the list • Push and pop work at the right hand (high index) end of the array • Shift and unshift work at the left hand (low index) end of the array • unshift() returns undef if the array is empty Perl

  44. reverse() • The reverse() operator reverse the order of the elements of its argument • @a=(7,8,9); • @b=reverse(@a); • @b=reverse(1..10); • @b=reverse(@b); • Note that reverse() does not change its arguments (unlike chop()) Perl

  45. sort() • The sort() operator sorts its arguments treating them as strings • sort(“small”,“medium”,“large”); • “large”, “medium”, “small” • sort(1,4,2,8,32,64,16); • 1, 2, 4, 8, 16, 32, 64 • sort(210,22,333,3000); • 210, 22, 3000, 333 Perl

  46. chop() • chop() works on lists as well • It chops each element in the list • Useful if you read in a list of lines from the keyboard • <STDIN> can be treated as an array • @a=<STDIN>; • chop(@a); • print @a,“\n”; Perl

  47. Your Turn • Write a program that reads a list of strings and prints out the list in reverse order • Write a program that reads a number and then a list of strings, and then prints one of the lines from the list as selected by the number. Perl

  48. reverse.perl print “Enter your strings: “; @list = <STDIN>; print “Reversed:\n”,reverse(@list); Perl

  49. select.perl print “Enter the line number: “; chop( $lineNum=<STDIN> ); print “Enter your strings: “; @list = <STDIN>; print “Answer: ”,$list[$lineNum-1]; Perl

  50. Statement Blocks { first_statement; second_statement; third_statement; … last_statement; } Perl

More Related