perl scripting
DESCRIPTION
My presentation for GTUG Summer Camp 2010,Madurai on "Introduction to Perl"TRANSCRIPT
![Page 1: Perl Scripting](https://reader035.vdocuments.us/reader035/viewer/2022062303/555a605cd8b42ae7218b46c8/html5/thumbnails/1.jpg)
Perl Scripting
M. Varadharajan
Thiagarajar College of Engineering
![Page 2: Perl Scripting](https://reader035.vdocuments.us/reader035/viewer/2022062303/555a605cd8b42ae7218b46c8/html5/thumbnails/2.jpg)
What We Will Cover?
What is Perl? Creating and Executing Perl scripts Standard Input and Output Scalar Variables Arrays Hashes Magic Variables: $_ and @ARGV
![Page 3: Perl Scripting](https://reader035.vdocuments.us/reader035/viewer/2022062303/555a605cd8b42ae7218b46c8/html5/thumbnails/3.jpg)
What We Will Cover?
Control Structures Looping Structures File Operations Split & Join Using shell commands Advanced Concepts you'll need to know
![Page 4: Perl Scripting](https://reader035.vdocuments.us/reader035/viewer/2022062303/555a605cd8b42ae7218b46c8/html5/thumbnails/4.jpg)
What is Perl
Perl stands for − 'Practical Extraction and Reporting Language'
Developed by Larry Wall in 1987 Its called Perl and not PERL High level Scripting Language Dynamically Typed Support for object oriented programming
![Page 5: Perl Scripting](https://reader035.vdocuments.us/reader035/viewer/2022062303/555a605cd8b42ae7218b46c8/html5/thumbnails/5.jpg)
Some Advantages of Perl
Free and Open source Fast, Flexible, Secure and Fun Interpreted Language Mature Community Portability Very good Documentation (POD) Availability of Modules (CPAN)
![Page 6: Perl Scripting](https://reader035.vdocuments.us/reader035/viewer/2022062303/555a605cd8b42ae7218b46c8/html5/thumbnails/6.jpg)
Typical Uses of Perl
Text processing System administration tasks CGI and web programming Database interaction Other Internet programming
![Page 7: Perl Scripting](https://reader035.vdocuments.us/reader035/viewer/2022062303/555a605cd8b42ae7218b46c8/html5/thumbnails/7.jpg)
Hello World!
This script will print 'Hello World!' Creation of the Perl Script:
− Open your Text Editor (!MSWORD)− Type the following block & save
#!/usr/bin/perl -w
print “Hello World! \n”;
![Page 8: Perl Scripting](https://reader035.vdocuments.us/reader035/viewer/2022062303/555a605cd8b42ae7218b46c8/html5/thumbnails/8.jpg)
Hello World!
Some point to Note:− All Perl statements end with ';'− Add 'use strict;' if you're serious on the
script− Comments in Perl start with '#'− The first line is known as Shebang line
#!/usr/bin/perl -w
![Page 9: Perl Scripting](https://reader035.vdocuments.us/reader035/viewer/2022062303/555a605cd8b42ae7218b46c8/html5/thumbnails/9.jpg)
Hello World!
Executing the script:− Call the interpreter with the script
perl helloworld.pl
or
− Grant Executable Permissions & Execute Chmod a+x helloworld.pl
./helloworld.pl
![Page 10: Perl Scripting](https://reader035.vdocuments.us/reader035/viewer/2022062303/555a605cd8b42ae7218b46c8/html5/thumbnails/10.jpg)
Scalar Variables
Place to store a single item of data Scalar variables begin with '$' Declaration is as follows (in strict mode)
my $name; Assigning values is similar to c
$name = “varadharajan”;
$total = 100;
$cost = 34.34
![Page 11: Perl Scripting](https://reader035.vdocuments.us/reader035/viewer/2022062303/555a605cd8b42ae7218b46c8/html5/thumbnails/11.jpg)
Standard Output
Print function is used Syntax:
print “some string”;
Example: (script prints “Perl is cool”)#/usr/bin/perl -w
my $name = “perl”;
print “$name is cool \n”;
![Page 12: Perl Scripting](https://reader035.vdocuments.us/reader035/viewer/2022062303/555a605cd8b42ae7218b46c8/html5/thumbnails/12.jpg)
Standard Input
Special operator '<>' is used Synatx:
$scalar = <STDIN>; Example: (Get name and print it)
#/usr/bin/perl -w
print “Enter Name : ”;
my $name = <STDIN>;
print “Hello $name”;
![Page 13: Perl Scripting](https://reader035.vdocuments.us/reader035/viewer/2022062303/555a605cd8b42ae7218b46c8/html5/thumbnails/13.jpg)
String Operations
Chomp:chomp($name);
#removes the trailing new line Concatenation:
my $name = “Varadharajan ” . “Mukundan”; Multiplication:
$name = “hello ” x 3;
#Assigns “hello hello hello” to name
![Page 14: Perl Scripting](https://reader035.vdocuments.us/reader035/viewer/2022062303/555a605cd8b42ae7218b46c8/html5/thumbnails/14.jpg)
Arrays
Set of Scalar variables Arrays start with '@' Declaring Arrays:
− Syntax:my @array_name=(value1,value2);
− Example:my @list = ('varadharajan',99,'cool');
![Page 15: Perl Scripting](https://reader035.vdocuments.us/reader035/viewer/2022062303/555a605cd8b42ae7218b46c8/html5/thumbnails/15.jpg)
Arrays Accessing individual elements:
− Syntax:$array_name[index];
#index starts with 0
− Example:print $list[1]; #prints 10
![Page 16: Perl Scripting](https://reader035.vdocuments.us/reader035/viewer/2022062303/555a605cd8b42ae7218b46c8/html5/thumbnails/16.jpg)
Array Slices
Access a set of continuous elements in an array.
− Syntax:@array_name[start_index .. end_index];
− Example:print @list[ 0 .. 2 ];
# Prints $list[0], $list[1], $list[2]
![Page 17: Perl Scripting](https://reader035.vdocuments.us/reader035/viewer/2022062303/555a605cd8b42ae7218b46c8/html5/thumbnails/17.jpg)
Hashes
“Key – value ” Data Structure. Keys present in a hash must be unique Value may be same for multiple keys Also commonly known as dictionaries
![Page 18: Perl Scripting](https://reader035.vdocuments.us/reader035/viewer/2022062303/555a605cd8b42ae7218b46c8/html5/thumbnails/18.jpg)
Hashes
Initializing a Hash:− Syntax:
my %hash_name = ( key => 'value');
− Example:my %students = (
name => 'varadharajan', age => 1
);
![Page 19: Perl Scripting](https://reader035.vdocuments.us/reader035/viewer/2022062303/555a605cd8b42ae7218b46c8/html5/thumbnails/19.jpg)
Hashes
Accessing a Hash− Syntax:
$hash_name{key_name};
− Example:print $student{name};
#prints varadharajan
print $student{age};
#prints 18
![Page 20: Perl Scripting](https://reader035.vdocuments.us/reader035/viewer/2022062303/555a605cd8b42ae7218b46c8/html5/thumbnails/20.jpg)
Hash Slices
Just like array slices Syntax:
@hash_name{'key1','key2'}; Example:
print @student{'name','age'};
![Page 21: Perl Scripting](https://reader035.vdocuments.us/reader035/viewer/2022062303/555a605cd8b42ae7218b46c8/html5/thumbnails/21.jpg)
Magic Variable: $_
Default variable for storing values, if no variables are manually specified.
Example:my @list = (1,2,4,34,5,223);
foreach (@list)
{
print;
}
# prints the entire list
![Page 22: Perl Scripting](https://reader035.vdocuments.us/reader035/viewer/2022062303/555a605cd8b42ae7218b46c8/html5/thumbnails/22.jpg)
Magic Variable: @ARGV
This Array is used to store the command line arguments
Exampleprint $ARGV[0];
# when this script is executed like this
# perl test1.pl text
# it prints “text”
![Page 23: Perl Scripting](https://reader035.vdocuments.us/reader035/viewer/2022062303/555a605cd8b42ae7218b46c8/html5/thumbnails/23.jpg)
Conditional control Structures
IF – ELSIF – ELSE statement:− Syntax:
if (EXPR) {BLOCK}
elsif (EXPR) {BLOCK}
else {BLOCK}
− Example:if($age==18) {print “Eighteen”;}
elsif($age==19) {print “Nineteen”}
else {print $age;}
![Page 24: Perl Scripting](https://reader035.vdocuments.us/reader035/viewer/2022062303/555a605cd8b42ae7218b46c8/html5/thumbnails/24.jpg)
Looping Structures
While:$i = 0;
while ($i < 10)
{
print $i;
$i++;
}
# Prints 0123456789
![Page 25: Perl Scripting](https://reader035.vdocuments.us/reader035/viewer/2022062303/555a605cd8b42ae7218b46c8/html5/thumbnails/25.jpg)
Looping Structures
For:for($i=0;$i<10;$i++)
{
print $i;
}
# prints 0123456789
![Page 26: Perl Scripting](https://reader035.vdocuments.us/reader035/viewer/2022062303/555a605cd8b42ae7218b46c8/html5/thumbnails/26.jpg)
Looping Structures
Foreach:my @list = (“varadha”,19);
foreach $value (@list)
{
print $value;
}
# prints the list
![Page 27: Perl Scripting](https://reader035.vdocuments.us/reader035/viewer/2022062303/555a605cd8b42ae7218b46c8/html5/thumbnails/27.jpg)
File Operations
Opening a File:− Syntax:
open(FILE_HANDLE , “[< |> |>>]File_Name”);
− Example:open(MYFILE, “<myfile.txt”);
− Available Modes:< - Read Mode
> - Write Mode
>> - Append Mode
![Page 28: Perl Scripting](https://reader035.vdocuments.us/reader035/viewer/2022062303/555a605cd8b42ae7218b46c8/html5/thumbnails/28.jpg)
File Operations
Reading from a File:− Syntax:
@array_name = <FILE_HANDLE>;
− Example:@data = <MYFILE>;
# Now @data contains the data presents in
# File whose file handle is MYFILE
![Page 29: Perl Scripting](https://reader035.vdocuments.us/reader035/viewer/2022062303/555a605cd8b42ae7218b46c8/html5/thumbnails/29.jpg)
File Operations
Writing to a File:− Syntax:
print FILE_HANDLE “Text”;
− Example:print MYFILE “This is the content”;
![Page 30: Perl Scripting](https://reader035.vdocuments.us/reader035/viewer/2022062303/555a605cd8b42ae7218b46c8/html5/thumbnails/30.jpg)
File Operations
Closing a File:− Syntax:
close(FILE_HANDLE);
− Example:close(MYFILE);
![Page 31: Perl Scripting](https://reader035.vdocuments.us/reader035/viewer/2022062303/555a605cd8b42ae7218b46c8/html5/thumbnails/31.jpg)
Split Function
Splits a scalar variable into arrays− Syntax:
@array = split(PATTERN,EXPR);
− Example:@words = split(/ /,$sentence);
![Page 32: Perl Scripting](https://reader035.vdocuments.us/reader035/viewer/2022062303/555a605cd8b42ae7218b46c8/html5/thumbnails/32.jpg)
Join Function
Used to join all elements in an array to form a scalar
− Syntax:$string = join(Joining_element,@arrays);
− Example:$sentence = join(' ',@words);
![Page 33: Perl Scripting](https://reader035.vdocuments.us/reader035/viewer/2022062303/555a605cd8b42ae7218b46c8/html5/thumbnails/33.jpg)
Executing Shell Commands
Makes us executed Shell commands from a Perl script
− Syntax:system(command);
− Example:$ls_data = system(“ls”);
![Page 34: Perl Scripting](https://reader035.vdocuments.us/reader035/viewer/2022062303/555a605cd8b42ae7218b46c8/html5/thumbnails/34.jpg)
Advanced Concepts
Subroutines Global and Local variables Regular Expressions OO programming CPAN
![Page 35: Perl Scripting](https://reader035.vdocuments.us/reader035/viewer/2022062303/555a605cd8b42ae7218b46c8/html5/thumbnails/35.jpg)
Perl Resources
Perl POD Learning Perl from o'reilly Programming Perl from o'reilly Perl Beginners Mailing list at
http://www.nntp.perl.org/group/perl.beginners/