SlideShare a Scribd company logo
1 of 171
Download to read offline
Let’s Learn Perl
An introduction to Perl


      Sreejith S
NLP Process Analyst
 365Media Pvt. Ltd.
srssreejith@gmail.com



   January 22, 2011




    Sreejith S   Let’s Learn Perl
Just a word about me !!




     Working in Natural Language Processing (NLP), Machine Learning,
     Data Mining
     Passionate about Free and Open source :-)
     Works for 365Media Pvt. Ltd. Coimbatore India.




                            Sreejith S   Let’s Learn Perl
What’s Perl ??




     Practical Extraction and Reporting Language




                            Sreejith S   Let’s Learn Perl
What’s Perl ??




     Practical Extraction and Reporting Language
     Perl is a Portable Scripting Language




                             Sreejith S   Let’s Learn Perl
What’s Perl ??




     Practical Extraction and Reporting Language
     Perl is a Portable Scripting Language
     Fast and easy text processing capability




                             Sreejith S   Let’s Learn Perl
What’s Perl ??




     Practical Extraction and Reporting Language
     Perl is a Portable Scripting Language
     Fast and easy text processing capability
     Fast and easy file handling capability




                             Sreejith S   Let’s Learn Perl
What’s Perl ??




     Practical Extraction and Reporting Language
     Perl is a Portable Scripting Language
     Fast and easy text processing capability
     Fast and easy file handling capability
     It aims to be practical (easy to use, efficient, complete) rather than
     beautiful (tiny, elegant, minimal)




                             Sreejith S   Let’s Learn Perl
What’s Perl ??




     Practical Extraction and Reporting Language
     Perl is a Portable Scripting Language
     Fast and easy text processing capability
     Fast and easy file handling capability
     It aims to be practical (easy to use, efficient, complete) rather than
     beautiful (tiny, elegant, minimal)
     Perl was developed by Larry Wall.




                             Sreejith S   Let’s Learn Perl
To install...




      Perl Comes by Default on Linux, Cygwin, MacOSX




                            Sreejith S   Let’s Learn Perl
To install...




      Perl Comes by Default on Linux, Cygwin, MacOSX
          www.perl.com has rpm’s for Linux




                             Sreejith S   Let’s Learn Perl
To install...




      Perl Comes by Default on Linux, Cygwin, MacOSX
          www.perl.com has rpm’s for Linux
          www.activestate.com has binaries for Windows




                              Sreejith S   Let’s Learn Perl
To install...




      Perl Comes by Default on Linux, Cygwin, MacOSX
          www.perl.com has rpm’s for Linux
          www.activestate.com has binaries for Windows
      Latest Version is 5.10




                               Sreejith S   Let’s Learn Perl
To install...




      Perl Comes by Default on Linux, Cygwin, MacOSX
          www.perl.com has rpm’s for Linux
          www.activestate.com has binaries for Windows
      Latest Version is 5.10
          To check if Perl is working and the version number




                               Sreejith S   Let’s Learn Perl
To install...




      Perl Comes by Default on Linux, Cygwin, MacOSX
          www.perl.com has rpm’s for Linux
          www.activestate.com has binaries for Windows
      Latest Version is 5.10
          To check if Perl is working and the version number
                perl -v or perl -version




                                   Sreejith S   Let’s Learn Perl
Sreejith S   Let’s Learn Perl
Basic Syntax of Perl


     Comments




                       Sreejith S   Let’s Learn Perl
Basic Syntax of Perl


     Comments
         begin with #




                        Sreejith S   Let’s Learn Perl
Basic Syntax of Perl


     Comments
         begin with #
         dont try to use // or /* ... */




                              Sreejith S   Let’s Learn Perl
Basic Syntax of Perl


     Comments
         begin with #
         dont try to use // or /* ... */
     Print




                              Sreejith S   Let’s Learn Perl
Basic Syntax of Perl


     Comments
         begin with #
         dont try to use // or /* ... */
     Print
         print is simple function to display/output something on
         monitor/stdout




                              Sreejith S   Let’s Learn Perl
Basic Syntax of Perl


     Comments
         begin with #
         dont try to use // or /* ... */
     Print
         print is simple function to display/output something on
         monitor/stdout
         e.g :- print “hello world“;




                              Sreejith S   Let’s Learn Perl
Basic Syntax of Perl


     Comments
         begin with #
         dont try to use // or /* ... */
     Print
         print is simple function to display/output something on
         monitor/stdout
         e.g :- print “hello world“;
     Statement terminator ;




                              Sreejith S   Let’s Learn Perl
Basic Syntax of Perl


     Comments
         begin with #
         dont try to use // or /* ... */
     Print
         print is simple function to display/output something on
         monitor/stdout
         e.g :- print “hello world“;
     Statement terminator ;
         All Statements end with ; like C language




                              Sreejith S   Let’s Learn Perl
Basic Syntax of Perl


     Comments
         begin with #
         dont try to use // or /* ... */
     Print
         print is simple function to display/output something on
         monitor/stdout
         e.g :- print “hello world“;
     Statement terminator ;
         All Statements end with ; like C language
     Delimiter




                              Sreejith S   Let’s Learn Perl
Basic Syntax of Perl


     Comments
         begin with #
         dont try to use // or /* ... */
     Print
         print is simple function to display/output something on
         monitor/stdout
         e.g :- print “hello world“;
     Statement terminator ;
         All Statements end with ; like C language
     Delimiter
         {} delimit blocks, loops, subroutines




                              Sreejith S   Let’s Learn Perl
Basic Syntax of Perl


     Comments
         begin with #
         dont try to use // or /* ... */
     Print
         print is simple function to display/output something on
         monitor/stdout
         e.g :- print “hello world“;
     Statement terminator ;
         All Statements end with ; like C language
     Delimiter
         {} delimit blocks, loops, subroutines
     No main() function




                              Sreejith S   Let’s Learn Perl
Basic Syntax of Perl


     Comments
         begin with #
         dont try to use // or /* ... */
     Print
         print is simple function to display/output something on
         monitor/stdout
         e.g :- print “hello world“;
     Statement terminator ;
         All Statements end with ; like C language
     Delimiter
         {} delimit blocks, loops, subroutines
     No main() function
         Top-down programming approach




                              Sreejith S   Let’s Learn Perl
Structure of a Perl Program




     #! /usr/bin/perl -w




                           Sreejith S   Let’s Learn Perl
Structure of a Perl Program




     #! /usr/bin/perl -w
         It tells where to find Perl




                               Sreejith S   Let’s Learn Perl
Structure of a Perl Program




     #! /usr/bin/perl -w
         It tells where to find Perl
     .pl extension is optional but is commonly used




                               Sreejith S   Let’s Learn Perl
Structure of a Perl Program




     #! /usr/bin/perl -w
         It tells where to find Perl
     .pl extension is optional but is commonly used
         You can write perl programs without extension also.




                               Sreejith S   Let’s Learn Perl
Structure of a Perl Program




     #! /usr/bin/perl -w
         It tells where to find Perl
     .pl extension is optional but is commonly used
         You can write perl programs without extension also.
     -w switches on warning : not required but a really good idea




                               Sreejith S   Let’s Learn Perl
Customary Hello World Program




  #! /usr/bin/perl -w
  print "Hello World!n";




                            Sreejith S   Let’s Learn Perl
Customary Hello World Program




  #! /usr/bin/perl -w
  print "Hello World!n";

     Save this as hello.pl




                             Sreejith S   Let’s Learn Perl
Customary Hello World Program




  #! /usr/bin/perl -w
  print "Hello World!n";

     Save this as hello.pl
     To run the program




                             Sreejith S   Let’s Learn Perl
Customary Hello World Program




  #! /usr/bin/perl -w
  print "Hello World!n";

     Save this as hello.pl
     To run the program
          perl hello.pl or perl hello




                                 Sreejith S   Let’s Learn Perl
Variables


      Variables are memory location to store information.




                              Sreejith S   Let’s Learn Perl
Variables


      Variables are memory location to store information.
      Variables are type less




                              Sreejith S   Let’s Learn Perl
Variables


      Variables are memory location to store information.
      Variables are type less
            There is no data type like int,char,float




                                 Sreejith S   Let’s Learn Perl
Variables


      Variables are memory location to store information.
      Variables are type less
            There is no data type like int,char,float
      Variables do not need to be declared




                                 Sreejith S   Let’s Learn Perl
Variables


      Variables are memory location to store information.
      Variables are type less
            There is no data type like int,char,float
      Variables do not need to be declared
      Every variable is a string




                                 Sreejith S   Let’s Learn Perl
Variables


      Variables are memory location to store information.
      Variables are type less
            There is no data type like int,char,float
      Variables do not need to be declared
      Every variable is a string
            Variable type is decided at run time




                                 Sreejith S   Let’s Learn Perl
Variables


      Variables are memory location to store information.
      Variables are type less
            There is no data type like int,char,float
      Variables do not need to be declared
      Every variable is a string
            Variable type is decided at run time
            Depending on the context will be treated as int, float etc.




                                 Sreejith S   Let’s Learn Perl
Variables


      Variables are memory location to store information.
      Variables are type less
            There is no data type like int,char,float
      Variables do not need to be declared
      Every variable is a string
            Variable type is decided at run time
            Depending on the context will be treated as int, float etc.
      There are 4 kinds of variables namely




                                 Sreejith S   Let’s Learn Perl
Variables


      Variables are memory location to store information.
      Variables are type less
            There is no data type like int,char,float
      Variables do not need to be declared
      Every variable is a string
            Variable type is decided at run time
            Depending on the context will be treated as int, float etc.
      There are 4 kinds of variables namely
            Scalars




                                 Sreejith S   Let’s Learn Perl
Variables


      Variables are memory location to store information.
      Variables are type less
            There is no data type like int,char,float
      Variables do not need to be declared
      Every variable is a string
            Variable type is decided at run time
            Depending on the context will be treated as int, float etc.
      There are 4 kinds of variables namely
            Scalars
            Lists




                                 Sreejith S   Let’s Learn Perl
Variables


      Variables are memory location to store information.
      Variables are type less
            There is no data type like int,char,float
      Variables do not need to be declared
      Every variable is a string
            Variable type is decided at run time
            Depending on the context will be treated as int, float etc.
      There are 4 kinds of variables namely
            Scalars
            Lists
            Arrays




                                 Sreejith S   Let’s Learn Perl
Variables


      Variables are memory location to store information.
      Variables are type less
            There is no data type like int,char,float
      Variables do not need to be declared
      Every variable is a string
            Variable type is decided at run time
            Depending on the context will be treated as int, float etc.
      There are 4 kinds of variables namely
            Scalars
            Lists
            Arrays
            Hashes.




                                 Sreejith S   Let’s Learn Perl
Scalars


      Scalar meaning single value




                              Sreejith S   Let’s Learn Perl
Scalars


      Scalar meaning single value
      Name of scalar variable must begin with $ symbol.




                             Sreejith S   Let’s Learn Perl
Scalars


      Scalar meaning single value
      Name of scalar variable must begin with $ symbol.
          next character must a letter or




                               Sreejith S   Let’s Learn Perl
Scalars


      Scalar meaning single value
      Name of scalar variable must begin with $ symbol.
          next character must a letter or
          remaining characters: letters, numbers, or




                               Sreejith S   Let’s Learn Perl
Scalars


      Scalar meaning single value
      Name of scalar variable must begin with $ symbol.
          next character must a letter or
          remaining characters: letters, numbers, or
          name can be up to 255 characters long




                               Sreejith S   Let’s Learn Perl
Scalars


      Scalar meaning single value
      Name of scalar variable must begin with $ symbol.
          next character must a letter or
          remaining characters: letters, numbers, or
          name can be up to 255 characters long
      Right




                               Sreejith S   Let’s Learn Perl
Scalars


      Scalar meaning single value
      Name of scalar variable must begin with $ symbol.
          next character must a letter or
          remaining characters: letters, numbers, or
          name can be up to 255 characters long
      Right
          $foo, $a, $zebra1, $F87dr df3




                               Sreejith S   Let’s Learn Perl
Scalars


      Scalar meaning single value
      Name of scalar variable must begin with $ symbol.
          next character must a letter or
          remaining characters: letters, numbers, or
          name can be up to 255 characters long
      Right
          $foo, $a, $zebra1, $F87dr df3
      Wrong




                               Sreejith S   Let’s Learn Perl
Scalars


      Scalar meaning single value
      Name of scalar variable must begin with $ symbol.
          next character must a letter or
          remaining characters: letters, numbers, or
          name can be up to 255 characters long
      Right
          $foo, $a, $zebra1, $F87dr df3
      Wrong
          $24da, $hi&bye, $bar$foo




                               Sreejith S   Let’s Learn Perl
Scalars


      Scalar meaning single value
      Name of scalar variable must begin with $ symbol.
          next character must a letter or
          remaining characters: letters, numbers, or
          name can be up to 255 characters long
      Right
          $foo, $a, $zebra1, $F87dr df3
      Wrong
          $24da, $hi&bye, $bar$foo
      Examples




                               Sreejith S   Let’s Learn Perl
Scalars


      Scalar meaning single value
      Name of scalar variable must begin with $ symbol.
          next character must a letter or
          remaining characters: letters, numbers, or
          name can be up to 255 characters long
      Right
          $foo, $a, $zebra1, $F87dr df3
      Wrong
          $24da, $hi&bye, $bar$foo
      Examples
          $name=”ram”;




                               Sreejith S   Let’s Learn Perl
Scalars


      Scalar meaning single value
      Name of scalar variable must begin with $ symbol.
          next character must a letter or
          remaining characters: letters, numbers, or
          name can be up to 255 characters long
      Right
          $foo, $a, $zebra1, $F87dr df3
      Wrong
          $24da, $hi&bye, $bar$foo
      Examples
          $name=”ram”; # in string context




                               Sreejith S   Let’s Learn Perl
Scalars


      Scalar meaning single value
      Name of scalar variable must begin with $ symbol.
          next character must a letter or
          remaining characters: letters, numbers, or
          name can be up to 255 characters long
      Right
          $foo, $a, $zebra1, $F87dr df3
      Wrong
          $24da, $hi&bye, $bar$foo
      Examples
          $name=”ram”; # in string context
          $age=30;




                               Sreejith S   Let’s Learn Perl
Scalars


      Scalar meaning single value
      Name of scalar variable must begin with $ symbol.
          next character must a letter or
          remaining characters: letters, numbers, or
          name can be up to 255 characters long
      Right
          $foo, $a, $zebra1, $F87dr df3
      Wrong
          $24da, $hi&bye, $bar$foo
      Examples
          $name=”ram”; # in string context
          $age=30; # in numerical context




                               Sreejith S   Let’s Learn Perl
Scalars


      Scalar meaning single value
      Name of scalar variable must begin with $ symbol.
          next character must a letter or
          remaining characters: letters, numbers, or
          name can be up to 255 characters long
      Right
          $foo, $a, $zebra1, $F87dr df3
      Wrong
          $24da, $hi&bye, $bar$foo
      Examples
          $name=”ram”; # in string context
          $age=30; # in numerical context
          $value = 4.43;




                               Sreejith S   Let’s Learn Perl
Scalars


      Scalar meaning single value
      Name of scalar variable must begin with $ symbol.
          next character must a letter or
          remaining characters: letters, numbers, or
          name can be up to 255 characters long
      Right
          $foo, $a, $zebra1, $F87dr df3
      Wrong
          $24da, $hi&bye, $bar$foo
      Examples
          $name=”ram”; # in string context
          $age=30; # in numerical context
          $value = 4.43; # in float context




                               Sreejith S   Let’s Learn Perl
Scalars


      Scalar meaning single value
      Name of scalar variable must begin with $ symbol.
          next character must a letter or
          remaining characters: letters, numbers, or
          name can be up to 255 characters long
      Right
          $foo, $a, $zebra1, $F87dr df3
      Wrong
          $24da, $hi&bye, $bar$foo
      Examples
          $name=”ram”; # in string context
          $age=30; # in numerical context
          $value = 4.43; # in float context
          $foo = Z;




                               Sreejith S   Let’s Learn Perl
Scalars


      Scalar meaning single value
      Name of scalar variable must begin with $ symbol.
          next character must a letter or
          remaining characters: letters, numbers, or
          name can be up to 255 characters long
      Right
          $foo, $a, $zebra1, $F87dr df3
      Wrong
          $24da, $hi&bye, $bar$foo
      Examples
          $name=”ram”; # in string context
          $age=30; # in numerical context
          $value = 4.43; # in float context
          $foo = Z; # in char context




                               Sreejith S   Let’s Learn Perl
Variable & Character Interpolation


     Perl looks for variables inside strings and replaces them with their
     value




                              Sreejith S   Let’s Learn Perl
Variable & Character Interpolation


     Perl looks for variables inside strings and replaces them with their
     value
        $name = "Sree";
        print "$name is my namen";




                              Sreejith S   Let’s Learn Perl
Variable & Character Interpolation


     Perl looks for variables inside strings and replaces them with their
     value
        $name = "Sree";
        print "$name is my namen";

     This does not happen when you use single quotes




                              Sreejith S   Let’s Learn Perl
Variable & Character Interpolation


     Perl looks for variables inside strings and replaces them with their
     value
        $name = "Sree";
        print "$name is my namen";

     This does not happen when you use single quotes
         print ’$name is my name’; and it will print




                              Sreejith S   Let’s Learn Perl
Variable & Character Interpolation


     Perl looks for variables inside strings and replaces them with their
     value
        $name = "Sree";
        print "$name is my namen";

     This does not happen when you use single quotes
         print ’$name is my name’; and it will print
         $name is my name




                              Sreejith S   Let’s Learn Perl
Variable & Character Interpolation


     Perl looks for variables inside strings and replaces them with their
     value
        $name = "Sree";
        print "$name is my namen";

     This does not happen when you use single quotes
         print ’$name is my name’; and it will print
         $name is my name
     List of character escapes that are recognized when using double
     quoted strings
         All escape characters are recognized




                              Sreejith S   Let’s Learn Perl
Variable & Character Interpolation


     Perl looks for variables inside strings and replaces them with their
     value
        $name = "Sree";
        print "$name is my namen";

     This does not happen when you use single quotes
         print ’$name is my name’; and it will print
         $name is my name
     List of character escapes that are recognized when using double
     quoted strings
         All escape characters are recognized
           print "Hello World!n";
           # It prints Hello World and then a return




                              Sreejith S   Let’s Learn Perl
Lists


        List variables are denoted by symbol ‘()‘




                                 Sreejith S   Let’s Learn Perl
Lists


        List variables are denoted by symbol ‘()‘
        List is just a list of values may be constants, scalars etc




                                 Sreejith S   Let’s Learn Perl
Lists


        List variables are denoted by symbol ‘()‘
        List is just a list of values may be constants, scalars etc
          (a,b,c) or ($name,$age,$sex)




                                 Sreejith S   Let’s Learn Perl
Lists


        List variables are denoted by symbol ‘()‘
        List is just a list of values may be constants, scalars etc
          (a,b,c) or ($name,$age,$sex)

        The index are specified inside a square bracket ‘[]‘




                                 Sreejith S   Let’s Learn Perl
Lists


        List variables are denoted by symbol ‘()‘
        List is just a list of values may be constants, scalars etc
          (a,b,c) or ($name,$age,$sex)

        The index are specified inside a square bracket ‘[]‘
        $first=(a,b,c)[0];
        print "$firstn";




                                 Sreejith S   Let’s Learn Perl
Lists


        List variables are denoted by symbol ‘()‘
        List is just a list of values may be constants, scalars etc
          (a,b,c) or ($name,$age,$sex)

        The index are specified inside a square bracket ‘[]‘
        $first=(a,b,c)[0];
        print "$firstn";

        List variables can be assigned like this




                                 Sreejith S   Let’s Learn Perl
Lists


        List variables are denoted by symbol ‘()‘
        List is just a list of values may be constants, scalars etc
          (a,b,c) or ($name,$age,$sex)

        The index are specified inside a square bracket ‘[]‘
        $first=(a,b,c)[0];
        print "$firstn";

        List variables can be assigned like this
        ($name,$age)=(’Raman’,20);
        print "name=$namen";




                                 Sreejith S   Let’s Learn Perl
Arrays


     Array variables should have prefix ‘@‘




                             Sreejith S   Let’s Learn Perl
Arrays


     Array variables should have prefix ‘@‘
     Arrays are used to store ordered values




                             Sreejith S   Let’s Learn Perl
Arrays


     Array variables should have prefix ‘@‘
     Arrays are used to store ordered values
     Much more dynamic than C/C++




                             Sreejith S   Let’s Learn Perl
Arrays


     Array variables should have prefix ‘@‘
     Arrays are used to store ordered values
     Much more dynamic than C/C++
         no declaration of size, type




                               Sreejith S   Let’s Learn Perl
Arrays


     Array variables should have prefix ‘@‘
     Arrays are used to store ordered values
     Much more dynamic than C/C++
         no declaration of size, type
         can hold any kind of value, and multiple kinds of values




                              Sreejith S   Let’s Learn Perl
Arrays


     Array variables should have prefix ‘@‘
     Arrays are used to store ordered values
     Much more dynamic than C/C++
         no declaration of size, type
         can hold any kind of value, and multiple kinds of values
     Array Indexes start at 0




                                Sreejith S   Let’s Learn Perl
Arrays


     Array variables should have prefix ‘@‘
     Arrays are used to store ordered values
     Much more dynamic than C/C++
          no declaration of size, type
          can hold any kind of value, and multiple kinds of values
     Array Indexes start at 0
         @array=(1,2,3);

     To access the whole array, use the whole array




                                Sreejith S   Let’s Learn Perl
Arrays


     Array variables should have prefix ‘@‘
     Arrays are used to store ordered values
     Much more dynamic than C/C++
          no declaration of size, type
          can hold any kind of value, and multiple kinds of values
     Array Indexes start at 0
         @array=(1,2,3);

     To access the whole array, use the whole array
          print @array;




                                Sreejith S   Let’s Learn Perl
Arrays


     Array variables should have prefix ‘@‘
     Arrays are used to store ordered values
     Much more dynamic than C/C++
          no declaration of size, type
          can hold any kind of value, and multiple kinds of values
     Array Indexes start at 0
         @array=(1,2,3);

     To access the whole array, use the whole array
          print @array;
     To access one element of the array : use $




                                Sreejith S   Let’s Learn Perl
Arrays


     Array variables should have prefix ‘@‘
     Arrays are used to store ordered values
     Much more dynamic than C/C++
          no declaration of size, type
          can hold any kind of value, and multiple kinds of values
     Array Indexes start at 0
         @array=(1,2,3);

     To access the whole array, use the whole array
          print @array;
     To access one element of the array : use $
         print $array[0]; # prints : 1




                                Sreejith S   Let’s Learn Perl
Operations on Arrays
     To append to the end of an array : PUSH




                           Sreejith S   Let’s Learn Perl
Operations on Arrays
     To append to the end of an array : PUSH
       @array=(1,2,3);
       push @array,’4’;
       print "@array n";




                           Sreejith S   Let’s Learn Perl
Operations on Arrays
     To append to the end of an array : PUSH
       @array=(1,2,3);
       push @array,’4’;
       print "@array n";

     To remove the last element of the array : POP




                            Sreejith S   Let’s Learn Perl
Operations on Arrays
     To append to the end of an array : PUSH
       @array=(1,2,3);
       push @array,’4’;
       print "@array n";

     To remove the last element of the array : POP
       @array=(1,2,3);
       $last=pop @array;
       print "last=$lastn";




                            Sreejith S   Let’s Learn Perl
Operations on Arrays
     To append to the end of an array : PUSH
       @array=(1,2,3);
       push @array,’4’;
       print "@array n";

     To remove the last element of the array : POP
       @array=(1,2,3);
       $last=pop @array;
       print "last=$lastn";

     To prepend to the beginning of an array : UNSHIFT




                            Sreejith S   Let’s Learn Perl
Operations on Arrays
     To append to the end of an array : PUSH
       @array=(1,2,3);
       push @array,’4’;
       print "@array n";

     To remove the last element of the array : POP
       @array=(1,2,3);
       $last=pop @array;
       print "last=$lastn";

     To prepend to the beginning of an array : UNSHIFT
       @array=(3,4,5);
       unshift @array,’2’;
       print "array=@arrayn";




                            Sreejith S   Let’s Learn Perl
Operations on Arrays
     To append to the end of an array : PUSH
       @array=(1,2,3);
       push @array,’4’;
       print "@array n";

     To remove the last element of the array : POP
       @array=(1,2,3);
       $last=pop @array;
       print "last=$lastn";

     To prepend to the beginning of an array : UNSHIFT
       @array=(3,4,5);
       unshift @array,’2’;
       print "array=@arrayn";

     To remove the first element of the array : SHIFT



                            Sreejith S   Let’s Learn Perl
Operations on Arrays
     To append to the end of an array : PUSH
       @array=(1,2,3);
       push @array,’4’;
       print "@array n";

     To remove the last element of the array : POP
       @array=(1,2,3);
       $last=pop @array;
       print "last=$lastn";

     To prepend to the beginning of an array : UNSHIFT
       @array=(3,4,5);
       unshift @array,’2’;
       print "array=@arrayn";

     To remove the first element of the array : SHIFT
       @array=(3,4,5);
       unshift @array,’2’;
       print "array=@arrayn";
                            Sreejith S   Let’s Learn Perl
Operations on Arrays
     To append to the end of an array : PUSH
       @array=(1,2,3);
       push @array,’4’;
       print "@array n";

     To remove the last element of the array : POP
       @array=(1,2,3);
       $last=pop @array;
       print "last=$lastn";

     To prepend to the beginning of an array : UNSHIFT
       @array=(3,4,5);
       unshift @array,’2’;
       print "array=@arrayn";

     To remove the first element of the array : SHIFT
       @array=(3,4,5);
       unshift @array,’2’;
       print "array=@arrayn";
                            Sreejith S   Let’s Learn Perl
Operations on Arrays
     To append to the end of an array : PUSH
       @array=(1,2,3);
       push @array,’4’;
       print "@array n";

     To remove the last element of the array : POP
       @array=(1,2,3);
       $last=pop @array;
       print "last=$lastn";

     To prepend to the beginning of an array : UNSHIFT
       @array=(3,4,5);
       unshift @array,’2’;
       print "array=@arrayn";

     To remove the first element of the array : SHIFT
       @array=(3,4,5);
       unshift @array,’2’;
       print "array=@arrayn";
                            Sreejith S   Let’s Learn Perl
Operations on Arrays
     To append to the end of an array : PUSH
       @array=(1,2,3);
       push @array,’4’;
       print "@array n";

     To remove the last element of the array : POP
       @array=(1,2,3);
       $last=pop @array;
       print "last=$lastn";

     To prepend to the beginning of an array : UNSHIFT
       @array=(3,4,5);
       unshift @array,’2’;
       print "array=@arrayn";

     To remove the first element of the array : SHIFT
       @array=(3,4,5);
       unshift @array,’2’;
       print "array=@arrayn";
                            Sreejith S   Let’s Learn Perl
Operations on Arrays
     To append to the end of an array : PUSH
       @array=(1,2,3);
       push @array,’4’;
       print "@array n";

     To remove the last element of the array : POP
       @array=(1,2,3);
       $last=pop @array;
       print "last=$lastn";

     To prepend to the beginning of an array : UNSHIFT
       @array=(3,4,5);
       unshift @array,’2’;
       print "array=@arrayn";

     To remove the first element of the array : SHIFT
       @array=(3,4,5);
       unshift @array,’2’;
       print "array=@arrayn";
                            Sreejith S   Let’s Learn Perl
Operations on Arrays Contind...




     $#array is a special variable containing index of last element.




                              Sreejith S   Let’s Learn Perl
Operations on Arrays Contind...




     $#array is a special variable containing index of last element.
         print $#array; # it will print 2




                               Sreejith S   Let’s Learn Perl
Operations on Arrays Contind...




     $#array is a special variable containing index of last element.
         print $#array; # it will print 2
     Another way to find the number of elements in the array:




                               Sreejith S   Let’s Learn Perl
Operations on Arrays Contind...




     $#array is a special variable containing index of last element.
         print $#array; # it will print 2
     Another way to find the number of elements in the array:
      $array_size = @array;
      scalar(@array) is a function to return the size of array




                               Sreejith S   Let’s Learn Perl
Operations on Arrays Contind...




     $#array is a special variable containing index of last element.
         print $#array; # it will print 2
     Another way to find the number of elements in the array:
      $array_size = @array;
      scalar(@array) is a function to return the size of array




                               Sreejith S   Let’s Learn Perl
Hashes


     Hash variables will have % as prefix




                             Sreejith S   Let’s Learn Perl
Hashes


     Hash variables will have % as prefix
     Key-Value pair




                             Sreejith S   Let’s Learn Perl
Hashes


     Hash variables will have % as prefix
     Key-Value pair
     The contents of hash are called values and index is called key. the
     array:




                             Sreejith S   Let’s Learn Perl
Hashes


     Hash variables will have % as prefix
     Key-Value pair
     The contents of hash are called values and index is called key. the
     array:
         %fruits= ( ’apple’=>’red’,’banana’=>’yellow’,
         ’grape’ =>’black’);
         print "colour of apple =$fruits{apple}n";




                             Sreejith S   Let’s Learn Perl
Hashes


     Hash variables will have % as prefix
     Key-Value pair
     The contents of hash are called values and index is called key. the
     array:
         %fruits= ( ’apple’=>’red’,’banana’=>’yellow’,
         ’grape’ =>’black’);
         print "colour of apple =$fruits{apple}n";
     Other way of populating a hash




                             Sreejith S   Let’s Learn Perl
Hashes


     Hash variables will have % as prefix
     Key-Value pair
     The contents of hash are called values and index is called key. the
     array:
         %fruits= ( ’apple’=>’red’,’banana’=>’yellow’,
         ’grape’ =>’black’);
         print "colour of apple =$fruits{apple}n";
     Other way of populating a hash
         %fruits =(’apple’,’red’,’banana’,’yellow’,
         ’grape’,’black’);
         print "colour of banana =$fruits{banana}n";




                             Sreejith S   Let’s Learn Perl
Hashes


     Hash variables will have % as prefix
     Key-Value pair
     The contents of hash are called values and index is called key. the
     array:
         %fruits= ( ’apple’=>’red’,’banana’=>’yellow’,
         ’grape’ =>’black’);
         print "colour of apple =$fruits{apple}n";
     Other way of populating a hash
         %fruits =(’apple’,’red’,’banana’,’yellow’,
         ’grape’,’black’);
         print "colour of banana =$fruits{banana}n";
     Note the {} instead of [ ] as in the case of array;




                              Sreejith S   Let’s Learn Perl
Conditionals - IF



      The if statement is similar to if in C language, except




                               Sreejith S   Let’s Learn Perl
Conditionals - IF



      The if statement is similar to if in C language, except
          flower brace is required even for single statement
          else if is noted by elsif (note missing e)




                               Sreejith S   Let’s Learn Perl
Conditionals - IF



      The if statement is similar to if in C language, except
          flower brace is required even for single statement
          else if is noted by elsif (note missing e)
      The syntax of if statement is




                               Sreejith S   Let’s Learn Perl
Conditionals - IF



      The if statement is similar to if in C language, except
          flower brace is required even for single statement
          else if is noted by elsif (note missing e)
      The syntax of if statement is
        if (condition) {
        }
        elsif (condition){
        }
        else {
        }




                               Sreejith S   Let’s Learn Perl
Control Structures - Loops



     Perl supports four main loop types




                             Sreejith S   Let’s Learn Perl
Control Structures - Loops



     Perl supports four main loop types
         while, until, for, and foreach




                               Sreejith S   Let’s Learn Perl
Control Structures - Loops



     Perl supports four main loop types
         while, until, for, and foreach
     while loop is used to iterate and has syntax similar to C.




                               Sreejith S   Let’s Learn Perl
Control Structures - Loops



     Perl supports four main loop types
         while, until, for, and foreach
     while loop is used to iterate and has syntax similar to C.
       $i = 0;
       while ( $i <= 1000 )
       {
       print $in;
       $i++;
       }




                               Sreejith S   Let’s Learn Perl
Control Structures - Until Loop




     The until function evaluates an expression repeatedly until a specific
     condition is met




                             Sreejith S   Let’s Learn Perl
Control Structures - Until Loop




     The until function evaluates an expression repeatedly until a specific
     condition is met
       $i = 0;
       until ($i == 1000)
       {
       print $in;
       $i++;
       }




                             Sreejith S   Let’s Learn Perl
Control Structures - For Loop


     For loop syntax is similar to c.




                              Sreejith S   Let’s Learn Perl
Control Structures - For Loop


     For loop syntax is similar to c.
     There are variations




                              Sreejith S   Let’s Learn Perl
Control Structures - For Loop


     For loop syntax is similar to c.
     There are variations
     Syntax 1:




                              Sreejith S   Let’s Learn Perl
Control Structures - For Loop


     For loop syntax is similar to c.
     There are variations
     Syntax 1:
       for ( $i = 0; $i <= 1000; $i=$i+2 )
       {
       print $i;
       }




                              Sreejith S   Let’s Learn Perl
