Your SlideShare is downloading. ×
0
Let’s Learn PerlAn introduction to Perl      Sreejith SNLP Process Analyst 365Media Pvt. Ltd.srssreejith@gmail.com   Janua...
Just a word about me !!     Working in Natural Language Processing (NLP), Machine Learning,     Data Mining     Passionate...
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                  ...
What’s Perl ??     Practical Extraction and Reporting Language     Perl is a Portable Scripting Language     Fast and easy...
What’s Perl ??     Practical Extraction and Reporting Language     Perl is a Portable Scripting Language     Fast and easy...
What’s Perl ??     Practical Extraction and Reporting Language     Perl is a Portable Scripting Language     Fast and easy...
What’s Perl ??     Practical Extraction and Reporting Language     Perl is a Portable Scripting Language     Fast and easy...
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               ...
To install...      Perl Comes by Default on Linux, Cygwin, MacOSX          www.perl.com has rpm’s for Linux          www.a...
To install...      Perl Comes by Default on Linux, Cygwin, MacOSX          www.perl.com has rpm’s for Linux          www.a...
To install...      Perl Comes by Default on Linux, Cygwin, MacOSX          www.perl.com has rpm’s for Linux          www.a...
To install...      Perl Comes by Default on Linux, Cygwin, MacOSX          www.perl.com has rpm’s for Linux          www.a...
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 /* ... */                            ...
Basic Syntax of Perl     Comments         begin with #         dont try to use // or /* ... */     Print                  ...
Basic Syntax of Perl     Comments         begin with #         dont try to use // or /* ... */     Print         print is ...
Basic Syntax of Perl     Comments         begin with #         dont try to use // or /* ... */     Print         print is ...
Basic Syntax of Perl     Comments         begin with #         dont try to use // or /* ... */     Print         print is ...
Basic Syntax of Perl     Comments         begin with #         dont try to use // or /* ... */     Print         print is ...
Basic Syntax of Perl     Comments         begin with #         dont try to use // or /* ... */     Print         print is ...
Basic Syntax of Perl     Comments         begin with #         dont try to use // or /* ... */     Print         print is ...
Basic Syntax of Perl     Comments         begin with #         dont try to use // or /* ... */     Print         print is ...
Basic Syntax of Perl     Comments         begin with #         dont try to use // or /* ... */     Print         print is ...
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                               Sreej...
Structure of a Perl Program     #! /usr/bin/perl -w         It tells where to find Perl     .pl extension is optional but i...
Structure of a Perl Program     #! /usr/bin/perl -w         It tells where to find Perl     .pl extension is optional but i...
Structure of a Perl Program     #! /usr/bin/perl -w         It tells where to find Perl     .pl extension is optional but i...
Customary Hello World Program  #! /usr/bin/perl -w  print "Hello World!n";                            Sreejith S   Let’s L...
Customary Hello World Program  #! /usr/bin/perl -w  print "Hello World!n";     Save this as hello.pl                      ...
Customary Hello World Program  #! /usr/bin/perl -w  print "Hello World!n";     Save this as hello.pl     To run the progra...
Customary Hello World Program  #! /usr/bin/perl -w  print "Hello World!n";     Save this as hello.pl     To run the progra...
Variables      Variables are memory location to store information.                              Sreejith S   Let’s Learn P...
Variables      Variables are memory location to store information.      Variables are type less                           ...
Variables      Variables are memory location to store information.      Variables are type less            There is no dat...
Variables      Variables are memory location to store information.      Variables are type less            There is no dat...
Variables      Variables are memory location to store information.      Variables are type less            There is no dat...
Variables      Variables are memory location to store information.      Variables are type less            There is no dat...
Variables      Variables are memory location to store information.      Variables are type less            There is no dat...
Variables      Variables are memory location to store information.      Variables are type less            There is no dat...
Variables      Variables are memory location to store information.      Variables are type less            There is no dat...
Variables      Variables are memory location to store information.      Variables are type less            There is no dat...
Variables      Variables are memory location to store information.      Variables are type less            There is no dat...
Variables      Variables are memory location to store information.      Variables are type less            There is no dat...
Scalars      Scalar meaning single value                              Sreejith S   Let’s Learn Perl
Scalars      Scalar meaning single value      Name of scalar variable must begin with $ symbol.                           ...
Scalars      Scalar meaning single value      Name of scalar variable must begin with $ symbol.          next character mu...
Scalars      Scalar meaning single value      Name of scalar variable must begin with $ symbol.          next character mu...
Scalars      Scalar meaning single value      Name of scalar variable must begin with $ symbol.          next character mu...
Scalars      Scalar meaning single value      Name of scalar variable must begin with $ symbol.          next character mu...
Scalars      Scalar meaning single value      Name of scalar variable must begin with $ symbol.          next character mu...
Scalars      Scalar meaning single value      Name of scalar variable must begin with $ symbol.          next character mu...
Scalars      Scalar meaning single value      Name of scalar variable must begin with $ symbol.          next character mu...
Scalars      Scalar meaning single value      Name of scalar variable must begin with $ symbol.          next character mu...
Scalars      Scalar meaning single value      Name of scalar variable must begin with $ symbol.          next character mu...
Scalars      Scalar meaning single value      Name of scalar variable must begin with $ symbol.          next character mu...
Scalars      Scalar meaning single value      Name of scalar variable must begin with $ symbol.          next character mu...
Scalars      Scalar meaning single value      Name of scalar variable must begin with $ symbol.          next character mu...
Scalars      Scalar meaning single value      Name of scalar variable must begin with $ symbol.          next character mu...
Scalars      Scalar meaning single value      Name of scalar variable must begin with $ symbol.          next character mu...
Scalars      Scalar meaning single value      Name of scalar variable must begin with $ symbol.          next character mu...
Scalars      Scalar meaning single value      Name of scalar variable must begin with $ symbol.          next character mu...
Variable & Character Interpolation     Perl looks for variables inside strings and replaces them with their     value     ...
Variable & Character Interpolation     Perl looks for variables inside strings and replaces them with their     value     ...
Variable & Character Interpolation     Perl looks for variables inside strings and replaces them with their     value     ...
Variable & Character Interpolation     Perl looks for variables inside strings and replaces them with their     value     ...
Variable & Character Interpolation     Perl looks for variables inside strings and replaces them with their     value     ...
Variable & Character Interpolation     Perl looks for variables inside strings and replaces them with their     value     ...
Variable & Character Interpolation     Perl looks for variables inside strings and replaces them with their     value     ...
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 ...
Lists        List variables are denoted by symbol ‘()‘        List is just a list of values may be constants, scalars etc ...
Lists        List variables are denoted by symbol ‘()‘        List is just a list of values may be constants, scalars etc ...
Lists        List variables are denoted by symbol ‘()‘        List is just a list of values may be constants, scalars etc ...
Lists        List variables are denoted by symbol ‘()‘        List is just a list of values may be constants, scalars etc ...
Lists        List variables are denoted by symbol ‘()‘        List is just a list of values may be constants, scalars etc ...
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                             S...
Arrays     Array variables should have prefix ‘@‘     Arrays are used to store ordered values     Much more dynamic than C/...
Arrays     Array variables should have prefix ‘@‘     Arrays are used to store ordered values     Much more dynamic than C/...
Arrays     Array variables should have prefix ‘@‘     Arrays are used to store ordered values     Much more dynamic than C/...
Arrays     Array variables should have prefix ‘@‘     Arrays are used to store ordered values     Much more dynamic than C/...
Arrays     Array variables should have prefix ‘@‘     Arrays are used to store ordered values     Much more dynamic than C/...
Arrays     Array variables should have prefix ‘@‘     Arrays are used to store ordered values     Much more dynamic than C/...
Arrays     Array variables should have prefix ‘@‘     Arrays are used to store ordered values     Much more dynamic than C/...
Arrays     Array variables should have prefix ‘@‘     Arrays are used to store ordered values     Much more dynamic than C/...
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 ...
Operations on Arrays     To append to the end of an array : PUSH       @array=(1,2,3);       push @array,’4’;       print ...
Operations on Arrays     To append to the end of an array : PUSH       @array=(1,2,3);       push @array,’4’;       print ...
Operations on Arrays     To append to the end of an array : PUSH       @array=(1,2,3);       push @array,’4’;       print ...
Operations on Arrays     To append to the end of an array : PUSH       @array=(1,2,3);       push @array,’4’;       print ...
Operations on Arrays     To append to the end of an array : PUSH       @array=(1,2,3);       push @array,’4’;       print ...
Operations on Arrays     To append to the end of an array : PUSH       @array=(1,2,3);       push @array,’4’;       print ...
Operations on Arrays     To append to the end of an array : PUSH       @array=(1,2,3);       push @array,’4’;       print ...
Operations on Arrays     To append to the end of an array : PUSH       @array=(1,2,3);       push @array,’4’;       print ...
Operations on Arrays     To append to the end of an array : PUSH       @array=(1,2,3);       push @array,’4’;       print ...
Operations on Arrays     To append to the end of an array : PUSH       @array=(1,2,3);       push @array,’4’;       print ...
Operations on Arrays Contind...     $#array is a special variable containing index of last element.                       ...
Operations on Arrays Contind...     $#array is a special variable containing index of last element.         print $#array;...
Operations on Arrays Contind...     $#array is a special variable containing index of last element.         print $#array;...
Operations on Arrays Contind...     $#array is a special variable containing index of last element.         print $#array;...
Operations on Arrays Contind...     $#array is a special variable containing index of last element.         print $#array;...
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 ...
Hashes     Hash variables will have % as prefix     Key-Value pair     The contents of hash are called values and index is ...
Hashes     Hash variables will have % as prefix     Key-Value pair     The contents of hash are called values and index is ...
Hashes     Hash variables will have % as prefix     Key-Value pair     The contents of hash are called values and index is ...
Hashes     Hash variables will have % as prefix     Key-Value pair     The contents of hash are called values and index is ...
Conditionals - IF      The if statement is similar to if in C language, except                               Sreejith S   ...
Conditionals - IF      The if statement is similar to if in C language, except          flower brace is required even for s...
Conditionals - IF      The if statement is similar to if in C language, except          flower brace is required even for s...
Conditionals - IF      The if statement is similar to if in C language, except          flower brace is required even for s...
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                  ...
Control Structures - Loops     Perl supports four main loop types         while, until, for, and foreach     while loop is...
Control Structures - Loops     Perl supports four main loop types         while, until, for, and foreach     while loop is...
Control Structures - Until Loop     The until function evaluates an expression repeatedly until a specific     condition is...
Control Structures - Until Loop     The until function evaluates an expression repeatedly until a specific     condition is...
Control Structures - For Loop     For loop syntax is similar to c.                              Sreejith S   Let’s Learn P...
Control Structures - For Loop     For loop syntax is similar to c.     There are variations                              S...
Control Structures - For Loop     For loop syntax is similar to c.     There are variations     Syntax 1:                 ...
Control Structures - For Loop     For loop syntax is similar to c.     There are variations     Syntax 1:       for ( $i =...
Control Structures - For Loop     For loop syntax is similar to c.     There are variations     Syntax 1:       for ( $i =...
Control Structures - For Loop     For loop syntax is similar to c.     There are variations     Syntax 1:       for ( $i =...
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;     }                       ...
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       ...
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>;      ...
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,...
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.      $...
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                              Sree...
File handling      File handling can be done very easily      To open a file in read mode      open( $fh, < , data.txt);   ...
File handling      File handling can be done very easily      To open a file in read mode      open( $fh, < , data.txt);   ...
File handling      File handling can be done very easily      To open a file in read mode      open( $fh, < , data.txt);   ...
File handling      File handling can be done very easily      To open a file in read mode      open( $fh, < , data.txt);   ...
File handling      File handling can be done very easily      To open a file in read mode      open( $fh, < , data.txt);   ...
File handling      File handling can be done very easily      To open a file in read mode      open( $fh, < , data.txt);   ...
Some Common String Functions     There are many built in string functions                             Sreejith S   Let’s L...
Some Common String Functions     There are many built in string functions         Convert to upper case                   ...
Some Common String Functions     There are many built in string functions         Convert to upper case         $name = uc...
Some Common String Functions     There are many built in string functions         Convert to upper case         $name = uc...
Some Common String Functions     There are many built in string functions         Convert to upper case         $name = uc...
Some Common String Functions     There are many built in string functions         Convert to upper case         $name = uc...
Some Common String Functions     There are many built in string functions         Convert to upper case         $name = uc...
Some Common String Functions     There are many built in string functions         Convert to upper case         $name = uc...
Some Common String Functions     There are many built in string functions         Convert to upper case         $name = uc...
Some Common String Functions     There are many built in string functions         Convert to upper case         $name = uc...
Some Common String Functions     There are many built in string functions         Convert to upper case         $name = uc...
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                            Sreejit...
Some Common Functions    There are many built in functions    Strings to Arrays : split     @array = split( /;/, "Larry;Cu...
Some Common Functions    There are many built in functions    Strings to Arrays : split     @array = split( /;/, "Larry;Cu...
Some Common Functions    There are many built in functions    Strings to Arrays : split     @array = split( /;/, "Larry;Cu...
Some Common Functions    There are many built in functions    Strings to Arrays : split     @array = split( /;/, "Larry;Cu...
Some Common Functions    There are many built in functions    Strings to Arrays : split     @array = split( /;/, "Larry;Cu...
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      ...
Recommended Reading                      Sreejith S   Let’s Learn Perl
Questions            Sreejith S   Let’s Learn Perl
Thank You            Sreejith S   Let’s Learn Perl
Upcoming SlideShare
Loading in...5
×

Perl

823

Published on

Introfuction to Perl

Published in: Technology
1 Comment
0 Likes
Statistics
Notes
  • Be the first to like this

No Downloads
Views
Total Views
823
On Slideshare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
46
Comments
1
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "Perl"

  1. 1. Let’s Learn PerlAn introduction to Perl Sreejith SNLP Process Analyst 365Media Pvt. Ltd.srssreejith@gmail.com January 22, 2011 Sreejith S Let’s Learn Perl
  2. 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. 3. What’s Perl ?? Practical Extraction and Reporting Language Sreejith S Let’s Learn Perl
  4. 4. What’s Perl ?? Practical Extraction and Reporting Language Perl is a Portable Scripting Language Sreejith S Let’s Learn Perl
  5. 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. 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. 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. 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. 9. To install... Perl Comes by Default on Linux, Cygwin, MacOSX Sreejith S Let’s Learn Perl
  10. 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. 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. 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. 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. 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. 15. Sreejith S Let’s Learn Perl
  16. 16. Basic Syntax of Perl Comments Sreejith S Let’s Learn Perl
  17. 17. Basic Syntax of Perl Comments begin with # Sreejith S Let’s Learn Perl
  18. 18. Basic Syntax of Perl Comments begin with # dont try to use // or /* ... */ Sreejith S Let’s Learn Perl
  19. 19. Basic Syntax of Perl Comments begin with # dont try to use // or /* ... */ Print Sreejith S Let’s Learn Perl
  20. 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. 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. 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. 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. 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. 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. 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. 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. 28. Structure of a Perl Program #! /usr/bin/perl -w Sreejith S Let’s Learn Perl
  29. 29. Structure of a Perl Program #! /usr/bin/perl -w It tells where to find Perl Sreejith S Let’s Learn Perl
  30. 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. 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. 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. 33. Customary Hello World Program #! /usr/bin/perl -w print "Hello World!n"; Sreejith S Let’s Learn Perl
  34. 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. 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. 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. 37. Variables Variables are memory location to store information. Sreejith S Let’s Learn Perl
  38. 38. Variables Variables are memory location to store information. Variables are type less Sreejith S Let’s Learn Perl
  39. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 49. Scalars Scalar meaning single value Sreejith S Let’s Learn Perl
  50. 50. Scalars Scalar meaning single value Name of scalar variable must begin with $ symbol. Sreejith S Let’s Learn Perl
  51. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 67. Variable & Character Interpolation Perl looks for variables inside strings and replaces them with their value Sreejith S Let’s Learn Perl
  68. 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. 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. 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. 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. 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. 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. 74. Lists List variables are denoted by symbol ‘()‘ Sreejith S Let’s Learn Perl
  75. 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. 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. 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. 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. 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. 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. 81. Arrays Array variables should have prefix ‘@‘ Sreejith S Let’s Learn Perl
  82. 82. Arrays Array variables should have prefix ‘@‘ Arrays are used to store ordered values Sreejith S Let’s Learn Perl
  83. 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. 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. 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. 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. 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. 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. 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. 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. 91. Operations on Arrays To append to the end of an array : PUSH Sreejith S Let’s Learn Perl
  92. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 103. Operations on Arrays Contind... $#array is a special variable containing index of last element. Sreejith S Let’s Learn Perl
  104. 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. 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. 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. 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. 108. Hashes Hash variables will have % as prefix Sreejith S Let’s Learn Perl
  109. 109. Hashes Hash variables will have % as prefix Key-Value pair Sreejith S Let’s Learn Perl
  110. 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. 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. 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. 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. 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. 115. Conditionals - IF The if statement is similar to if in C language, except Sreejith S Let’s Learn Perl
  116. 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. 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. 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. 119. Control Structures - Loops Perl supports four main loop types Sreejith S Let’s Learn Perl
  120. 120. Control Structures - Loops Perl supports four main loop types while, until, for, and foreach Sreejith S Let’s Learn Perl
  121. 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. 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. 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. 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. 125. Control Structures - For Loop For loop syntax is similar to c. Sreejith S Let’s Learn Perl
  126. 126. Control Structures - For Loop For loop syntax is similar to c. There are variations Sreejith S Let’s Learn Perl
  127. 127. Control Structures - For Loop For loop syntax is similar to c. There are variations Syntax 1: Sreejith S Let’s Learn Perl
  128. 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. 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. 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. 131. Control Structures - ForEach Loop Syntax 3: Sreejith S Let’s Learn Perl
  132. 132. Control Structures - ForEach Loop Syntax 3: foreach $i (a,b,c) { print uc $i; } Sreejith S Let’s Learn Perl
  133. 133. Default scalar variable $ is called default variable Sreejith S Let’s Learn Perl
  134. 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. 135. Accepting input Keyboard inputs can be accepted using STDIN Sreejith S Let’s Learn Perl
  136. 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. 137. Subroutines Subroutines can be defined using sub keyword Sreejith S Let’s Learn Perl
  138. 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. 139. Scope of variables By default all variables are global Sreejith S Let’s Learn Perl
  140. 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. 141. File handling File handling can be done very easily Sreejith S Let’s Learn Perl
  142. 142. File handling File handling can be done very easily To open a file in read mode Sreejith S Let’s Learn Perl
  143. 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. 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. 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. 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. 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. 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. 149. Some Common String Functions There are many built in string functions Sreejith S Let’s Learn Perl
  150. 150. Some Common String Functions There are many built in string functions Convert to upper case Sreejith S Let’s Learn Perl
  151. 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. 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. 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. 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. 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. 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. 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. 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. 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. 160. Some Common Functions There are many built in functions Sreejith S Let’s Learn Perl
  161. 161. Some Common Functions There are many built in functions Strings to Arrays : split Sreejith S Let’s Learn Perl
  162. 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. 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. 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. 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. 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. 167. Web Resource for Perl Web resources for Perl Sreejith S Let’s Learn Perl
  168. 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. 169. Recommended Reading Sreejith S Let’s Learn Perl
  170. 170. Questions Sreejith S Let’s Learn Perl
  171. 171. Thank You Sreejith S Let’s Learn Perl
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×