Advantages of Perl by bzs12927


									                       Lecture 9                                                          Advantages of Perl
                    Introduction to Perl                                      • Fills the gap between shell programming and
                                                                                a conventional compiled programming
         COP 3344 Introduction to UNIX                                          language like C or C++

                            Fall 2007                                         • Perl code is very dense
                                                                                   – Size is often 30% to 70% that of similar C code
                                                                              • Good for simple programs that you want to
                                                                                code quickly
                                                                                   – Good for text manipulation
                                                                              • It is very portable
    Acknowledgment: These slides are modified versions of Prof. Sudhir
                          Aggarwal’s slides
                                                                         1                                                                   2

      A Sample Perl Program                                                               Scalar Data in Perl
             hello                     $ chmod 700 hello
 #!/usr/bin/perl -w                    $ ./hello                             • A scalar is a single item of data
                                       Hello world!
 #Test program: hello world                                                     – A number, for example 255 or 3.1416e2
 print “hello world!\n”;
                                                                                – A string, for example ‘hello\n’ or “good bye”
 exit 0;
                                                                                     • ‘the \n does not have a special meaning here’
                                                                                     • “the \n here represents the newline character”
 • Program features                                                                  • “the $var variable is replaced by its value here”

    – #! specifies the program that executes the file                        • Perl uses strings and numbers almost interchangeably
        • The -w flag prints warnings                                           – Implicit conversion in performed between strings and numbers
                                                                                  depending on the operations performed on the scalar data
    – Comments start with #
                                                                                           hello               $ chmod 700 hello
    – White space can be used almost anywhere                                  #!/usr/bin/perl -w              $ ./hello
                                                                               #Test program: hello world      Hello world!\n$
    – Statements end with a ;
                                                                               print ‘hello world!\n’;
                                                                               exit 0;
                                                                         3                                                                   4

              Scalar Variables                                                        Examples of Operators
• Names preceded by $ regardless of its use                                    =                              assignment
  on the left or right side of an assignment                                   +, -, *, …                     arithmetic

• Examples                                                                     <, <=, …                       relational
                                                                               &&, ||, !                      logical
  $sum = 14
                                                                               ++, --                         increment, decrement
  $sum = $var + 47.3
                                                                               eq, ne, lt, gt, le, ge                   string relational
                pprog2                $./pprog2
        #!/usr/bin/perl -w            bandaid                                  cmp                            string comparison
        $s="band" . $help;
                                                                               .                              concatenation
        print "$s\n";
                                                                               x                              string repetition
                                                                                    “fred” x 3               result is “fredfredfred”
                                                                         5                                                                   6

 Line Input Operator <STDIN>                                                         Acting on Each Line
• The <STDIN> operator reads line of input                                   #!/usr/bin/perl -w -n
   – Read from standard input, up to and including the next                  print;
     newline character                                                    • The -n causes the program to be executed on each
  $line = <STDIN>;                                                          line
   – If the end-of-file is reached, then <STDIN> returns undef,                     pprog4               datafile       $./pprog4 < datafile
     which acts like 0 or the empty string                                  #!/usr/bin/perl -w -n    Name       GPA     Name       GPA
                                                                                                     asd         4.0    asd         4.0
   – The chomp operator is used to remove a newline from the                print;                   sdf         3.2    sdf         3.2
     end of a string                                                                                 fghsd       3.6    fghsd       3.6
                                                                                                     qwer        4.0    qwer        4.0
     chomp ($line = <STDIN>);
                  pprog3                      $./pprog3
    #!/usr/bin/perl -w
    $line = <STDIN>;                          Blank line!
    if($line eq "\n"){                        $ ./pprog3
      print "Blank line!\n";}                 sdf
    else{                                     The line was: sdf
      print "The line was: $line";}
                                                                      7                                                                         8

             Pattern Matching                                                        Pattern Matching with
• Match patterns using m/Pattern/                                                         Substitution
   – Usually used with the binding operator =~
                                                                          • Substitute patterns using s/Pattern/Substitute/
   – Example: $mystring =~ m/cat+/ has the value true if
     $mystring has any of the following values: cat, catt, cattt,                 pprog6                           $./pprog6 < datafile2
     ...                                                                  #!/usr/bin/perl -w -n                    dogs are good
                                                                          $line = $_;                              dogs are good
                                                                          $line =~ s/cat+/dog/;                    dogs are good, good cat
          pprog5                datafile       $./pprog5 < datafile
  #!/usr/bin/perl -w -n     Name       GPA     asd         4.0            print $line;
  if($_ =~ m/4\.0/)         asd         4.0    qwer        4.0
  {                         sdf         3.2
                                                                                           dogs are good
     print $_;              fghsd       3.6
                                                                                           cats are good
  }                         qwer        4.0
                                                                                           catts are good, good cat

                                                                                   pprog7                          $./pprog7 < datafile2
                                                                          #!/usr/bin/perl -w -n                    dogs are good
                                                                          $line = $_;                              dogs are good
                                                                          $line =~ s/cat+/dog/g;                   dogs are good, good dog
                                                                          print $line;

                                                                      9                                                                        10


To top