Control Structures - For Loop


     For loop syntax is similar to c.
     There are variations
     Syntax 1:
       for ( $i = 0; $i <= 1000; $i=$i+2 )
       {
       print $i;
       }
     Syntax 2:




                              Sreejith S   Let’s Learn Perl
Control Structures - For Loop


     For loop syntax is similar to c.
     There are variations
     Syntax 1:
       for ( $i = 0; $i <= 1000; $i=$i+2 )
       {
       print $i;
       }
     Syntax 2:
      for $i(0..1000)
      {
       print $i;
      }




                              Sreejith S   Let’s Learn Perl
Control Structures - ForEach Loop




     Syntax 3:




                      Sreejith S   Let’s Learn Perl
Control Structures - ForEach Loop




     Syntax 3:
     foreach $i (a,b,c)
     {
     print uc $i;
     }




                          Sreejith S   Let’s Learn Perl
Default scalar variable




      $ is called default variable




                               Sreejith S   Let’s Learn Perl
Default scalar variable




      $ is called default variable
          It will be used if no other variable is specified
        foreach (a,b,c){
        print uc ;
        }




                                Sreejith S   Let’s Learn Perl
Accepting input




     Keyboard inputs can be accepted using STDIN




                           Sreejith S   Let’s Learn Perl
Accepting input




     Keyboard inputs can be accepted using STDIN
       print "enter your name ";
       $name=<STDIN>;
       chomp $name;
       # chomp function is similar to fflush in C.
       # It removes trailing newline if any
       print "Welcome $namen";




                           Sreejith S   Let’s Learn Perl
Subroutines




     Subroutines can be defined using sub keyword




                           Sreejith S   Let’s Learn Perl
Subroutines




     Subroutines can be defined using sub keyword
         ’@ ’ default array
       $v1=10;$v2=20;
       add($v1,$v2);
       sub add {
       ($a,$b)=@_;
       print $a+$b;
       }




                              Sreejith S   Let’s Learn Perl
Scope of variables




     By default all variables are global




                              Sreejith S   Let’s Learn Perl
Scope of variables




     By default all variables are global
          You can limit scope to a block/sub by using my.
      $v1=10; $v2=30; #v1,v2 global
      my $i = 10;




                              Sreejith S   Let’s Learn Perl
File handling
      File handling can be done very easily




                              Sreejith S   Let’s Learn Perl
File handling
      File handling can be done very easily
      To open a file in read mode




                              Sreejith S   Let’s Learn Perl
File handling
      File handling can be done very easily
      To open a file in read mode
      open( $fh, < , data.txt);




                              Sreejith S   Let’s Learn Perl
File handling
      File handling can be done very easily
      To open a file in read mode
      open( $fh, < , data.txt);
      File reading line by line can be done like




                               Sreejith S   Let’s Learn Perl
File handling
      File handling can be done very easily
      To open a file in read mode
      open( $fh, < , data.txt);
      File reading line by line can be done like
      $line=<$fh>;




                               Sreejith S   Let’s Learn Perl
File handling
      File handling can be done very easily
      To open a file in read mode
      open( $fh, < , data.txt);
      File reading line by line can be done like
      $line=<$fh>;
      File writing can be done using print and filehandle like this
      print $fh "hello";




                               Sreejith S   Let’s Learn Perl
File handling
      File handling can be done very easily
      To open a file in read mode
      open( $fh, < , data.txt);
      File reading line by line can be done like
      $line=<$fh>;
      File writing can be done using print and filehandle like this
      print $fh "hello";
      Example




                               Sreejith S   Let’s Learn Perl
File handling
      File handling can be done very easily
      To open a file in read mode
      open( $fh, < , data.txt);
      File reading line by line can be done like
      $line=<$fh>;
      File writing can be done using print and filehandle like this
      print $fh "hello";
      Example
       open ( $fh, "<", "data.txt" ); #open file read only
       open ($fh1,">","udata.txt");              #Open file write mode
       while ( $line = <$fh>; ) {                #read line by
         print "line=$line";                     #display content on screen
         print $fh1 uc($line);                   #write upper cased content t
         }
         close($fh);
         close($fh1);

                            Sreejith S   Let’s Learn Perl
Some Common String Functions


     There are many built in string functions




                             Sreejith S   Let’s Learn Perl
Some Common String Functions


     There are many built in string functions
         Convert to upper case




                             Sreejith S   Let’s Learn Perl
Some Common String Functions


     There are many built in string functions
         Convert to upper case
         $name = uc($name);




                             Sreejith S   Let’s Learn Perl
Some Common String Functions


     There are many built in string functions
         Convert to upper case
         $name = uc($name);
     Convert only the first char to upper case




                             Sreejith S   Let’s Learn Perl
Some Common String Functions


     There are many built in string functions
         Convert to upper case
         $name = uc($name);
     Convert only the first char to upper case
      $name = ucfirst($name);




                             Sreejith S   Let’s Learn Perl
Some Common String Functions


     There are many built in string functions
         Convert to upper case
         $name = uc($name);
     Convert only the first char to upper case
      $name = ucfirst($name);
     Convert to lower case




                             Sreejith S   Let’s Learn Perl
Some Common String Functions


     There are many built in string functions
         Convert to upper case
         $name = uc($name);
     Convert only the first char to upper case
      $name = ucfirst($name);
     Convert to lower case
      $name = lc($name);




                             Sreejith S   Let’s Learn Perl
Some Common String Functions


     There are many built in string functions
         Convert to upper case
         $name = uc($name);
     Convert only the first char to upper case
      $name = ucfirst($name);
     Convert to lower case
      $name = lc($name);
     Convert only the first char to lower case




                             Sreejith S   Let’s Learn Perl
Some Common String Functions


     There are many built in string functions
         Convert to upper case
         $name = uc($name);
     Convert only the first char to upper case
      $name = ucfirst($name);
     Convert to lower case
      $name = lc($name);
     Convert only the first char to lower case
     $name = lcfirst($name);




                             Sreejith S   Let’s Learn Perl
Some Common String Functions


     There are many built in string functions
         Convert to upper case
         $name = uc($name);
     Convert only the first char to upper case
      $name = ucfirst($name);
     Convert to lower case
      $name = lc($name);
     Convert only the first char to lower case
     $name = lcfirst($name);
     Concatenation: ’.’ similar to strcat




                              Sreejith S   Let’s Learn Perl
Some Common String Functions


     There are many built in string functions
         Convert to upper case
         $name = uc($name);
     Convert only the first char to upper case
      $name = ucfirst($name);
     Convert to lower case
      $name = lc($name);
     Convert only the first char to lower case
     $name = lcfirst($name);
     Concatenation: ’.’ similar to strcat
      $first_name = Larry;
      $last_name = Wall;
      $full_name = $first_name .                 . $last_name;



                              Sreejith S   Let’s Learn Perl
Some Common Functions


    There are many built in functions




                            Sreejith S   Let’s Learn Perl
Some Common Functions


    There are many built in functions
    Strings to Arrays : split




                            Sreejith S   Let’s Learn Perl
Some Common Functions


    There are many built in functions
    Strings to Arrays : split
     @array = split( /;/, "Larry;Curly;Moe" );
     @array= ("Larry", "Curly", "Moe");




                            Sreejith S   Let’s Learn Perl
Some Common Functions


    There are many built in functions
    Strings to Arrays : split
     @array = split( /;/, "Larry;Curly;Moe" );
     @array= ("Larry", "Curly", "Moe");
    Split into characters




                            Sreejith S   Let’s Learn Perl
