for web applications
play

for Web Applications 01 Introduction to Perl Alexandros Labrinidis - PDF document

CS 1520 / Fall 2012 Programming Languages for Web Applications 01 Introduction to Perl Alexandros Labrinidis University of Pittsburgh What is Perl n P ractical E xtraction and R eporting L anguage n 1987 n Larry Wall n roots in


  1. CS 1520 / Fall 2012 Programming Languages for Web Applications 01 – Introduction to Perl Alexandros Labrinidis University of Pittsburgh What is Perl n P ractical E xtraction and R eporting L anguage n 1987 n Larry Wall n roots in unix applications (awk/sed) n http://history.perl.org n Uses of Perl: n EVERYWHERE :-) n Scripts for repeated tasks n Powerful regular expression matching n Text processing n Web programming 2 � Alexandros Labrinidis, Univ. of Pittsburgh � CS 1520 / Fall 2012 � 1

  2. How to run Perl programs (revised) Use text editor (say pico) to write and save program, say hello.pl n n Option 1: perl -w hello.pl Next time: n perl - w hello.pl n Option 2: n Make sure first line of hello.pl is the following #!/bin/perl -w chmod u+x hello.pl ./hello.pl Next time: n ./hello.pl 3 � Alexandros Labrinidis, Univ. of Pittsburgh � CS 1520 / Fall 2012 � Hello World #!/bin/perl -w print ( “ Hello world!\n ” ); n Observations: n First line is needed to make program self-executable n -w flag is highly recommended: it prints out warnings n All statements in in semicolon ; n Strings are enclosed in double quotes “ (more on this later) 4 � Alexandros Labrinidis, Univ. of Pittsburgh � CS 1520 / Fall 2012 � 2

  3. Scalar Data: Numbers When we have “ one of something ” n n E.g., numbers, strings Examples:Floating point literals Examples: Integer literals n n n 1.25 n 0 n 255.000 n 2001 n 255.0 n 1520 n 3.14159 n -40 n -6.5e24 n 61298040283768 n 1.2E-4 n 61_298_040_283_768 5 � Alexandros Labrinidis, Univ. of Pittsburgh � CS 1520 / Fall 2012 � Scalar Data: Numbers n Examples: non-decimal integer n 0377 is 377 octal, same as 255 decimal n 0xff is FF hex, same as 255 decimal n 0b11111111 is in binary, same as 255 decimal n 0x50_65_72_7C 6 � Alexandros Labrinidis, Univ. of Pittsburgh � CS 1520 / Fall 2012 � 3

  4. Numeric operators n 2 + 3 # 2 plus 3, or 5 n 5.1 - 2.4 # 5.1 minus 2.4, or 2.7 n 3 * 12 # 3 times 12 = 36 n 14 / 2 # 14 divided by 2, or 7 n 10.2 / 0.3 # 10.2 divided by 0.3, or 34 n 10 / 3 # always floating-point divide, so 3.3333333... n Modulus operator: n 10 % 3 # 10 module 3, or 1 n 10.5 % 3.2 # first converted to 10 % 3 n Exponent operator: n 2 ** 3 # 2 to the 3rd, or 8 7 � Alexandros Labrinidis, Univ. of Pittsburgh � CS 1520 / Fall 2012 � Scalar Data: Strings n Strings are sequences of characters (like hello). n Strings may contain any combination of any characters n Two types: n Single-quoted string literals n No variable substitution in string n Double-quoted string literals n Allow for variable substitution 8 � Alexandros Labrinidis, Univ. of Pittsburgh � CS 1520 / Fall 2012 � 4

  5. Single-quoted Strings n 'fred' # those four characters: f, r, e, and d n 'barney' # those six characters n '' # the null string (no characters) n 'Don\'t let an apostrophe end this string prematurely! ’ n 'the last character of this string is a backslash: \\ ’ n 'hello\n' # hello followed by backslash followed by n n 'hello there' # hello, newline, there (11 characters total) n ’ \ ’ \\' # single quote followed by backslash Note that the \n within a single-quoted string is not interpreted as a newline, n but as the two characters backslash and n. Only when the backslash is followed by another backslash or a single quote does it have special meaning. 9 � Alexandros Labrinidis, Univ. of Pittsburgh � CS 1520 / Fall 2012 � Double-quoted strings n "barney" # just the same as 'barney ’ n "hello world\n" # hello world, and a newline n "The last character of this string is a quote mark: \" ” n "coke\tsprite" # coke, a tab, and sprite 10 � Alexandros Labrinidis, Univ. of Pittsburgh � CS 1520 / Fall 2012 � 5

  6. String operators n Concatenation n "hello" . "world" # same as "helloworld ” n "hello" . ' ' . "world" # same as 'hello world ’ n 'hello world' . ” \n" # same as "hello world\n ” n String repetition n "fred" x 3 # is "fredfredfred ” n "barney" x (4+1) # is "barney" x 5, or "barneybarneybarneybarneybarney ” n 5 x 4 # is really "5" x 4, which is "5555" 11 � Alexandros Labrinidis, Univ. of Pittsburgh � CS 1520 / Fall 2012 � Scalar Variables n A variable is a name for a container that holds one or more values n Unlike other languages, there is no variable definition/declaration in Perl (unless you invoke “ strict ” ) n Example variable names: n $a_very_long_variable_that_ends_in_1 n $a_very_long_variable_that_ends_in_2 n $Fred is different from $fred n $is_it_better_with_underscores n $orIsItBetterWithCaps 12 � Alexandros Labrinidis, Univ. of Pittsburgh � CS 1520 / Fall 2012 � 6

  7. Assignment n $fred = 17; # give $fred the value of 17 n $barney = 'hello'; # give $barney the five-character string 'hello ’ n $barney = $fred + 3; # give $barney the current value of $fred plus 3 (20) n $barney = $barney * 2; # $barney is now $barney multiplied by 2 (40) 13 � Alexandros Labrinidis, Univ. of Pittsburgh � CS 1520 / Fall 2012 � Binary Assignment n $fred = $fred + 5; # without the binary assignment operator n $fred += 5; # with the binary assignment operator n $barney = $barney * 3; $barney *= 3; n $str = $str . " "; # append a space to $str $str .= " "; # same thing with assignment operator 14 � Alexandros Labrinidis, Univ. of Pittsburgh � CS 1520 / Fall 2012 � 7

  8. Print print "hello world\n"; � # say hello world, followed by a newline � print "The answer is "; � print 6 * 7; � print ".\n"; � � OR: � print "The answer is ", 6 * 7, ".\n"; 15 � Alexandros Labrinidis, Univ. of Pittsburgh � CS 1520 / Fall 2012 � Scalar Variables into strings n $meal = "brontosaurus steak"; n $barney = "fred ate a $meal"; # $barney is now "fred ate a brontosaurus steak ” n $barney = 'fred ate a ' . $meal; # another way to write that 16 � Alexandros Labrinidis, Univ. of Pittsburgh � CS 1520 / Fall 2012 � 8

  9. Lists and Arrays in Perl n Q: What is a List or Array? n A: a list is ordered scalar data n A: an array is a variable that holds a list n Example - list literals: n (1, 2, 3) #array of three values: 1, 2, and 3 n ( “ fred ” , 4, 5) #array of three values: “ fred ” , 4, and 5 n ($a, 42) #two values: current value of $a and 42 n ($a+$b, $c+$d) #also two values n () #the empty list (no elements) n (1 .. 5) #same as (1, 2, 3, 4, 5) 17 � Alexandros Labrinidis, Univ. of Pittsburgh � CS 1520 / Fall 2012 � More examples - arrays n @a = ( “ rachel ” , “ phoebe ” , “ chandler ” , “ ross ” , “ monica ” , “ joey ” ); n Shortcut: n @a = qw (rachel phoebe chandler ross monica joey); n qw stands for quote word : it creates a list from the nonwhitespace parts within the parentheses n @b = qw(mary 2 5 had a little 4 lamb 6); n Q: what will the following do? print ( “ the answer is “ ,@a, ” \n ” ); 18 � Alexandros Labrinidis, Univ. of Pittsburgh � CS 1520 / Fall 2012 � 9

  10. Arrays - Assignment n @rachel = (1, 2, 3); #the rachel array is a 3-element list literal n @monica = @rachel; #the rachel array is copied to the monica array n @joey = 1; # 1 is automatically converted to (1) n @ross = qw(one two three); # equivalent to @ross = ( “ one ” , “ two ” , “ three ” ); 19 � Alexandros Labrinidis, Univ. of Pittsburgh � CS 1520 / Fall 2012 � Arrays - Assignment (RHS) n @phoebe = qw(smelly cat); #equivalent to @phoebe = ( “ smelly ” , “ cat ” ); n @chandler = ( “ here ” , “ is ” , @phoebe, 5, 6); #chandler becomes ( “ here ” , “ is ” , “ smelly ” , “ cat ” , 5, 6); n @chandler = (42, @chandler); #puts 42 in front of chandler n @chandler = (@chandler, “ last ” ); #puts “ last ” at the end of chandler 20 � Alexandros Labrinidis, Univ. of Pittsburgh � CS 1520 / Fall 2012 � 10

  11. Arrays - Assignment (LHS) n ($a, $b, $c) = (1, 2, 3) #give value of 1 to $a, 2 to $b, 3 to $c n ($a, $b) = ($b, $a) #swap $a and $b n ($d, @ross) = ($a, $b, $c) #give $a to $d, and ($b, $c) to @ross n ($f, @ross) = @ross; #remove first element of @ross and give it to $f #this makes $f = $b, and @ross = ($c) 21 � Alexandros Labrinidis, Univ. of Pittsburgh � CS 1520 / Fall 2012 � Scalar and List context n Perl will do automatic “ conversion ” of an array, depending on the context. n List context : normal n Scalar context : when the array must be “ squeezed ” into a scalar variable. n In this case, the length of the array is returned! n Examples: n @barney = @fred; # all of @fred is copied to @barney n ($a) = @fred; # $a gets the first element of @fred n $a = @fred; # $a gets the length of @fred 22 � Alexandros Labrinidis, Univ. of Pittsburgh � CS 1520 / Fall 2012 � 11

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend