jagomart
digital resources
picture1_Perl Pdf 187660 | Lecture 19   Perl Programming


 179x       Filetype PDF       File size 0.07 MB       Source: www.cs.cmu.edu


File: Perl Pdf 187660 | Lecture 19 Perl Programming
lecture 19 perl programming perl practical extraction and report language is a powerful and adaptable scripting language perl became very popular in early 90 s as web became a reality ...

icon picture PDF Filetype PDF | Posted on 02 Feb 2023 | 2 years ago
Partial capture of text on file.
                            Lecture 19 
                          Perl Programming 
           
          Perl (Practical Extraction and Report Language) is a powerful and adaptable scripting 
          language. Perl became very popular in early 90’s as web became a reality. Perl is ideal 
          for processing text files containing strings. Perl is also good for processing web pages 
          containing tags of different types (image tags, url tags etc). These tags or substrings can 
          be extracted using Perl commands. Perl programs are ideal for managing web 
          applications, such as passwd authentication, database access, network access, and multi-
          platform capabilities. Perl is also good for working with html web forms, obtaining user 
          inputs, enabling cookies and tracking clicks and access counters, connecting to mail 
          servers, integrating perl with html, remote file management via the web, creating 
          dynamic images among many other capabilities. 
           
          Perl programs are not compiled but interpreted. Perl interpreter in your unix system can 
          be found by typing 
           
             where perl 
           
          It may show 
          /usr/local/bin/perl 
          /usr/bin/perl 
           
          giving the path of the perl interpreter. Perl interpreter is used to run perl programs. 
           
          Let us start with a simple Hello world program in perl. 
           
          #!/usr/local/bin/perl 
          print "Hello World\n"; 
           
          WARNING: The #!/usr/local/bin/perl must be the first line in the file. 
          DO NOT ADD comments # before that line 
           
          Assuming this is in a file called hello.pl, we can run the program by 
          typing 
             perl hello.pl 
           
          Or you can set the executable permission for the file and run the 
          program as follows. 
           
             chmod +x hello.pl 
             ./hello.pl 
           
           
           
           
           
           
           
           
                      Copyright @ 2009 Ananda Gunawardena 
           
                         Scalars in Perl 
                         A scalar in perl is either a numeric (103, 45.67) or a string. A string 
                         is a sequence of characters where each character is represented by 8-
                         bits. There is also null string or the shortest possible string that 
                         has no characters. A string inside single quotes (‘hello there’) is a 
                         literal string, and double quoted strings can have escape characters 
                         such as ‘\t’ (tab) inside them for formatting purposes. A double quoted 
                         string is very much like a C string. 
                          
                         Strings in Perl 
                         Perl strings can be surrounded by single quotes or double quotes. 
                         Single quote means string must be interpreted literally and double 
                         quotes could have “\n” type escape characters that have special 
                         meaning. So for example 
                          
                         print “hello world\n”;  prints the string hello world with a new line 
                         print ‘hello world\n’;  prints the string hello world\n 
                          
                         Operators for Strings 
                         Strings can be concatenated using “.” Operator. So if we define two 
                         strings s1 and s2 and concatenate and store them in a string s3, you 
                         would do it like this in perl. 
                          
                         $s1 = “hello”; 
                         $s2 = “world”; 
                         $s3 = $s1.$s2; 
                          
                         Note that variable declarations are preceded by $. Other useful 
                         functions that can operate on strings are: 
                          
                              ·    substr($s,start, length) --- substring of $s from start of length 
                              ·    index string, substring, position – look for first index of the 
                                   substring in string starting from position 
                              ·    index string, substring – look for first index of the substring 
                                   in string starting from the beginning 
                              ·    rindex string, substring –- position of substring in string 
                                   starting from the end of the string 
                              ·    length(string) – returns the length of the string 
                              ·    $_ = string; tr/a/z/; -- replaces all ‘a’ characters of string 
                                   with a ‘z’ character and assign to $1.  
                              ·    $_ = string; tr/ab/xz/; -- replaces all ‘a’ characters of string 
                                   with a ‘x’ character and b with z and assign to $1. More 
                                   variations available. 
                              ·    $_ = string; s/foo/me/; -- replaces all strings of “foo” with 
                                   string “me” 
                              ·    chop – this removes the last character at the end of a scalar. 
                              ·    chomp – removes a newline character from the end of a string 
                              ·    split – splits a string and places in an array 
                                        o    @array = split(/:/,$name); # splits the string $name at 
                                             each : and stores in an array (see arrays ahead) 
                                        o    The ASCII value of a character $a is given by ord($a)  
                          
                          
                                                           Copyright @ 2009 Ananda Gunawardena 
                          
            
           Comparison Operators 
            
               Comparison     Numeric    String 
           Equal               ==          Eq 
           Not Equal           !=          Ne 
           Greater than         >          Gt 
           Less than            <          Lt 
           Greater or equal    >=          Ge 
           Less or equal       <=          Le 
            
            
           Another string operator of special interest is the letter x (lower 
           case). This operator causes the variable to be repeated. For example, 
            
           $s1 = “guna”; 
           $s2 = $s1 x 3;  
            
           will cause $s2 to store the string “gunagunaguna” 
            
           Operator Precedence and Associativity 
                 
             Associativity        Operator 
                left        terms and list operators (leftward) 
                left        -> 
                nonassoc    ++ -- 
                right       ** 
                right       ! ~ \ and unary + and - 
                left        =~ !~ 
                left        * / % x 
                left        + - . 
                left        << >> 
                nonassoc    named unary operators (chomp) 
                nonassoc    < > <= >= lt gt le ge 
                nonassoc    == != <=> eq ne cmp 
                left        & 
                left        | ^ 
                left        && 
                left        || 
                nonassoc    ..  ... 
                right       ?: 
                right       = += -= *= etc. 
                left        , => 
                nonassoc    list operators (rightward) 
                right       not 
                left        and 
                left        or xor 
             
            source: perl.com 
                                                                
            
            
            
            
            
                          Copyright @ 2009 Ananda Gunawardena 
            
                  
                 Variables in Perl 
                 We have already seen how to define a variable. Perl has three types of 
                 variables - scalars (strings or numeric’s), arrays and hashes.  
                 Let us look at defining scalar variables. 
                  
                 $x = 45.67; 
                 $var = ‘cost’; 
                  
                 So a statement such as  
                  
                 print “$var is $x”;  
                  
                 will print “cost is 45.67”.  Simple arithmetic can be performed on 
                 numeric variables such as 
                  
                 $x = 563; 
                 $y = 32.56; 
                 $y++; 
                 $x += 3; 
                  
                 Arrays 
                 Array in perl is defined as a list of scalars. So we can have arrays of 
                 numerics or strings. For example, 
                  
                 @array = (10,12,45); 
                 @A = (‘guna’, ‘me’, ‘cmu’, ‘pgh’); 
                  
                                                                                  th
                 Defines arrays of numeric’s and strings. To process the i  element of 
                 an array A (array indices starts from 0) we simply refer to $A[$i]. For 
                 example, we can write 
                  
                 $i = 1; 
                 $A[$i] = ‘guna’; 
                  
                 this sets the element in A with index 1 to “guna”. 
                  
                 The length of an array A can be found using $#A. The length of an 
                 array is one more than $#A. That is 
                  
                 $len = $#A + 1 
                  
                 You can also find length of an array as  
                 $len = @A; 
                  
                 To resize an array, we can simply set the $#A to desired size. 
                 So for example, 
                  
                 @array = (10,12,45); 
                 $#array = 1;  
                  
                 Will result in an array of size 2 or simply 
                  
                 @array = (10,12); 
                  
                  
                                        Copyright @ 2009 Ananda Gunawardena 
                  
The words contained in this file might help you see if this file matches what you are looking for:

...Lecture perl programming practical extraction and report language is a powerful adaptable scripting became very popular in early s as web reality ideal for processing text files containing strings also good pages tags of different types image url etc these or substrings can be extracted using commands programs are managing applications such passwd authentication database access network multi platform capabilities working with html forms obtaining user inputs enabling cookies tracking clicks counters connecting to mail servers integrating remote file management via the creating dynamic images among many other not compiled but interpreted interpreter your unix system found by typing where it may show usr local bin giving path used run let us start simple hello world program print n warning must first line do add comments before that assuming this called pl we you set executable permission follows chmod x copyright ananda gunawardena scalars scalar either numeric string sequence character...

no reviews yet
Please Login to review.