Some Common Functions


    There are many built in functions
    Strings to Arrays : split
     @array = split( /;/, "Larry;Curly;Moe" );
     @array= ("Larry", "Curly", "Moe");
    Split into characters
     @stooge = split( //, "curly" );
     # array @stooge has 5 elements: c, u, r, l, y




                            Sreejith S   Let’s Learn Perl
Some Common Functions


    There are many built in functions
    Strings to Arrays : split
     @array = split( /;/, "Larry;Curly;Moe" );
     @array= ("Larry", "Curly", "Moe");
    Split into characters
     @stooge = split( //, "curly" );
     # array @stooge has 5 elements: c, u, r, l, y
    Join with any character you want




                            Sreejith S   Let’s Learn Perl
Some Common Functions


    There are many built in functions
    Strings to Arrays : split
     @array = split( /;/, "Larry;Curly;Moe" );
     @array= ("Larry", "Curly", "Moe");
    Split into characters
     @stooge = split( //, "curly" );
     # array @stooge has 5 elements: c, u, r, l, y
    Join with any character you want
    @array = 10, 20, 30, 40);
    $string = join(->, @array);
    # string = 10->20->30->40




                            Sreejith S   Let’s Learn Perl
Web Resource for Perl




     Web resources for Perl




                              Sreejith S   Let’s Learn Perl
Web Resource for Perl




     Web resources for Perl
         www.perl.com
         www.perl.org
         www.perlmonks.org




                              Sreejith S   Let’s Learn Perl
Recommended Reading




                      Sreejith S   Let’s Learn Perl
Questions




            Sreejith S   Let’s Learn Perl
Thank You




            Sreejith S   Let’s Learn Perl

More Related Content

Viewers also liked (14)

Machine Learning with Python
Machine Learning with PythonMachine Learning with Python
Machine Learning with Python
 
DPG_Talk_March2011_AlexandraM_Liguori
DPG_Talk_March2011_AlexandraM_LiguoriDPG_Talk_March2011_AlexandraM_Liguori
DPG_Talk_March2011_AlexandraM_Liguori
 
NLP_session-3_Alexandra
NLP_session-3_AlexandraNLP_session-3_Alexandra
NLP_session-3_Alexandra
 
PHD_Final_exam_AlexandraM_Liguori
PHD_Final_exam_AlexandraM_LiguoriPHD_Final_exam_AlexandraM_Liguori
PHD_Final_exam_AlexandraM_Liguori
 
Introduction to NLTK
Introduction to NLTKIntroduction to NLTK
Introduction to NLTK
 
QuantumBiology_AlexandraM_Liguori
QuantumBiology_AlexandraM_LiguoriQuantumBiology_AlexandraM_Liguori
QuantumBiology_AlexandraM_Liguori
 
Benasque_Sept2010_AlexandraM_Liguori
Benasque_Sept2010_AlexandraM_LiguoriBenasque_Sept2010_AlexandraM_Liguori
Benasque_Sept2010_AlexandraM_Liguori
 
Quantum_Mechanics_Intro_AlexandraM_Liguori
Quantum_Mechanics_Intro_AlexandraM_LiguoriQuantum_Mechanics_Intro_AlexandraM_Liguori
Quantum_Mechanics_Intro_AlexandraM_Liguori
 
NLP_session-1
NLP_session-1NLP_session-1
NLP_session-1
 
Methods in Unsupervised Dependency Parsing
Methods in Unsupervised Dependency ParsingMethods in Unsupervised Dependency Parsing
Methods in Unsupervised Dependency Parsing
 
NLP_session-2
NLP_session-2NLP_session-2
NLP_session-2
 
AlexandraLiguori_CogniCor_Talk_UPC
AlexandraLiguori_CogniCor_Talk_UPCAlexandraLiguori_CogniCor_Talk_UPC
AlexandraLiguori_CogniCor_Talk_UPC
 
NLP_lectures_English
NLP_lectures_EnglishNLP_lectures_English
NLP_lectures_English
 
Enabling Exploratory Analysis of Large Data with Apache Spark and R
Enabling Exploratory Analysis of Large Data with Apache Spark and REnabling Exploratory Analysis of Large Data with Apache Spark and R
Enabling Exploratory Analysis of Large Data with Apache Spark and R
 

Similar to Perl

Introduction to writing readable and maintainable Perl
Introduction to writing readable and maintainable PerlIntroduction to writing readable and maintainable Perl
Introduction to writing readable and maintainable PerlAlex Balhatchet
 
Was können wir von Rebol lernen?
Was können wir von Rebol lernen?Was können wir von Rebol lernen?
Was können wir von Rebol lernen?lichtkind
 
Unit 1-introduction to perl
Unit 1-introduction to perlUnit 1-introduction to perl
Unit 1-introduction to perlsana mateen
 
Mastering Regex in Perl
Mastering Regex in PerlMastering Regex in Perl
Mastering Regex in PerlEdureka!
 
Bioinformatica 29-09-2011-p1-introduction
Bioinformatica 29-09-2011-p1-introductionBioinformatica 29-09-2011-p1-introduction
Bioinformatica 29-09-2011-p1-introductionProf. Wim Van Criekinge
 
Introduction to Writing Readable and Maintainable Perl (YAPC::EU 2011 Version)
Introduction to Writing Readable and Maintainable Perl (YAPC::EU 2011 Version)Introduction to Writing Readable and Maintainable Perl (YAPC::EU 2011 Version)
Introduction to Writing Readable and Maintainable Perl (YAPC::EU 2011 Version)Alex Balhatchet
 
The *on-going* future of Perl5
The *on-going* future of Perl5The *on-going* future of Perl5
The *on-going* future of Perl5Vytautas Dauksa
 
PHP: The easiest language to learn.
PHP: The easiest language to learn.PHP: The easiest language to learn.
PHP: The easiest language to learn.Binny V A
 
Le PERL est mort
Le PERL est mortLe PERL est mort
Le PERL est mortapeiron
 
Using Erlang in an Embedded and Cross-Compiled World
Using Erlang in an Embedded and Cross-Compiled WorldUsing Erlang in an Embedded and Cross-Compiled World
Using Erlang in an Embedded and Cross-Compiled WorldFrank Hunleth
 
Introduction to perl
Introduction to perlIntroduction to perl
Introduction to perlsana mateen
 
What's new in Perl 5.10?
What's new in Perl 5.10?What's new in Perl 5.10?
What's new in Perl 5.10?acme
 

Similar to Perl (20)

Introduction to Perl
Introduction to PerlIntroduction to Perl
Introduction to Perl
 
Introduction to writing readable and maintainable Perl
Introduction to writing readable and maintainable PerlIntroduction to writing readable and maintainable Perl
Introduction to writing readable and maintainable Perl
 
Perl Moderno
Perl ModernoPerl Moderno
Perl Moderno
 
Was können wir von Rebol lernen?
Was können wir von Rebol lernen?Was können wir von Rebol lernen?
Was können wir von Rebol lernen?
 
Unit 1-introduction to perl
Unit 1-introduction to perlUnit 1-introduction to perl
Unit 1-introduction to perl
 
Mastering Regex in Perl
Mastering Regex in PerlMastering Regex in Perl
Mastering Regex in Perl
 
Php, mysq lpart1
Php, mysq lpart1Php, mysq lpart1
Php, mysq lpart1
 
Python overview
Python overviewPython overview
Python overview
 
Bioinformatica 29-09-2011-p1-introduction
Bioinformatica 29-09-2011-p1-introductionBioinformatica 29-09-2011-p1-introduction
Bioinformatica 29-09-2011-p1-introduction
 
Introduction to Writing Readable and Maintainable Perl (YAPC::EU 2011 Version)
Introduction to Writing Readable and Maintainable Perl (YAPC::EU 2011 Version)Introduction to Writing Readable and Maintainable Perl (YAPC::EU 2011 Version)
Introduction to Writing Readable and Maintainable Perl (YAPC::EU 2011 Version)
 
Ruby
RubyRuby
Ruby
 
The *on-going* future of Perl5
The *on-going* future of Perl5The *on-going* future of Perl5
The *on-going* future of Perl5
 
PHP: The easiest language to learn.
PHP: The easiest language to learn.PHP: The easiest language to learn.
PHP: The easiest language to learn.
 
Using PHP
Using PHPUsing PHP
Using PHP
 
Le PERL est mort
Le PERL est mortLe PERL est mort
Le PERL est mort
 
Using Erlang in an Embedded and Cross-Compiled World
Using Erlang in an Embedded and Cross-Compiled WorldUsing Erlang in an Embedded and Cross-Compiled World
Using Erlang in an Embedded and Cross-Compiled World
 
Starting Out With PHP
Starting Out With PHPStarting Out With PHP
Starting Out With PHP
 
Introduction to perl
Introduction to perlIntroduction to perl
Introduction to perl
 
What's new in Perl 5.10?
What's new in Perl 5.10?What's new in Perl 5.10?
What's new in Perl 5.10?
 
ppt7
ppt7ppt7
ppt7
 

Recently uploaded

FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraDeakin University
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxnull - The Open Security Community
 
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024BookNet Canada
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphNeo4j
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Unlocking the Potential of the Cloud for IBM Power Systems
Unlocking the Potential of the Cloud for IBM Power SystemsUnlocking the Potential of the Cloud for IBM Power Systems
Unlocking the Potential of the Cloud for IBM Power SystemsPrecisely
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 

Recently uploaded (20)

FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
The transition to renewables in India.pdf
The transition to renewables in India.pdfThe transition to renewables in India.pdf
The transition to renewables in India.pdf
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning era
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
 
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
Unlocking the Potential of the Cloud for IBM Power Systems
Unlocking the Potential of the Cloud for IBM Power SystemsUnlocking the Potential of the Cloud for IBM Power Systems
Unlocking the Potential of the Cloud for IBM Power Systems
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 

Perl

  • 1. Let’s Learn Perl An introduction to Perl Sreejith S NLP Process Analyst 365Media Pvt. Ltd. srssreejith@gmail.com January 22, 2011 Sreejith S Let’s Learn Perl
  • 2. Just a word about me !! Working in Natural Language Processing (NLP), Machine Learning, Data Mining Passionate about Free and Open source :-) Works for 365Media Pvt. Ltd. Coimbatore India. Sreejith S Let’s Learn Perl
  • 3. What’s Perl ?? Practical Extraction and Reporting Language Sreejith S Let’s Learn Perl
  • 4. What’s Perl ?? Practical Extraction and Reporting Language Perl is a Portable Scripting Language Sreejith S Let’s Learn Perl
  • 5. What’s Perl ?? Practical Extraction and Reporting Language Perl is a Portable Scripting Language Fast and easy text processing capability Sreejith S Let’s Learn Perl
  • 6. What’s Perl ?? Practical Extraction and Reporting Language Perl is a Portable Scripting Language Fast and easy text processing capability Fast and easy file handling capability Sreejith S Let’s Learn Perl
  • 7. What’s Perl ?? Practical Extraction and Reporting Language Perl is a Portable Scripting Language Fast and easy text processing capability Fast and easy file handling capability It aims to be practical (easy to use, efficient, complete) rather than beautiful (tiny, elegant, minimal) Sreejith S Let’s Learn Perl
  • 8. What’s Perl ?? Practical Extraction and Reporting Language Perl is a Portable Scripting Language Fast and easy text processing capability Fast and easy file handling capability It aims to be practical (easy to use, efficient, complete) rather than beautiful (tiny, elegant, minimal) Perl was developed by Larry Wall. Sreejith S Let’s Learn Perl
  • 9. To install... Perl Comes by Default on Linux, Cygwin, MacOSX Sreejith S Let’s Learn Perl
  • 10. To install... Perl Comes by Default on Linux, Cygwin, MacOSX www.perl.com has rpm’s for Linux Sreejith S Let’s Learn Perl
  • 11. To install... Perl Comes by Default on Linux, Cygwin, MacOSX www.perl.com has rpm’s for Linux www.activestate.com has binaries for Windows Sreejith S Let’s Learn Perl
  • 12. To install... Perl Comes by Default on Linux, Cygwin, MacOSX www.perl.com has rpm’s for Linux www.activestate.com has binaries for Windows Latest Version is 5.10 Sreejith S Let’s Learn Perl
  • 13. To install... Perl Comes by Default on Linux, Cygwin, MacOSX www.perl.com has rpm’s for Linux www.activestate.com has binaries for Windows Latest Version is 5.10 To check if Perl is working and the version number Sreejith S Let’s Learn Perl
  • 14. To install... Perl Comes by Default on Linux, Cygwin, MacOSX www.perl.com has rpm’s for Linux www.activestate.com has binaries for Windows Latest Version is 5.10 To check if Perl is working and the version number perl -v or perl -version Sreejith S Let’s Learn Perl
  • 15. Sreejith S Let’s Learn Perl
  • 16. Basic Syntax of Perl Comments Sreejith S Let’s Learn Perl
  • 17. Basic Syntax of Perl Comments begin with # Sreejith S Let’s Learn Perl
  • 18. Basic Syntax of Perl Comments begin with # dont try to use // or /* ... */ Sreejith S Let’s Learn Perl
  • 19. Basic Syntax of Perl Comments begin with # dont try to use // or /* ... */ Print Sreejith S Let’s Learn Perl
  • 20. Basic Syntax of Perl Comments begin with # dont try to use // or /* ... */ Print print is simple function to display/output something on monitor/stdout Sreejith S Let’s Learn Perl
  • 21. Basic Syntax of Perl Comments begin with # dont try to use // or /* ... */ Print print is simple function to display/output something on monitor/stdout e.g :- print “hello world“; Sreejith S Let’s Learn Perl
  • 22. Basic Syntax of Perl Comments begin with # dont try to use // or /* ... */ Print print is simple function to display/output something on monitor/stdout e.g :- print “hello world“; Statement terminator ; Sreejith S Let’s Learn Perl
  • 23. Basic Syntax of Perl Comments begin with # dont try to use // or /* ... */ Print print is simple function to display/output something on monitor/stdout e.g :- print “hello world“; Statement terminator ; All Statements end with ; like C language Sreejith S Let’s Learn Perl
  • 24. Basic Syntax of Perl Comments begin with # dont try to use // or /* ... */ Print print is simple function to display/output something on monitor/stdout e.g :- print “hello world“; Statement terminator ; All Statements end with ; like C language Delimiter Sreejith S Let’s Learn Perl
  • 25. Basic Syntax of Perl Comments begin with # dont try to use // or /* ... */ Print print is simple function to display/output something on monitor/stdout e.g :- print “hello world“; Statement terminator ; All Statements end with ; like C language Delimiter {} delimit blocks, loops, subroutines Sreejith S Let’s Learn Perl
  • 26. Basic Syntax of Perl Comments begin with # dont try to use // or /* ... */ Print print is simple function to display/output something on monitor/stdout e.g :- print “hello world“; Statement terminator ; All Statements end with ; like C language Delimiter {} delimit blocks, loops, subroutines No main() function Sreejith S Let’s Learn Perl
  • 27. Basic Syntax of Perl Comments begin with # dont try to use // or /* ... */ Print print is simple function to display/output something on monitor/stdout e.g :- print “hello world“; Statement terminator ; All Statements end with ; like C language Delimiter {} delimit blocks, loops, subroutines No main() function Top-down programming approach Sreejith S Let’s Learn Perl
  • 28. Structure of a Perl Program #! /usr/bin/perl -w Sreejith S Let’s Learn Perl
  • 29. Structure of a Perl Program #! /usr/bin/perl -w It tells where to find Perl Sreejith S Let’s Learn Perl
  • 30. Structure of a Perl Program #! /usr/bin/perl -w It tells where to find Perl .pl extension is optional but is commonly used Sreejith S Let’s Learn Perl
  • 31. Structure of a Perl Program #! /usr/bin/perl -w It tells where to find Perl .pl extension is optional but is commonly used You can write perl programs without extension also. Sreejith S Let’s Learn Perl
  • 32. Structure of a Perl Program #! /usr/bin/perl -w It tells where to find Perl .pl extension is optional but is commonly used You can write perl programs without extension also. -w switches on warning : not required but a really good idea Sreejith S Let’s Learn Perl
  • 33. Customary Hello World Program #! /usr/bin/perl -w print "Hello World!n"; Sreejith S Let’s Learn Perl
  • 34. Customary Hello World Program #! /usr/bin/perl -w print "Hello World!n"; Save this as hello.pl Sreejith S Let’s Learn Perl
  • 35. Customary Hello World Program #! /usr/bin/perl -w print "Hello World!n"; Save this as hello.pl To run the program Sreejith S Let’s Learn Perl
  • 36. Customary Hello World Program #! /usr/bin/perl -w print "Hello World!n"; Save this as hello.pl To run the program perl hello.pl or perl hello Sreejith S Let’s Learn Perl
  • 37. Variables Variables are memory location to store information. Sreejith S Let’s Learn Perl
  • 38. Variables Variables are memory location to store information. Variables are type less Sreejith S Let’s Learn Perl
  • 39. Variables Variables are memory location to store information. Variables are type less There is no data type like int,char,float Sreejith S Let’s Learn Perl
  • 40. Variables Variables are memory location to store information. Variables are type less There is no data type like int,char,float Variables do not need to be declared Sreejith S Let’s Learn Perl
  • 41. Variables Variables are memory location to store information. Variables are type less There is no data type like int,char,float Variables do not need to be declared Every variable is a string Sreejith S Let’s Learn Perl
  • 42. Variables Variables are memory location to store information. Variables are type less There is no data type like int,char,float Variables do not need to be declared Every variable is a string Variable type is decided at run time Sreejith S Let’s Learn Perl
  • 43. Variables Variables are memory location to store information. Variables are type less There is no data type like int,char,float Variables do not need to be declared Every variable is a string Variable type is decided at run time Depending on the context will be treated as int, float etc. Sreejith S Let’s Learn Perl
  • 44. Variables Variables are memory location to store information. Variables are type less There is no data type like int,char,float Variables do not need to be declared Every variable is a string Variable type is decided at run time Depending on the context will be treated as int, float etc. There are 4 kinds of variables namely Sreejith S Let’s Learn Perl
  • 45. Variables Variables are memory location to store information. Variables are type less There is no data type like int,char,float Variables do not need to be declared Every variable is a string Variable type is decided at run time Depending on the context will be treated as int, float etc. There are 4 kinds of variables namely Scalars Sreejith S Let’s Learn Perl
  • 46. Variables Variables are memory location to store information. Variables are type less There is no data type like int,char,float Variables do not need to be declared Every variable is a string Variable type is decided at run time Depending on the context will be treated as int, float etc. There are 4 kinds of variables namely Scalars Lists Sreejith S Let’s Learn Perl
  • 47. Variables Variables are memory location to store information. Variables are type less There is no data type like int,char,float Variables do not need to be declared Every variable is a string Variable type is decided at run time Depending on the context will be treated as int, float etc. There are 4 kinds of variables namely Scalars Lists Arrays Sreejith S Let’s Learn Perl
  • 48. Variables Variables are memory location to store information. Variables are type less There is no data type like int,char,float Variables do not need to be declared Every variable is a string Variable type is decided at run time Depending on the context will be treated as int, float etc. There are 4 kinds of variables namely Scalars Lists Arrays Hashes. Sreejith S Let’s Learn Perl
  • 49. Scalars Scalar meaning single value Sreejith S Let’s Learn Perl
  • 50. Scalars Scalar meaning single value Name of scalar variable must begin with $ symbol. Sreejith S Let’s Learn Perl
  • 51. Scalars Scalar meaning single value Name of scalar variable must begin with $ symbol. next character must a letter or Sreejith S Let’s Learn Perl
  • 52. Scalars Scalar meaning single value Name of scalar variable must begin with $ symbol. next character must a letter or remaining characters: letters, numbers, or Sreejith S Let’s Learn Perl
  • 53. Scalars Scalar meaning single value Name of scalar variable must begin with $ symbol. next character must a letter or remaining characters: letters, numbers, or name can be up to 255 characters long Sreejith S Let’s Learn Perl
  • 54. Scalars Scalar meaning single value Name of scalar variable must begin with $ symbol. next character must a letter or remaining characters: letters, numbers, or name can be up to 255 characters long Right Sreejith S Let’s Learn Perl
  • 55. Scalars Scalar meaning single value Name of scalar variable must begin with $ symbol. next character must a letter or remaining characters: letters, numbers, or name can be up to 255 characters long Right $foo, $a, $zebra1, $F87dr df3 Sreejith S Let’s Learn Perl
  • 56. Scalars Scalar meaning single value Name of scalar variable must begin with $ symbol. next character must a letter or remaining characters: letters, numbers, or name can be up to 255 characters long Right $foo, $a, $zebra1, $F87dr df3 Wrong Sreejith S Let’s Learn Perl
  • 57. Scalars Scalar meaning single value Name of scalar variable must begin with $ symbol. next character must a letter or remaining characters: letters, numbers, or name can be up to 255 characters long Right $foo, $a, $zebra1, $F87dr df3 Wrong $24da, $hi&bye, $bar$foo Sreejith S Let’s Learn Perl
  • 58. Scalars Scalar meaning single value Name of scalar variable must begin with $ symbol. next character must a letter or remaining characters: letters, numbers, or name can be up to 255 characters long Right $foo, $a, $zebra1, $F87dr df3 Wrong $24da, $hi&bye, $bar$foo Examples Sreejith S Let’s Learn Perl
  • 59. Scalars Scalar meaning single value Name of scalar variable must begin with $ symbol. next character must a letter or remaining characters: letters, numbers, or name can be up to 255 characters long Right $foo, $a, $zebra1, $F87dr df3 Wrong $24da, $hi&bye, $bar$foo Examples $name=”ram”; Sreejith S Let’s Learn Perl
  • 60. Scalars Scalar meaning single value Name of scalar variable must begin with $ symbol. next character must a letter or remaining characters: letters, numbers, or name can be up to 255 characters long Right $foo, $a, $zebra1, $F87dr df3 Wrong $24da, $hi&bye, $bar$foo Examples $name=”ram”; # in string context Sreejith S Let’s Learn Perl
  • 61. Scalars Scalar meaning single value Name of scalar variable must begin with $ symbol. next character must a letter or remaining characters: letters, numbers, or name can be up to 255 characters long Right $foo, $a, $zebra1, $F87dr df3 Wrong $24da, $hi&bye, $bar$foo Examples $name=”ram”; # in string context $age=30; Sreejith S Let’s Learn Perl
  • 62. Scalars Scalar meaning single value Name of scalar variable must begin with $ symbol. next character must a letter or remaining characters: letters, numbers, or name can be up to 255 characters long Right $foo, $a, $zebra1, $F87dr df3 Wrong $24da, $hi&bye, $bar$foo Examples $name=”ram”; # in string context $age=30; # in numerical context Sreejith S Let’s Learn Perl
  • 63. Scalars Scalar meaning single value Name of scalar variable must begin with $ symbol. next character must a letter or remaining characters: letters, numbers, or name can be up to 255 characters long Right $foo, $a, $zebra1, $F87dr df3 Wrong $24da, $hi&bye, $bar$foo Examples $name=”ram”; # in string context $age=30; # in numerical context $value = 4.43; Sreejith S Let’s Learn Perl
  • 64. Scalars Scalar meaning single value Name of scalar variable must begin with $ symbol. next character must a letter or remaining characters: letters, numbers, or name can be up to 255 characters long Right $foo, $a, $zebra1, $F87dr df3 Wrong $24da, $hi&bye, $bar$foo Examples $name=”ram”; # in string context $age=30; # in numerical context $value = 4.43; # in float context Sreejith S Let’s Learn Perl
  • 65. Scalars Scalar meaning single value Name of scalar variable must begin with $ symbol. next character must a letter or remaining characters: letters, numbers, or name can be up to 255 characters long Right $foo, $a, $zebra1, $F87dr df3 Wrong $24da, $hi&bye, $bar$foo Examples $name=”ram”; # in string context $age=30; # in numerical context $value = 4.43; # in float context $foo = Z; Sreejith S Let’s Learn Perl
  • 66. Scalars Scalar meaning single value Name of scalar variable must begin with $ symbol. next character must a letter or remaining characters: letters, numbers, or name can be up to 255 characters long Right $foo, $a, $zebra1, $F87dr df3 Wrong $24da, $hi&bye, $bar$foo Examples $name=”ram”; # in string context $age=30; # in numerical context $value = 4.43; # in float context $foo = Z; # in char context Sreejith S Let’s Learn Perl
  • 67. Variable & Character Interpolation Perl looks for variables inside strings and replaces them with their value Sreejith S Let’s Learn Perl
  • 68. Variable & Character Interpolation Perl looks for variables inside strings and replaces them with their value $name = "Sree"; print "$name is my namen"; Sreejith S Let’s Learn Perl
  • 69. Variable & Character Interpolation Perl looks for variables inside strings and replaces them with their value $name = "Sree"; print "$name is my namen"; This does not happen when you use single quotes Sreejith S Let’s Learn Perl
  • 70. Variable & Character Interpolation Perl looks for variables inside strings and replaces them with their value $name = "Sree"; print "$name is my namen"; This does not happen when you use single quotes print ’$name is my name’; and it will print Sreejith S Let’s Learn Perl
  • 71. Variable & Character Interpolation Perl looks for variables inside strings and replaces them with their value $name = "Sree"; print "$name is my namen"; This does not happen when you use single quotes print ’$name is my name’; and it will print $name is my name Sreejith S Let’s Learn Perl
  • 72. Variable & Character Interpolation Perl looks for variables inside strings and replaces them with their value $name = "Sree"; print "$name is my namen"; This does not happen when you use single quotes print ’$name is my name’; and it will print $name is my name List of character escapes that are recognized when using double quoted strings All escape characters are recognized Sreejith S Let’s Learn Perl
  • 73. Variable & Character Interpolation Perl looks for variables inside strings and replaces them with their value $name = "Sree"; print "$name is my namen"; This does not happen when you use single quotes print ’$name is my name’; and it will print $name is my name List of character escapes that are recognized when using double quoted strings All escape characters are recognized print "Hello World!n"; # It prints Hello World and then a return Sreejith S Let’s Learn Perl
  • 74. Lists List variables are denoted by symbol ‘()‘ Sreejith S Let’s Learn Perl
  • 75. Lists List variables are denoted by symbol ‘()‘ List is just a list of values may be constants, scalars etc Sreejith S Let’s Learn Perl
  • 76. Lists List variables are denoted by symbol ‘()‘ List is just a list of values may be constants, scalars etc (a,b,c) or ($name,$age,$sex) Sreejith S Let’s Learn Perl
  • 77. Lists List variables are denoted by symbol ‘()‘ List is just a list of values may be constants, scalars etc (a,b,c) or ($name,$age,$sex) The index are specified inside a square bracket ‘[]‘ Sreejith S Let’s Learn Perl
  • 78. Lists List variables are denoted by symbol ‘()‘ List is just a list of values may be constants, scalars etc (a,b,c) or ($name,$age,$sex) The index are specified inside a square bracket ‘[]‘ $first=(a,b,c)[0]; print "$firstn"; Sreejith S Let’s Learn Perl
  • 79. Lists List variables are denoted by symbol ‘()‘ List is just a list of values may be constants, scalars etc (a,b,c) or ($name,$age,$sex) The index are specified inside a square bracket ‘[]‘ $first=(a,b,c)[0]; print "$firstn"; List variables can be assigned like this Sreejith S Let’s Learn Perl
  • 80. Lists List variables are denoted by symbol ‘()‘ List is just a list of values may be constants, scalars etc (a,b,c) or ($name,$age,$sex) The index are specified inside a square bracket ‘[]‘ $first=(a,b,c)[0]; print "$firstn"; List variables can be assigned like this ($name,$age)=(’Raman’,20); print "name=$namen"; Sreejith S Let’s Learn Perl
  • 81. Arrays Array variables should have prefix ‘@‘ Sreejith S Let’s Learn Perl
  • 82. Arrays Array variables should have prefix ‘@‘ Arrays are used to store ordered values Sreejith S Let’s Learn Perl
  • 83. Arrays Array variables should have prefix ‘@‘ Arrays are used to store ordered values Much more dynamic than C/C++ Sreejith S Let’s Learn Perl
  • 84. Arrays Array variables should have prefix ‘@‘ Arrays are used to store ordered values Much more dynamic than C/C++ no declaration of size, type Sreejith S Let’s Learn Perl
  • 85. Arrays Array variables should have prefix ‘@‘ Arrays are used to store ordered values Much more dynamic than C/C++ no declaration of size, type can hold any kind of value, and multiple kinds of values Sreejith S Let’s Learn Perl
  • 86. Arrays Array variables should have prefix ‘@‘ Arrays are used to store ordered values Much more dynamic than C/C++ no declaration of size, type can hold any kind of value, and multiple kinds of values Array Indexes start at 0 Sreejith S Let’s Learn Perl
  • 87. Arrays Array variables should have prefix ‘@‘ Arrays are used to store ordered values Much more dynamic than C/C++ no declaration of size, type can hold any kind of value, and multiple kinds of values Array Indexes start at 0 @array=(1,2,3); To access the whole array, use the whole array Sreejith S Let’s Learn Perl
  • 88. Arrays Array variables should have prefix ‘@‘ Arrays are used to store ordered values Much more dynamic than C/C++ no declaration of size, type can hold any kind of value, and multiple kinds of values Array Indexes start at 0 @array=(1,2,3); To access the whole array, use the whole array print @array; Sreejith S Let’s Learn Perl
  • 89. Arrays Array variables should have prefix ‘@‘ Arrays are used to store ordered values Much more dynamic than C/C++ no declaration of size, type can hold any kind of value, and multiple kinds of values Array Indexes start at 0 @array=(1,2,3); To access the whole array, use the whole array print @array; To access one element of the array : use $ Sreejith S Let’s Learn Perl
  • 90. Arrays Array variables should have prefix ‘@‘ Arrays are used to store ordered values Much more dynamic than C/C++ no declaration of size, type can hold any kind of value, and multiple kinds of values Array Indexes start at 0 @array=(1,2,3); To access the whole array, use the whole array print @array; To access one element of the array : use $ print $array[0]; # prints : 1 Sreejith S Let’s Learn Perl
  • 91. Operations on Arrays To append to the end of an array : PUSH Sreejith S Let’s Learn Perl
  • 92. Operations on Arrays To append to the end of an array : PUSH @array=(1,2,3); push @array,’4’; print "@array n"; Sreejith S Let’s Learn Perl
  • 93. Operations on Arrays To append to the end of an array : PUSH @array=(1,2,3); push @array,’4’; print "@array n"; To remove the last element of the array : POP Sreejith S Let’s Learn Perl
  • 94. Operations on Arrays To append to the end of an array : PUSH @array=(1,2,3); push @array,’4’; print "@array n"; To remove the last element of the array : POP @array=(1,2,3); $last=pop @array; print "last=$lastn"; Sreejith S Let’s Learn Perl
  • 95. Operations on Arrays To append to the end of an array : PUSH @array=(1,2,3); push @array,’4’; print "@array n"; To remove the last element of the array : POP @array=(1,2,3); $last=pop @array; print "last=$lastn"; To prepend to the beginning of an array : UNSHIFT Sreejith S Let’s Learn Perl
  • 96. Operations on Arrays To append to the end of an array : PUSH @array=(1,2,3); push @array,’4’; print "@array n"; To remove the last element of the array : POP @array=(1,2,3); $last=pop @array; print "last=$lastn"; To prepend to the beginning of an array : UNSHIFT @array=(3,4,5); unshift @array,’2’; print "array=@arrayn"; Sreejith S Let’s Learn Perl
  • 97. Operations on Arrays To append to the end of an array : PUSH @array=(1,2,3); push @array,’4’; print "@array n"; To remove the last element of the array : POP @array=(1,2,3); $last=pop @array; print "last=$lastn"; To prepend to the beginning of an array : UNSHIFT @array=(3,4,5); unshift @array,’2’; print "array=@arrayn"; To remove the first element of the array : SHIFT Sreejith S Let’s Learn Perl
  • 98. Operations on Arrays To append to the end of an array : PUSH @array=(1,2,3); push @array,’4’; print "@array n"; To remove the last element of the array : POP @array=(1,2,3); $last=pop @array; print "last=$lastn"; To prepend to the beginning of an array : UNSHIFT @array=(3,4,5); unshift @array,’2’; print "array=@arrayn"; To remove the first element of the array : SHIFT @array=(3,4,5); unshift @array,’2’; print "array=@arrayn"; Sreejith S Let’s Learn Perl
  • 99. Operations on Arrays To append to the end of an array : PUSH @array=(1,2,3); push @array,’4’; print "@array n"; To remove the last element of the array : POP @array=(1,2,3); $last=pop @array; print "last=$lastn"; To prepend to the beginning of an array : UNSHIFT @array=(3,4,5); unshift @array,’2’; print "array=@arrayn"; To remove the first element of the array : SHIFT @array=(3,4,5); unshift @array,’2’; print "array=@arrayn"; Sreejith S Let’s Learn Perl
  • 100. Operations on Arrays To append to the end of an array : PUSH @array=(1,2,3); push @array,’4’; print "@array n"; To remove the last element of the array : POP @array=(1,2,3); $last=pop @array; print "last=$lastn"; To prepend to the beginning of an array : UNSHIFT @array=(3,4,5); unshift @array,’2’; print "array=@arrayn"; To remove the first element of the array : SHIFT @array=(3,4,5); unshift @array,’2’; print "array=@arrayn"; Sreejith S Let’s Learn Perl
  • 101. Operations on Arrays To append to the end of an array : PUSH @array=(1,2,3); push @array,’4’; print "@array n"; To remove the last element of the array : POP @array=(1,2,3); $last=pop @array; print "last=$lastn"; To prepend to the beginning of an array : UNSHIFT @array=(3,4,5); unshift @array,’2’; print "array=@arrayn"; To remove the first element of the array : SHIFT @array=(3,4,5); unshift @array,’2’; print "array=@arrayn"; Sreejith S Let’s Learn Perl
  • 102. Operations on Arrays To append to the end of an array : PUSH @array=(1,2,3); push @array,’4’; print "@array n"; To remove the last element of the array : POP @array=(1,2,3); $last=pop @array; print "last=$lastn"; To prepend to the beginning of an array : UNSHIFT @array=(3,4,5); unshift @array,’2’; print "array=@arrayn"; To remove the first element of the array : SHIFT @array=(3,4,5); unshift @array,’2’; print "array=@arrayn"; Sreejith S Let’s Learn Perl
  • 103. Operations on Arrays Contind... $#array is a special variable containing index of last element. Sreejith S Let’s Learn Perl
  • 104. Operations on Arrays Contind... $#array is a special variable containing index of last element. print $#array; # it will print 2 Sreejith S Let’s Learn Perl
  • 105. Operations on Arrays Contind... $#array is a special variable containing index of last element. print $#array; # it will print 2 Another way to find the number of elements in the array: Sreejith S Let’s Learn Perl
  • 106. Operations on Arrays Contind... $#array is a special variable containing index of last element. print $#array; # it will print 2 Another way to find the number of elements in the array: $array_size = @array; scalar(@array) is a function to return the size of array Sreejith S Let’s Learn Perl
  • 107. Operations on Arrays Contind... $#array is a special variable containing index of last element. print $#array; # it will print 2 Another way to find the number of elements in the array: $array_size = @array; scalar(@array) is a function to return the size of array Sreejith S Let’s Learn Perl
  • 108. Hashes Hash variables will have % as prefix Sreejith S Let’s Learn Perl
  • 109. Hashes Hash variables will have % as prefix Key-Value pair Sreejith S Let’s Learn Perl
  • 110. Hashes Hash variables will have % as prefix Key-Value pair The contents of hash are called values and index is called key. the array: Sreejith S Let’s Learn Perl
  • 111. Hashes Hash variables will have % as prefix Key-Value pair The contents of hash are called values and index is called key. the array: %fruits= ( ’apple’=>’red’,’banana’=>’yellow’, ’grape’ =>’black’); print "colour of apple =$fruits{apple}n"; Sreejith S Let’s Learn Perl
  • 112. Hashes Hash variables will have % as prefix Key-Value pair The contents of hash are called values and index is called key. the array: %fruits= ( ’apple’=>’red’,’banana’=>’yellow’, ’grape’ =>’black’); print "colour of apple =$fruits{apple}n"; Other way of populating a hash Sreejith S Let’s Learn Perl
  • 113. Hashes Hash variables will have % as prefix Key-Value pair The contents of hash are called values and index is called key. the array: %fruits= ( ’apple’=>’red’,’banana’=>’yellow’, ’grape’ =>’black’); print "colour of apple =$fruits{apple}n"; Other way of populating a hash %fruits =(’apple’,’red’,’banana’,’yellow’, ’grape’,’black’); print "colour of banana =$fruits{banana}n"; Sreejith S Let’s Learn Perl
  • 114. Hashes Hash variables will have % as prefix Key-Value pair The contents of hash are called values and index is called key. the array: %fruits= ( ’apple’=>’red’,’banana’=>’yellow’, ’grape’ =>’black’); print "colour of apple =$fruits{apple}n"; Other way of populating a hash %fruits =(’apple’,’red’,’banana’,’yellow’, ’grape’,’black’); print "colour of banana =$fruits{banana}n"; Note the {} instead of [ ] as in the case of array; Sreejith S Let’s Learn Perl
  • 115. Conditionals - IF The if statement is similar to if in C language, except Sreejith S Let’s Learn Perl
  • 116. Conditionals - IF The if statement is similar to if in C language, except flower brace is required even for single statement else if is noted by elsif (note missing e) Sreejith S Let’s Learn Perl
  • 117. Conditionals - IF The if statement is similar to if in C language, except flower brace is required even for single statement else if is noted by elsif (note missing e) The syntax of if statement is Sreejith S Let’s Learn Perl
  • 118. Conditionals - IF The if statement is similar to if in C language, except flower brace is required even for single statement else if is noted by elsif (note missing e) The syntax of if statement is if (condition) { } elsif (condition){ } else { } Sreejith S Let’s Learn Perl
  • 119. Control Structures - Loops Perl supports four main loop types Sreejith S Let’s Learn Perl
  • 120. Control Structures - Loops Perl supports four main loop types while, until, for, and foreach Sreejith S Let’s Learn Perl
  • 121. Control Structures - Loops Perl supports four main loop types while, until, for, and foreach while loop is used to iterate and has syntax similar to C. Sreejith S Let’s Learn Perl
  • 122. Control Structures - Loops Perl supports four main loop types while, until, for, and foreach while loop is used to iterate and has syntax similar to C. $i = 0; while ( $i <= 1000 ) { print $in; $i++; } Sreejith S Let’s Learn Perl
  • 123. Control Structures - Until Loop The until function evaluates an expression repeatedly until a specific condition is met Sreejith S Let’s Learn Perl
  • 124. Control Structures - Until Loop The until function evaluates an expression repeatedly until a specific condition is met $i = 0; until ($i == 1000) { print $in; $i++; } Sreejith S Let’s Learn Perl
  • 125. Control Structures - For Loop For loop syntax is similar to c. Sreejith S Let’s Learn Perl
  • 126. Control Structures - For Loop For loop syntax is similar to c. There are variations Sreejith S Let’s Learn Perl
  • 127. Control Structures - For Loop For loop syntax is similar to c. There are variations Syntax 1: Sreejith S Let’s Learn Perl
  • 128. Control Structures - For Loop For loop syntax is similar to c. There are variations Syntax 1: for ( $i = 0; $i <= 1000; $i=$i+2 ) { print $i; } Sreejith S Let’s Learn Perl
  • 129. Control Structures - For Loop For loop syntax is similar to c. There are variations Syntax 1: for ( $i = 0; $i <= 1000; $i=$i+2 ) { print $i; } Syntax 2: Sreejith S Let’s Learn Perl
  • 130. Control Structures - For Loop For loop syntax is similar to c. There are variations Syntax 1: for ( $i = 0; $i <= 1000; $i=$i+2 ) { print $i; } Syntax 2: for $i(0..1000) { print $i; } Sreejith S Let’s Learn Perl
  • 131. Control Structures - ForEach Loop Syntax 3: Sreejith S Let’s Learn Perl
  • 132. Control Structures - ForEach Loop Syntax 3: foreach $i (a,b,c) { print uc $i; } Sreejith S Let’s Learn Perl
  • 133. Default scalar variable $ is called default variable Sreejith S Let’s Learn Perl
  • 134. Default scalar variable $ is called default variable It will be used if no other variable is specified foreach (a,b,c){ print uc ; } Sreejith S Let’s Learn Perl
  • 135. Accepting input Keyboard inputs can be accepted using STDIN Sreejith S Let’s Learn Perl
  • 136. Accepting input Keyboard inputs can be accepted using STDIN print "enter your name "; $name=<STDIN>; chomp $name; # chomp function is similar to fflush in C. # It removes trailing newline if any print "Welcome $namen"; Sreejith S Let’s Learn Perl
  • 137. Subroutines Subroutines can be defined using sub keyword Sreejith S Let’s Learn Perl
  • 138. Subroutines Subroutines can be defined using sub keyword ’@ ’ default array $v1=10;$v2=20; add($v1,$v2); sub add { ($a,$b)=@_; print $a+$b; } Sreejith S Let’s Learn Perl
  • 139. Scope of variables By default all variables are global Sreejith S Let’s Learn Perl
  • 140. Scope of variables By default all variables are global You can limit scope to a block/sub by using my. $v1=10; $v2=30; #v1,v2 global my $i = 10; Sreejith S Let’s Learn Perl
  • 141. File handling File handling can be done very easily Sreejith S Let’s Learn Perl
  • 142. File handling File handling can be done very easily To open a file in read mode Sreejith S Let’s Learn Perl
  • 143. File handling File handling can be done very easily To open a file in read mode open( $fh, < , data.txt); Sreejith S Let’s Learn Perl
  • 144. File handling File handling can be done very easily To open a file in read mode open( $fh, < , data.txt); File reading line by line can be done like Sreejith S Let’s Learn Perl
  • 145. File handling File handling can be done very easily To open a file in read mode open( $fh, < , data.txt); File reading line by line can be done like $line=<$fh>; Sreejith S Let’s Learn Perl
  • 146. File handling File handling can be done very easily To open a file in read mode open( $fh, < , data.txt); File reading line by line can be done like $line=<$fh>; File writing can be done using print and filehandle like this print $fh "hello"; Sreejith S Let’s Learn Perl
  • 147. File handling File handling can be done very easily To open a file in read mode open( $fh, < , data.txt); File reading line by line can be done like $line=<$fh>; File writing can be done using print and filehandle like this print $fh "hello"; Example Sreejith S Let’s Learn Perl
  • 148. File handling File handling can be done very easily To open a file in read mode open( $fh, < , data.txt); File reading line by line can be done like $line=<$fh>; File writing can be done using print and filehandle like this print $fh "hello"; Example open ( $fh, "<", "data.txt" ); #open file read only open ($fh1,">","udata.txt"); #Open file write mode while ( $line = <$fh>; ) { #read line by print "line=$line"; #display content on screen print $fh1 uc($line); #write upper cased content t } close($fh); close($fh1); Sreejith S Let’s Learn Perl
  • 149. Some Common String Functions There are many built in string functions Sreejith S Let’s Learn Perl
  • 150. Some Common String Functions There are many built in string functions Convert to upper case Sreejith S Let’s Learn Perl
  • 151. Some Common String Functions There are many built in string functions Convert to upper case $name = uc($name); Sreejith S Let’s Learn Perl
  • 152. Some Common String Functions There are many built in string functions Convert to upper case $name = uc($name); Convert only the first char to upper case Sreejith S Let’s Learn Perl
  • 153. Some Common String Functions There are many built in string functions Convert to upper case $name = uc($name); Convert only the first char to upper case $name = ucfirst($name); Sreejith S Let’s Learn Perl
  • 154. Some Common String Functions There are many built in string functions Convert to upper case $name = uc($name); Convert only the first char to upper case $name = ucfirst($name); Convert to lower case Sreejith S Let’s Learn Perl
  • 155. Some Common String Functions There are many built in string functions Convert to upper case $name = uc($name); Convert only the first char to upper case $name = ucfirst($name); Convert to lower case $name = lc($name); Sreejith S Let’s Learn Perl
  • 156. Some Common String Functions There are many built in string functions Convert to upper case $name = uc($name); Convert only the first char to upper case $name = ucfirst($name); Convert to lower case $name = lc($name); Convert only the first char to lower case Sreejith S Let’s Learn Perl
  • 157. Some Common String Functions There are many built in string functions Convert to upper case $name = uc($name); Convert only the first char to upper case $name = ucfirst($name); Convert to lower case $name = lc($name); Convert only the first char to lower case $name = lcfirst($name); Sreejith S Let’s Learn Perl
  • 158. Some Common String Functions There are many built in string functions Convert to upper case $name = uc($name); Convert only the first char to upper case $name = ucfirst($name); Convert to lower case $name = lc($name); Convert only the first char to lower case $name = lcfirst($name); Concatenation: ’.’ similar to strcat Sreejith S Let’s Learn Perl
  • 159. Some Common String Functions There are many built in string functions Convert to upper case $name = uc($name); Convert only the first char to upper case $name = ucfirst($name); Convert to lower case $name = lc($name); Convert only the first char to lower case $name = lcfirst($name); Concatenation: ’.’ similar to strcat $first_name = Larry; $last_name = Wall; $full_name = $first_name . . $last_name; Sreejith S Let’s Learn Perl
  • 160. Some Common Functions There are many built in functions Sreejith S Let’s Learn Perl
  • 161. Some Common Functions There are many built in functions Strings to Arrays : split Sreejith S Let’s Learn Perl
  • 162. Some Common Functions There are many built in functions Strings to Arrays : split @array = split( /;/, "Larry;Curly;Moe" ); @array= ("Larry", "Curly", "Moe"); Sreejith S Let’s Learn Perl
  • 163. Some Common Functions There are many built in functions Strings to Arrays : split @array = split( /;/, "Larry;Curly;Moe" ); @array= ("Larry", "Curly", "Moe"); Split into characters Sreejith S Let’s Learn Perl
  • 164. Some Common Functions There are many built in functions Strings to Arrays : split @array = split( /;/, "Larry;Curly;Moe" ); @array= ("Larry", "Curly", "Moe"); Split into characters @stooge = split( //, "curly" ); # array @stooge has 5 elements: c, u, r, l, y Sreejith S Let’s Learn Perl
  • 165. Some Common Functions There are many built in functions Strings to Arrays : split @array = split( /;/, "Larry;Curly;Moe" ); @array= ("Larry", "Curly", "Moe"); Split into characters @stooge = split( //, "curly" ); # array @stooge has 5 elements: c, u, r, l, y Join with any character you want Sreejith S Let’s Learn Perl
  • 166. Some Common Functions There are many built in functions Strings to Arrays : split @array = split( /;/, "Larry;Curly;Moe" ); @array= ("Larry", "Curly", "Moe"); Split into characters @stooge = split( //, "curly" ); # array @stooge has 5 elements: c, u, r, l, y Join with any character you want @array = 10, 20, 30, 40); $string = join(->, @array); # string = 10->20->30->40 Sreejith S Let’s Learn Perl
  • 167. Web Resource for Perl Web resources for Perl Sreejith S Let’s Learn Perl
  • 168. Web Resource for Perl Web resources for Perl www.perl.com www.perl.org www.perlmonks.org Sreejith S Let’s Learn Perl
  • 169. Recommended Reading Sreejith S Let’s Learn Perl
  • 170. Questions Sreejith S Let’s Learn Perl
  • 171. Thank You Sreejith S Let’s Learn Perl