1 / 21

A Brief Introduction to Perl

A Brief Introduction to Perl. Part I. A Brief Introduction to Perl. Introduction Scalar Data Variables Operators If-Else Control Structures While Control Structures Standard Input Lists and Arrays. Introduction. Perl 1 - Created by Larry Wall in 1987 Stable Release: Perl 5.10.1.

davis-king
Télécharger la présentation

A Brief Introduction to 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. A Brief Introduction to Perl Part I

  2. A Brief Introduction to Perl • Introduction • Scalar Data • Variables • Operators • If-Else Control Structures • While Control Structures • Standard Input • Lists and Arrays

  3. Introduction • Perl 1 - Created by Larry Wall in 1987 • Stable Release: Perl 5.10.1

  4. Scalar Data Two basic scalar data types: Numbers and Strings • Numbers • Integers and Floating-Point Numbers have the same format. • Ex. $v=8      #v contains value of 8 • Strings • Sequences of Characters. • Can either be enclosed in single or double quotes. • Ex. $v=“a”      #v contains value of “a” Undef – For Numbers or Strings, there is a special value called undef. This is initially assigned to all variables and acts as a zero for numbers and a null or empty string for strings.

  5. Variables • A scalar variable holds a single scalar value • Names of any scalar variable must begin with the “$” symbol followed by a Perl identifier Ex. $tom, $cs265

  6. Operators • Assignment • Arithmetic • String • Numeric Comparison • String Comparison

  7. Assignment Operator = Ex. $cs = 265

  8. Arithmetic Operators +, -, *, / Ex. $c = a + b (Addition) $c = a – b (Subtraction) $c = a * b (Multiplication) $c = a / b (Division)

  9. String Operators ., x . (dot) – String Concatenation Ex. $cl = class, $ro = room, $cl . $ro = classroom x – String Multiplier Ex. $cl x 2 = classclass

  10. Numeric Comparison Operators ==, !=, <,>,<=,>= “==” = Equal To “!=” = Not Equal To “<” = Less Than “>” = Greater Than “<=” = Less Than or Equal To “>=” = Greater Than or Equal To

  11. String Comparison Operators eq, ne, lt, gt, le, ge “eq” = Equal To “ne” = Not Equal To “lt” = Less Than “gt” = Greater Than “le” = Less Than or Equal To “ge” = Greater Than or Equal To

  12. Two Special Features of Perl • 1. Interpolation of scalars into strings • Any scalar variable inside a double quoted string is replaced by its value. • Ex. $example=“example”; print "$example"; prints example • 2. Automatic conversion between numbers and strings • Arguments of an arithmetic operator are automatically recognized as numbers, and arguments of a string operator as strings. • Ex. $one = 1, $two = 2.. • print $one*$two;  prints 2 • print $one x $two;  prints 11 • print $one . $two  prints 12

  13. If-Else Control Structures Conditional Loop if(…){ … } else { … }

  14. Example: If-Else Control Structure $expected = “apple”; $given = “pear”; if($expected eq $given){ print “Apple” } else { print “Other Fruit” }

  15. While Control Structure Conditional Loop while (…) { … }

  16. Example: While Control Structure $num = 1; while ($num != 1111) { $num . $num; }

  17. Standard Input • Input in Perl is very convenient • <STDIN> Ex. Typical Line of Code for input $line=<STDIN> # read a full line from standard input and store it in variable $line

  18. Example $line=<STDIN>; if($line eq "\n"){ print "This was an empty line.\n"; } else { print "This line was not empty.\n"; }

  19. Lists • Lists are also user friendly in Perl • List Literals (representations of lists) • Ex. (1,2,3,4) # a list consisting of numbers 1,2,.. 4 • Ex. (1..10) # the same list as the one above, now created by the range operator • ($a ..$b) - the range determined by current values of $a and $b List Assignment Examples:

  20. Arrays • Arrays are variables storing lists. • @num = (1..100) # creates an array “num” with a numbers 1 through 100 • print $num[0] # prints out 1 • print $num[$#num] # prints out the largest index of the array

More Related