Professional Documents
Culture Documents
Perl Scripting: M. Varadharajan Thiagarajar College of Engineering
Perl Scripting: M. Varadharajan Thiagarajar College of Engineering
What is Perl? Creating and Executing Perl scripts Standard Input and Output Scalar Variables Arrays Hashes Magic Variables: $_ and @ARGV
Control Structures Looping Structures File Operations Split & Join Using shell commands Advanced Concepts you'll need to know
What is Perl
Developed by Larry Wall in 1987 Its called Perl and not PERL High level Scripting Language Dynamically Typed Support for object oriented programming
Free and Open source Fast, Flexible, Secure and Fun Interpreted Language Mature Community Portability Very good Documentation (POD) Availability of Modules (CPAN)
Text processing System administration tasks CGI and web programming Database interaction Other Internet programming
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;
Hello World!
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
Hello World!
or
Scalar Variables
Place to store a single item of data Scalar variables begin with '$' Declaration is as follows (in strict mode)
my $name;
Standard Output
Standard Input
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
Arrays
Syntax:
my @array_name=(value1,value2);
Example:
my @list = ('varadharajan',99,'cool');
Arrays
Syntax:
$array_name[index]; #index starts with 0
Example:
print $list[1]; #prints 10
Array Slices
Syntax:
@array_name[start_index .. end_index];
Example:
print @list[ 0 .. 2 ]; # Prints $list[0], $list[1], $list[2]
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
Hashes
Initializing a Hash:
Syntax:
my %hash_name = ( key => 'value');
Example:
my %students = ( name => 'varadharajan', age => 1 );
Hashes
Accessing a Hash
Syntax:
$hash_name{key_name};
Example:
print $student{name}; #prints varadharajan print $student{age}; #prints 18
Hash Slices
Example:
print @student{'name','age'};
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
Syntax:
if (EXPR) {BLOCK} elsif (EXPR) {BLOCK} else {BLOCK}
Example:
if($age==18) {print Eighteen;} elsif($age==19) {print Nineteen} else {print $age;}
Looping Structures
While:
$i = 0; while ($i < 10) { print $i; $i++; } # Prints 0123456789
Looping Structures
For:
for($i=0;$i<10;$i++) { print $i; } # prints 0123456789
Looping Structures
Foreach:
my @list = (varadha,19); foreach $value (@list) { print $value; } # prints the list
File Operations
Opening a File:
Syntax:
open(FILE_HANDLE , [< |> |>>]File_Name);
Example:
open(MYFILE, <myfile.txt);
Available Modes:
< - Read Mode > - Write Mode >> - Append Mode
File Operations
Syntax:
@array_name = <FILE_HANDLE>;
Example:
@data = <MYFILE>; # Now @data contains the data presents in # File whose file handle is MYFILE
File Operations
Writing to a File:
Syntax:
print FILE_HANDLE Text;
Example:
print MYFILE This is the content;
File Operations
Closing a File:
Syntax:
close(FILE_HANDLE);
Example:
close(MYFILE);
Split Function
Syntax:
@array = split(PATTERN,EXPR);
Example:
@words = split(/ /,$sentence);
Join Function
Syntax:
$string = join(Joining_element,@arrays);
Example:
$sentence = join(' ',@words);
Syntax:
system(command);
Example:
$ls_data = system(ls);
Advanced Concepts
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.beginn ers/
Ping me at srinathsmn@gmail.com
Thank You