perl
DESCRIPTION
Introfuction to PerlTRANSCRIPT
![Page 1: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/1.jpg)
Let’s Learn PerlAn introduction to Perl
Sreejith SNLP Process Analyst365Media Pvt. Ltd.
January 22, 2011
Sreejith S Let’s Learn Perl
![Page 2: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/2.jpg)
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
![Page 3: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/3.jpg)
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 thanbeautiful (tiny, elegant, minimal)
Perl was developed by Larry Wall.
Sreejith S Let’s Learn Perl
![Page 4: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/4.jpg)
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 thanbeautiful (tiny, elegant, minimal)
Perl was developed by Larry Wall.
Sreejith S Let’s Learn Perl
![Page 5: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/5.jpg)
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 thanbeautiful (tiny, elegant, minimal)
Perl was developed by Larry Wall.
Sreejith S Let’s Learn Perl
![Page 6: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/6.jpg)
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 thanbeautiful (tiny, elegant, minimal)
Perl was developed by Larry Wall.
Sreejith S Let’s Learn Perl
![Page 7: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/7.jpg)
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 thanbeautiful (tiny, elegant, minimal)
Perl was developed by Larry Wall.
Sreejith S Let’s Learn Perl
![Page 8: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/8.jpg)
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 thanbeautiful (tiny, elegant, minimal)
Perl was developed by Larry Wall.
Sreejith S Let’s Learn Perl
![Page 9: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/9.jpg)
To install...
Perl Comes by Default on Linux, Cygwin, MacOSX
www.perl.com has rpm’s for Linuxwww.activestate.com has binaries for Windows
Latest Version is 5.10To check if Perl is working and the version number
perl -v or perl -version
Sreejith S Let’s Learn Perl
![Page 10: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/10.jpg)
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.10To check if Perl is working and the version number
perl -v or perl -version
Sreejith S Let’s Learn Perl
![Page 11: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/11.jpg)
To install...
Perl Comes by Default on Linux, Cygwin, MacOSX
www.perl.com has rpm’s for Linuxwww.activestate.com has binaries for Windows
Latest Version is 5.10To check if Perl is working and the version number
perl -v or perl -version
Sreejith S Let’s Learn Perl
![Page 12: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/12.jpg)
To install...
Perl Comes by Default on Linux, Cygwin, MacOSX
www.perl.com has rpm’s for Linuxwww.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
![Page 13: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/13.jpg)
To install...
Perl Comes by Default on Linux, Cygwin, MacOSX
www.perl.com has rpm’s for Linuxwww.activestate.com has binaries for Windows
Latest Version is 5.10To check if Perl is working and the version number
perl -v or perl -version
Sreejith S Let’s Learn Perl
![Page 14: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/14.jpg)
To install...
Perl Comes by Default on Linux, Cygwin, MacOSX
www.perl.com has rpm’s for Linuxwww.activestate.com has binaries for Windows
Latest Version is 5.10To check if Perl is working and the version number
perl -v or perl -version
Sreejith S Let’s Learn Perl
![Page 15: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/15.jpg)
Sreejith S Let’s Learn Perl
![Page 16: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/16.jpg)
Basic Syntax of Perl
Comments
begin with #dont try to use // or /* ... */
print is simple function to display/output something onmonitor/stdoute.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
![Page 17: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/17.jpg)
Basic Syntax of Perl
Comments
begin with #
dont try to use // or /* ... */
print is simple function to display/output something onmonitor/stdoute.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
![Page 18: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/18.jpg)
Basic Syntax of Perl
Comments
begin with #dont try to use // or /* ... */
print is simple function to display/output something onmonitor/stdoute.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
![Page 19: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/19.jpg)
Basic Syntax of Perl
Comments
begin with #dont try to use // or /* ... */
print is simple function to display/output something onmonitor/stdoute.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
![Page 20: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/20.jpg)
Basic Syntax of Perl
Comments
begin with #dont try to use // or /* ... */
print is simple function to display/output something onmonitor/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
![Page 21: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/21.jpg)
Basic Syntax of Perl
Comments
begin with #dont try to use // or /* ... */
print is simple function to display/output something onmonitor/stdoute.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
![Page 22: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/22.jpg)
Basic Syntax of Perl
Comments
begin with #dont try to use // or /* ... */
print is simple function to display/output something onmonitor/stdoute.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
![Page 23: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/23.jpg)
Basic Syntax of Perl
Comments
begin with #dont try to use // or /* ... */
print is simple function to display/output something onmonitor/stdoute.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
![Page 24: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/24.jpg)
Basic Syntax of Perl
Comments
begin with #dont try to use // or /* ... */
print is simple function to display/output something onmonitor/stdoute.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
![Page 25: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/25.jpg)
Basic Syntax of Perl
Comments
begin with #dont try to use // or /* ... */
print is simple function to display/output something onmonitor/stdoute.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
![Page 26: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/26.jpg)
Basic Syntax of Perl
Comments
begin with #dont try to use // or /* ... */
print is simple function to display/output something onmonitor/stdoute.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
![Page 27: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/27.jpg)
Basic Syntax of Perl
Comments
begin with #dont try to use // or /* ... */
print is simple function to display/output something onmonitor/stdoute.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
![Page 28: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/28.jpg)
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
![Page 29: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/29.jpg)
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
![Page 30: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/30.jpg)
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
![Page 31: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/31.jpg)
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
![Page 32: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/32.jpg)
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
![Page 33: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/33.jpg)
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
![Page 34: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/34.jpg)
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
![Page 35: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/35.jpg)
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
![Page 36: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/36.jpg)
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
![Page 37: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/37.jpg)
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 timeDepending on the context will be treated as int, float etc.
There are 4 kinds of variables namely
ScalarsListsArraysHashes.
Sreejith S Let’s Learn Perl
![Page 38: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/38.jpg)
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 timeDepending on the context will be treated as int, float etc.
There are 4 kinds of variables namely
ScalarsListsArraysHashes.
Sreejith S Let’s Learn Perl
![Page 39: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/39.jpg)
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 timeDepending on the context will be treated as int, float etc.
There are 4 kinds of variables namely
ScalarsListsArraysHashes.
Sreejith S Let’s Learn Perl
![Page 40: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/40.jpg)
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 timeDepending on the context will be treated as int, float etc.
There are 4 kinds of variables namely
ScalarsListsArraysHashes.
Sreejith S Let’s Learn Perl
![Page 41: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/41.jpg)
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 timeDepending on the context will be treated as int, float etc.
There are 4 kinds of variables namely
ScalarsListsArraysHashes.
Sreejith S Let’s Learn Perl
![Page 42: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/42.jpg)
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
ScalarsListsArraysHashes.
Sreejith S Let’s Learn Perl
![Page 43: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/43.jpg)
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 timeDepending on the context will be treated as int, float etc.
There are 4 kinds of variables namely
ScalarsListsArraysHashes.
Sreejith S Let’s Learn Perl
![Page 44: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/44.jpg)
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 timeDepending on the context will be treated as int, float etc.
There are 4 kinds of variables namely
ScalarsListsArraysHashes.
Sreejith S Let’s Learn Perl
![Page 45: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/45.jpg)
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 timeDepending on the context will be treated as int, float etc.
There are 4 kinds of variables namely
Scalars
ListsArraysHashes.
Sreejith S Let’s Learn Perl
![Page 46: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/46.jpg)
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 timeDepending on the context will be treated as int, float etc.
There are 4 kinds of variables namely
ScalarsLists
ArraysHashes.
Sreejith S Let’s Learn Perl
![Page 47: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/47.jpg)
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 timeDepending on the context will be treated as int, float etc.
There are 4 kinds of variables namely
ScalarsListsArrays
Hashes.
Sreejith S Let’s Learn Perl
![Page 48: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/48.jpg)
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 timeDepending on the context will be treated as int, float etc.
There are 4 kinds of variables namely
ScalarsListsArraysHashes.
Sreejith S Let’s Learn Perl
![Page 49: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/49.jpg)
Scalars
Scalar meaning single value
Name of scalar variable must begin with $ symbol.
next character must a letter orremaining characters: letters, numbers, orname 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
![Page 50: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/50.jpg)
Scalars
Scalar meaning single value
Name of scalar variable must begin with $ symbol.
next character must a letter orremaining characters: letters, numbers, orname 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
![Page 51: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/51.jpg)
Scalars
Scalar meaning single value
Name of scalar variable must begin with $ symbol.
next character must a letter or
remaining characters: letters, numbers, orname 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
![Page 52: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/52.jpg)
Scalars
Scalar meaning single value
Name of scalar variable must begin with $ symbol.
next character must a letter orremaining 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
![Page 53: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/53.jpg)
Scalars
Scalar meaning single value
Name of scalar variable must begin with $ symbol.
next character must a letter orremaining characters: letters, numbers, orname 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
![Page 54: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/54.jpg)
Scalars
Scalar meaning single value
Name of scalar variable must begin with $ symbol.
next character must a letter orremaining characters: letters, numbers, orname 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
![Page 55: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/55.jpg)
Scalars
Scalar meaning single value
Name of scalar variable must begin with $ symbol.
next character must a letter orremaining characters: letters, numbers, orname 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
![Page 56: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/56.jpg)
Scalars
Scalar meaning single value
Name of scalar variable must begin with $ symbol.
next character must a letter orremaining characters: letters, numbers, orname 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
![Page 57: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/57.jpg)
Scalars
Scalar meaning single value
Name of scalar variable must begin with $ symbol.
next character must a letter orremaining characters: letters, numbers, orname 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
![Page 58: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/58.jpg)
Scalars
Scalar meaning single value
Name of scalar variable must begin with $ symbol.
next character must a letter orremaining characters: letters, numbers, orname 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
![Page 59: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/59.jpg)
Scalars
Scalar meaning single value
Name of scalar variable must begin with $ symbol.
next character must a letter orremaining characters: letters, numbers, orname 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
![Page 60: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/60.jpg)
Scalars
Scalar meaning single value
Name of scalar variable must begin with $ symbol.
next character must a letter orremaining characters: letters, numbers, orname 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
![Page 61: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/61.jpg)
Scalars
Scalar meaning single value
Name of scalar variable must begin with $ symbol.
next character must a letter orremaining characters: letters, numbers, orname 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
![Page 62: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/62.jpg)
Scalars
Scalar meaning single value
Name of scalar variable must begin with $ symbol.
next character must a letter orremaining characters: letters, numbers, orname 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
![Page 63: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/63.jpg)
Scalars
Scalar meaning single value
Name of scalar variable must begin with $ symbol.
next character must a letter orremaining characters: letters, numbers, orname 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
![Page 64: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/64.jpg)
Scalars
Scalar meaning single value
Name of scalar variable must begin with $ symbol.
next character must a letter orremaining characters: letters, numbers, orname 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
![Page 65: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/65.jpg)
Scalars
Scalar meaning single value
Name of scalar variable must begin with $ symbol.
next character must a letter orremaining characters: letters, numbers, orname 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
![Page 66: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/66.jpg)
Scalars
Scalar meaning single value
Name of scalar variable must begin with $ symbol.
next character must a letter orremaining characters: letters, numbers, orname 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
![Page 67: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/67.jpg)
Variable & Character Interpolation
Perl looks for variables inside strings and replaces them with theirvalue
$name = "Sree";
print "$name is my name\n";
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 doublequoted strings
All escape characters are recognized
print "Hello World!\n";
# It prints Hello World and then a return
Sreejith S Let’s Learn Perl
![Page 68: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/68.jpg)
Variable & Character Interpolation
Perl looks for variables inside strings and replaces them with theirvalue
$name = "Sree";
print "$name is my name\n";
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 doublequoted strings
All escape characters are recognized
print "Hello World!\n";
# It prints Hello World and then a return
Sreejith S Let’s Learn Perl
![Page 69: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/69.jpg)
Variable & Character Interpolation
Perl looks for variables inside strings and replaces them with theirvalue
$name = "Sree";
print "$name is my name\n";
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 doublequoted strings
All escape characters are recognized
print "Hello World!\n";
# It prints Hello World and then a return
Sreejith S Let’s Learn Perl
![Page 70: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/70.jpg)
Variable & Character Interpolation
Perl looks for variables inside strings and replaces them with theirvalue
$name = "Sree";
print "$name is my name\n";
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 doublequoted strings
All escape characters are recognized
print "Hello World!\n";
# It prints Hello World and then a return
Sreejith S Let’s Learn Perl
![Page 71: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/71.jpg)
Variable & Character Interpolation
Perl looks for variables inside strings and replaces them with theirvalue
$name = "Sree";
print "$name is my name\n";
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 doublequoted strings
All escape characters are recognized
print "Hello World!\n";
# It prints Hello World and then a return
Sreejith S Let’s Learn Perl
![Page 72: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/72.jpg)
Variable & Character Interpolation
Perl looks for variables inside strings and replaces them with theirvalue
$name = "Sree";
print "$name is my name\n";
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 doublequoted strings
All escape characters are recognized
print "Hello World!\n";
# It prints Hello World and then a return
Sreejith S Let’s Learn Perl
![Page 73: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/73.jpg)
Variable & Character Interpolation
Perl looks for variables inside strings and replaces them with theirvalue
$name = "Sree";
print "$name is my name\n";
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 doublequoted strings
All escape characters are recognized
print "Hello World!\n";
# It prints Hello World and then a return
Sreejith S Let’s Learn Perl
![Page 74: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/74.jpg)
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 "$first\n";
List variables can be assigned like this
($name,$age)=(’Raman’,20);
print "name=$name\n";
Sreejith S Let’s Learn Perl
![Page 75: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/75.jpg)
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 "$first\n";
List variables can be assigned like this
($name,$age)=(’Raman’,20);
print "name=$name\n";
Sreejith S Let’s Learn Perl
![Page 76: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/76.jpg)
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 "$first\n";
List variables can be assigned like this
($name,$age)=(’Raman’,20);
print "name=$name\n";
Sreejith S Let’s Learn Perl
![Page 77: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/77.jpg)
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 "$first\n";
List variables can be assigned like this
($name,$age)=(’Raman’,20);
print "name=$name\n";
Sreejith S Let’s Learn Perl
![Page 78: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/78.jpg)
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 "$first\n";
List variables can be assigned like this
($name,$age)=(’Raman’,20);
print "name=$name\n";
Sreejith S Let’s Learn Perl
![Page 79: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/79.jpg)
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 "$first\n";
List variables can be assigned like this
($name,$age)=(’Raman’,20);
print "name=$name\n";
Sreejith S Let’s Learn Perl
![Page 80: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/80.jpg)
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 "$first\n";
List variables can be assigned like this
($name,$age)=(’Raman’,20);
print "name=$name\n";
Sreejith S Let’s Learn Perl
![Page 81: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/81.jpg)
Arrays
Array variables should have prefix ‘@‘
Arrays are used to store ordered values
Much more dynamic than C/C++
no declaration of size, typecan 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
![Page 82: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/82.jpg)
Arrays
Array variables should have prefix ‘@‘
Arrays are used to store ordered values
Much more dynamic than C/C++
no declaration of size, typecan 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
![Page 83: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/83.jpg)
Arrays
Array variables should have prefix ‘@‘
Arrays are used to store ordered values
Much more dynamic than C/C++
no declaration of size, typecan 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
![Page 84: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/84.jpg)
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
![Page 85: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/85.jpg)
Arrays
Array variables should have prefix ‘@‘
Arrays are used to store ordered values
Much more dynamic than C/C++
no declaration of size, typecan 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
![Page 86: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/86.jpg)
Arrays
Array variables should have prefix ‘@‘
Arrays are used to store ordered values
Much more dynamic than C/C++
no declaration of size, typecan 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
![Page 87: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/87.jpg)
Arrays
Array variables should have prefix ‘@‘
Arrays are used to store ordered values
Much more dynamic than C/C++
no declaration of size, typecan 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
![Page 88: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/88.jpg)
Arrays
Array variables should have prefix ‘@‘
Arrays are used to store ordered values
Much more dynamic than C/C++
no declaration of size, typecan 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
![Page 89: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/89.jpg)
Arrays
Array variables should have prefix ‘@‘
Arrays are used to store ordered values
Much more dynamic than C/C++
no declaration of size, typecan 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
![Page 90: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/90.jpg)
Arrays
Array variables should have prefix ‘@‘
Arrays are used to store ordered values
Much more dynamic than C/C++
no declaration of size, typecan 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
![Page 91: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/91.jpg)
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=$last\n";
To prepend to the beginning of an array : UNSHIFT@array=(3,4,5);
unshift @array,’2’;
print "array=@array\n";
To remove the first element of the array : SHIFT@array=(3,4,5);
unshift @array,’2’;
print "array=@array\n";
To access the whole array, use the whole arrayprint @array;
To access one element of the array : use $print $array[0]; # prints : 1
Sreejith S Let’s Learn Perl
![Page 92: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/92.jpg)
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=$last\n";
To prepend to the beginning of an array : UNSHIFT@array=(3,4,5);
unshift @array,’2’;
print "array=@array\n";
To remove the first element of the array : SHIFT@array=(3,4,5);
unshift @array,’2’;
print "array=@array\n";
To access the whole array, use the whole arrayprint @array;
To access one element of the array : use $print $array[0]; # prints : 1
Sreejith S Let’s Learn Perl
![Page 93: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/93.jpg)
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=$last\n";
To prepend to the beginning of an array : UNSHIFT@array=(3,4,5);
unshift @array,’2’;
print "array=@array\n";
To remove the first element of the array : SHIFT@array=(3,4,5);
unshift @array,’2’;
print "array=@array\n";
To access the whole array, use the whole arrayprint @array;
To access one element of the array : use $print $array[0]; # prints : 1
Sreejith S Let’s Learn Perl
![Page 94: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/94.jpg)
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=$last\n";
To prepend to the beginning of an array : UNSHIFT@array=(3,4,5);
unshift @array,’2’;
print "array=@array\n";
To remove the first element of the array : SHIFT@array=(3,4,5);
unshift @array,’2’;
print "array=@array\n";
To access the whole array, use the whole arrayprint @array;
To access one element of the array : use $print $array[0]; # prints : 1
Sreejith S Let’s Learn Perl
![Page 95: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/95.jpg)
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=$last\n";
To prepend to the beginning of an array : UNSHIFT
@array=(3,4,5);
unshift @array,’2’;
print "array=@array\n";
To remove the first element of the array : SHIFT@array=(3,4,5);
unshift @array,’2’;
print "array=@array\n";
To access the whole array, use the whole arrayprint @array;
To access one element of the array : use $print $array[0]; # prints : 1
Sreejith S Let’s Learn Perl
![Page 96: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/96.jpg)
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=$last\n";
To prepend to the beginning of an array : UNSHIFT@array=(3,4,5);
unshift @array,’2’;
print "array=@array\n";
To remove the first element of the array : SHIFT@array=(3,4,5);
unshift @array,’2’;
print "array=@array\n";
To access the whole array, use the whole arrayprint @array;
To access one element of the array : use $print $array[0]; # prints : 1
Sreejith S Let’s Learn Perl
![Page 97: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/97.jpg)
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=$last\n";
To prepend to the beginning of an array : UNSHIFT@array=(3,4,5);
unshift @array,’2’;
print "array=@array\n";
To remove the first element of the array : SHIFT
@array=(3,4,5);
unshift @array,’2’;
print "array=@array\n";
To access the whole array, use the whole arrayprint @array;
To access one element of the array : use $print $array[0]; # prints : 1
Sreejith S Let’s Learn Perl
![Page 98: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/98.jpg)
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=$last\n";
To prepend to the beginning of an array : UNSHIFT@array=(3,4,5);
unshift @array,’2’;
print "array=@array\n";
To remove the first element of the array : SHIFT@array=(3,4,5);
unshift @array,’2’;
print "array=@array\n";
To access the whole array, use the whole arrayprint @array;
To access one element of the array : use $print $array[0]; # prints : 1
Sreejith S Let’s Learn Perl
![Page 99: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/99.jpg)
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=$last\n";
To prepend to the beginning of an array : UNSHIFT@array=(3,4,5);
unshift @array,’2’;
print "array=@array\n";
To remove the first element of the array : SHIFT@array=(3,4,5);
unshift @array,’2’;
print "array=@array\n";
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
![Page 100: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/100.jpg)
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=$last\n";
To prepend to the beginning of an array : UNSHIFT@array=(3,4,5);
unshift @array,’2’;
print "array=@array\n";
To remove the first element of the array : SHIFT@array=(3,4,5);
unshift @array,’2’;
print "array=@array\n";
To access the whole array, use the whole arrayprint @array;
To access one element of the array : use $print $array[0]; # prints : 1
Sreejith S Let’s Learn Perl
![Page 101: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/101.jpg)
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=$last\n";
To prepend to the beginning of an array : UNSHIFT@array=(3,4,5);
unshift @array,’2’;
print "array=@array\n";
To remove the first element of the array : SHIFT@array=(3,4,5);
unshift @array,’2’;
print "array=@array\n";
To access the whole array, use the whole arrayprint @array;
To access one element of the array : use $
print $array[0]; # prints : 1
Sreejith S Let’s Learn Perl
![Page 102: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/102.jpg)
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=$last\n";
To prepend to the beginning of an array : UNSHIFT@array=(3,4,5);
unshift @array,’2’;
print "array=@array\n";
To remove the first element of the array : SHIFT@array=(3,4,5);
unshift @array,’2’;
print "array=@array\n";
To access the whole array, use the whole arrayprint @array;
To access one element of the array : use $print $array[0]; # prints : 1
Sreejith S Let’s Learn Perl
![Page 103: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/103.jpg)
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
![Page 104: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/104.jpg)
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
![Page 105: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/105.jpg)
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
![Page 106: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/106.jpg)
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
![Page 107: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/107.jpg)
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
![Page 108: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/108.jpg)
Hashes
Hash variables will have % as prefix
Key-Value pair
The contents of hash are called values and index is called key. thearray:
%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
![Page 109: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/109.jpg)
Hashes
Hash variables will have % as prefix
Key-Value pair
The contents of hash are called values and index is called key. thearray:
%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
![Page 110: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/110.jpg)
Hashes
Hash variables will have % as prefix
Key-Value pair
The contents of hash are called values and index is called key. thearray:
%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
![Page 111: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/111.jpg)
Hashes
Hash variables will have % as prefix
Key-Value pair
The contents of hash are called values and index is called key. thearray:
%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
![Page 112: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/112.jpg)
Hashes
Hash variables will have % as prefix
Key-Value pair
The contents of hash are called values and index is called key. thearray:
%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
![Page 113: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/113.jpg)
Hashes
Hash variables will have % as prefix
Key-Value pair
The contents of hash are called values and index is called key. thearray:
%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
![Page 114: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/114.jpg)
Hashes
Hash variables will have % as prefix
Key-Value pair
The contents of hash are called values and index is called key. thearray:
%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
![Page 115: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/115.jpg)
Conditionals - IF
The if statement is similar to if in C language, except
flower brace is required even for single statementelse 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
![Page 116: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/116.jpg)
Conditionals - IF
The if statement is similar to if in C language, except
flower brace is required even for single statementelse 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
![Page 117: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/117.jpg)
Conditionals - IF
The if statement is similar to if in C language, except
flower brace is required even for single statementelse 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
![Page 118: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/118.jpg)
Conditionals - IF
The if statement is similar to if in C language, except
flower brace is required even for single statementelse 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
![Page 119: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/119.jpg)
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 $i\n;
$i++;
}
Sreejith S Let’s Learn Perl
![Page 120: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/120.jpg)
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 $i\n;
$i++;
}
Sreejith S Let’s Learn Perl
![Page 121: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/121.jpg)
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 $i\n;
$i++;
}
Sreejith S Let’s Learn Perl
![Page 122: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/122.jpg)
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 $i\n;
$i++;
}
Sreejith S Let’s Learn Perl
![Page 123: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/123.jpg)
Control Structures - Until Loop
The until function evaluates an expression repeatedly until a specificcondition is met
$i = 0;
until ($i == 1000)
{
print $i\n;
$i++;
}
Sreejith S Let’s Learn Perl
![Page 124: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/124.jpg)
Control Structures - Until Loop
The until function evaluates an expression repeatedly until a specificcondition is met
$i = 0;
until ($i == 1000)
{
print $i\n;
$i++;
}
Sreejith S Let’s Learn Perl
![Page 125: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/125.jpg)
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
![Page 126: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/126.jpg)
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
![Page 127: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/127.jpg)
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
![Page 128: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/128.jpg)
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
![Page 129: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/129.jpg)
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
![Page 130: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/130.jpg)
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
![Page 131: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/131.jpg)
Control Structures - ForEach Loop
Syntax 3:
foreach $i (a,b,c)
{
print uc $i;
}
Sreejith S Let’s Learn Perl
![Page 132: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/132.jpg)
Control Structures - ForEach Loop
Syntax 3:
foreach $i (a,b,c)
{
print uc $i;
}
Sreejith S Let’s Learn Perl
![Page 133: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/133.jpg)
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
![Page 134: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/134.jpg)
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
![Page 135: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/135.jpg)
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 $name\n";
Sreejith S Let’s Learn Perl
![Page 136: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/136.jpg)
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 $name\n";
Sreejith S Let’s Learn Perl
![Page 137: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/137.jpg)
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
![Page 138: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/138.jpg)
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
![Page 139: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/139.jpg)
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
![Page 140: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/140.jpg)
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
![Page 141: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/141.jpg)
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 to new file
}
close($fh);
close($fh1);
Sreejith S Let’s Learn Perl
![Page 142: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/142.jpg)
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 to new file
}
close($fh);
close($fh1);
Sreejith S Let’s Learn Perl
![Page 143: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/143.jpg)
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 to new file
}
close($fh);
close($fh1);
Sreejith S Let’s Learn Perl
![Page 144: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/144.jpg)
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 to new file
}
close($fh);
close($fh1);
Sreejith S Let’s Learn Perl
![Page 145: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/145.jpg)
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 to new file
}
close($fh);
close($fh1);
Sreejith S Let’s Learn Perl
![Page 146: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/146.jpg)
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 to new file
}
close($fh);
close($fh1);
Sreejith S Let’s Learn Perl
![Page 147: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/147.jpg)
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 to new file
}
close($fh);
close($fh1);
Sreejith S Let’s Learn Perl
![Page 148: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/148.jpg)
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 to new file
}
close($fh);
close($fh1);
Sreejith S Let’s Learn Perl
![Page 149: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/149.jpg)
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
![Page 150: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/150.jpg)
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
![Page 151: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/151.jpg)
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
![Page 152: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/152.jpg)
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
![Page 153: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/153.jpg)
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
![Page 154: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/154.jpg)
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
![Page 155: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/155.jpg)
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
![Page 156: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/156.jpg)
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
![Page 157: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/157.jpg)
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
![Page 158: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/158.jpg)
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
![Page 159: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/159.jpg)
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
![Page 160: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/160.jpg)
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
![Page 161: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/161.jpg)
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
![Page 162: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/162.jpg)
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
![Page 163: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/163.jpg)
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
![Page 164: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/164.jpg)
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
![Page 165: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/165.jpg)
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
![Page 166: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/166.jpg)
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
![Page 167: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/167.jpg)
Web Resource for Perl
Web resources for Perl
www.perl.comwww.perl.orgwww.perlmonks.org
Sreejith S Let’s Learn Perl
![Page 168: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/168.jpg)
Web Resource for Perl
Web resources for Perl
www.perl.comwww.perl.orgwww.perlmonks.org
Sreejith S Let’s Learn Perl
![Page 169: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/169.jpg)
Recommended Reading
Sreejith S Let’s Learn Perl
![Page 170: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/170.jpg)
Questions
Sreejith S Let’s Learn Perl
![Page 171: Perl](https://reader034.vdocuments.us/reader034/viewer/2022042700/55501289b4c90555618b4aec/html5/thumbnails/171.jpg)
Thank You
Sreejith S Let’s Learn Perl