The document provides an overview of the Perl Scripting course contents which includes 13 topics such as introduction to Perl, scalars, operators, conditions and loops, arrays and hashes, subroutines, regular expressions, references and complex data structures, debugging in Perl, packages and modules, managing and working with files and I/O, and handling databases in Perl. It also discusses the basics of Perl including its history, features, installing Perl, writing simple programs, data types, variables, inputs, outputs and functions.
The document provides an overview of the Perl Scripting course contents which includes 13 topics such as introduction to Perl, scalars, operators, conditions and loops, arrays and hashes, subroutines, regular expressions, references and complex data structures, debugging in Perl, packages and modules, managing and working with files and I/O, and handling databases in Perl. It also discusses the basics of Perl including its history, features, installing Perl, writing simple programs, data types, variables, inputs, outputs and functions.
1. Introduction to Perl 2. Working with scalars (numbers & strings) 3. Operators and functions 4. Conditions & loops 5. Working with lists (arrays & hashes) 6. Sub-routines 7. Regular expressions Contd.. Contd.. 8. references and complex data structures 9. debugging in Perl 10. Packages & Modules 11. managing files and directories 12. working with files and I/O 13. handling databases in Perl 1. Introduction to Perl 1. Introduction to Perl y Perl stands for Practical Extraction and Report Language. y Perl is a mixture of UNIX tools (sed, grep, awk etc.), Shell Scripting, C and Object Oriented features. y Perl was created by Larry Wall. It is now not owned by any single Organization. Perl is defined and maintained by a group of volunteer programmers including Larry Wall. Features of Perl Features of Perl 1. Perl is free 2. Perl is both a compiled and interpreted language 3. Perl is portable 4. Perl can interface with other languages 5. Perl is easy to use and debug How to get Perl How to get Perl Perl can be downloaded from the Internet thru www.perl.org (or) www.perl.com The current version of Perl is 5.10.0. Perl is available on different platforms, Unix,Windows, MacOS etc. Writing the first program Writing the first program $ vi first.pl # program to print a message print Hello, World\n ; To run the program, $ perl first.pl About the Program About the Program Any line that starts with a #, is a comment To display messages, print command is used Every perl statement must end with a ; Command Line Options Command Line Options To know the current version of Perl $ perl v To check the syntax errors only but does not run the script $ perl c first.pl To turn on the warnings $ perl w first.pl Perl Online Help Perl Online Help The online help is available in pod (plain old documentation) format and can be invoked using perldoc command. $ perldoc perlfunc $ perldoc f <command> ($ perldoc f print) $ perldoc perldoc Data Types Data Types Perl has two types of data: 1. scalars: data consisting of a single thing ex: Numbers, Strings, References 2. lists: data consisting of a collection of single things or scalars ex: Arrays, Hashes 2. Scalars 2. Scalars A) Numbers: 4 45e-4 3.2 0xbeef .234567 012 5. 1_234_456 10E2 Perl does not differentiate between integers and floats, signed or unsigned, or short and long numbers. Perl converts between the number types as needed in our scripts Scalars Scalars B) Strings : A string is zero or more characters surrounded by single quotes ( ) or by double quotes ( ) Escape sequences are used for string formatting in print function. \n new line \t tab \b backspace Scalars Scalars \a bell sound \0nn octal \0xnn hexadecimal \l make next letter lower case \u make next letter upper case \L make all the following letters lower \U make all the following letters upper Scalars Scalars Converting between numbers and strings: Since, numbers and strings are scalar data, they are interchangeable. ex: 5 + 14 gives 19 foo + 5 gives 5 23abc + 7 gives 30 Scalar Variables Scalar Variables $a=5; $a=$b=5; $a=$b=5, $c=7; $s=abc; $t=xyz; Rules for variable names: 1.should start with a letter, underscore 2.case sensitive 3.can be up to 255 characters. We dont have to declare or initialize variables in the beginning. Input Input Reading scalar values from Keyboard: $a=<STDIN>; chomp ($a); When we enter values from the keyboard, \n is automatically attached to the input value. To remove \n from the input, we use chomp function. Output Output Displaying the values on the screen: print ($a\n); printf (%.2f\n,$b); $c=sprintf(%.2f , $b); 3. Operators & Functions 3. Operators & Functions Arithmetic operators: + (addition), - (subtraction), * (multiplication), / (division), % (remainder), ** (exponent) Operators Operators Relational Operators: Numeric Operators: ==, !=, <, >, <=, >= String Operators: eq, ne, lt, gt, le, ge Logical Operators: and (&&), or (||), not (!) Operators Operators Assignment Operators: +=, -=,*=, /=, %=, **= Increment and Decrement Operators: ++, -- Conditional Operator: exp1?exp2:exp3 Operators Operators Bitwise Operators: <<, >> String catenation operator: . String repetition operator: x Operators Operators Quoting the strings: Single quotes: q/how are you/ Double quotes: qq/how are you/ General quotes: qw (abc); Number Functions Number Functions abs () absolute value int () converts to integer sin () sine value cos () cosine value rand () random number exp () e to the power of atan2 () arctangent ord () ascii number sqrt () square root String Functions String Functions chr () ascii character index () returns the pos. of chr. length () length reverse () reverses a scalar rindex () reverse index substr () substring lc () lowercase conversion uc () upper case conversion lcfirst () first character lower uc first() first character upper 4. Conditions and Loops 4. Conditions and Loops if if (condition) { statements} if (condition) {statements} else {statements} Conditions and Loops Conditions and Loops if (condition) { statements } elsif (condition) { statements } Conditions and Loops Conditions and Loops unless unless (condition) { statements } Conditions and Loops Conditions and Loops while while (condition) { statements } Conditions and Loops Conditions and Loops until until (condition) { statements } Conditions and Loops Conditions and Loops do do {statements} while (condition); do {statements} until (condition); Conditions and Loops Conditions and Loops for for (init; test; change) { statements } Conditions and Loops Conditions and Loops foreach foreach value (list) { statements } Conditions and Loops Conditions and Loops Infinite loops while () { statements } for (;;) { statements } Conditions and Loops Conditions and Loops break from a control statement last continue next Conditions and Loops Conditions and Loops $_ variable It is the default placeholder for scalar values example foreach (`dir`) { print ($_\n); } Conditions and Loops Conditions and Loops switch use Switch; switch ($value) { case 1 {print 1; } . else {print 0 ;} } 5. Arrays and Hashes 5. Arrays and Hashes Creating Arrays: @nums = (1,2,3,4); @nums = (1..4); @strings = (ab , cd , ef ); @strings = (a ,z); @strings = (); @combine=(@nums, @strings); Arrays and Hashes Arrays and Hashes Printing the Array: print (@nums\n); print (@strings\n); print (@combine\n); Processing each element: foreach $x (@nums) { print ($x\n); } Arrays and Hashes Arrays and Hashes Accessing Array elements: $nums[3]; The subscript starts from 0 to n-1 Negative Array Indexes: $nums[-1]; Negative array subscripts will count back from the end of the array. Arrays and Hashes Arrays and Hashes Growing Arrays: @nums=(1..4); $nums[6]=7; Result is 1,2,3,4,undefined, undefined,7 Finding the end of the Array: $end=$#nums; Finding the length of the Array: $len=@nums; Arrays and Hashes Arrays and Hashes Sorting the Array: cmp (strings), < = > (numbers) @sorted=sort {$a cmp $b} @strings; @sorted=sort {$a < = > $b} @nums; Reversing the Array: @new=reverse (@old); Arrays and Hashes Arrays and Hashes push and pop functions allow you to add or remove elements from the end of the list $new=push (@nums,5); $new1=pop (@nums); Arrays and Hashes Arrays and Hashes shift and unshift remove the elements from the beginning of the Array $new=shift (@nums); $new1=unshift (@nums, 10); splice removes the elements from the Array @new=splice (@nums,5,3); Arrays and Hashes Arrays and Hashes slice creates pieces of the Array @array=(1..10); @slice=@array[0,1,2]; splitting the data into an Array $strings=12 23 43; @nums=split( ,$strings); joining the data from an Array $strings=join(+, @nums); Arrays and Hashes Arrays and Hashes Creating a Hash: %hash=(key=>value, key=>value, key=>value); Ex: %pairs=(red=>255, blue=>355, green=>455); Accessing Hash Elements: $hash {$key}; Ex: $pairs {red}; Printing the Hash: print %hash; Ex: print %pairs; Arrays and Hashes Arrays and Hashes Processing a Hash: foreach $key (sort keys %hash) { print $hash {$key}\n; } while (($key, $value) = each (%hash) { print $hash {$key} \n; } Arrays and Hashes Arrays and Hashes Ex: foreach $key (sort keys %pairs) { print $pairs {$key}\n; } or while (($key, $value) = each (%pairs)) { print $pairs {$key} \n; } Arrays and Hashes Arrays and Hashes Adding an element to a Hash: $hash {$key} = $value; Ex: $pairs {yellow}=555; Deleting an element from a Hash: delete ($hash {$key}); Ex: delete ($pairs {red}); Arrays and Hashes Arrays and Hashes Inverting a Hash: %new=reverse %hash; Ex: %newpairs = reverse %pairs; Finding the no. of keys in a Hash: $n=keys %hash; Ex: $n=keys %pairs; 6. Subroutines 6. Subroutines The terms function and subroutine are entirely equivalent in Perl. Perl handles three types of functions. 1. Built-in: These are the functions that are defined by the standard Perl library that we can use any where in our Perl scripts. Subroutines Subroutines 2. Additional: These are the functions that are available to us by using the additional Perl modules or libraries, written by other Perl programmers, that we can load in at the start of the script. 3. Subroutines: These are user defined functions. By convention, the term subroutine is used to identify the function as user defined one. Subroutines Subroutines Defining & Calling basic subroutines: print enter temperature in forenheight \n; chomp ($f=<STDIN>); &cal(); print The result is $cel\n; sub cal () { $cel=($f-32)*5/9; } Subroutines Subroutines Returning values from a subroutine: $sum=&cal(); print The sum is $sum\n; sub cal () { print enter two nos. \n; chomp ($n1=<STDIN>); chomp ($n2=<STDIN>); return ($n1 + $n2); } Subroutines Subroutines Using local variables inside a subroutine: We can create local variables inside a subroutine using my modifier. $sum=&cal(); print The sum is $sum\n; sub cal () { my ($n1,$n2); print enter two nos. \n; chomp ($n1=<STDIN>); chomp ($n2=<STDIN>); return ($n1 + $n2); Subroutines Subroutines Passing Arguments to a subroutine: $sum=&cal(3,5); print The sum is $sum\n; sub cal () { my ($m, $n)= @_; my $p=1; while ($n>0) { $p=$p*$m; $n--; } return ($p); } Subroutines Subroutines Anonymous subroutines: These are subroutines without names and they operate sort of like pointers to functions in C. 7. REGULAR EXPRESSIONS 7. REGULAR EXPRESSIONS REGULAR EXPRESSION OPERATORS: m//, s///, tr/// @x = grep /x/, @words; PATTERN MATCHING OPERATORS: =~, !~ REs: abc exact character sequence REGULAR EXPRESSIONS REGULAR EXPRESSIONS ^abc abc at the beginning abc$ abc at the ending a|b a or b ab {2,4}c a followed by 2,3,4 bs followed by c ab{2,}c a followed by at least 2 bs followed by c REGULAR EXPRESSIONS REGULAR EXPRESSIONS ab*c a followed by zero or more bs followed by c ab+c a followed by one or more bs followed by c ab?c a followed by optional b followed by c (abc or ac) a.c an a followed by any single character (no newline) followed by c REGULAR EXPRESSIONS REGULAR EXPRESSIONS a\.c a.c exactly [abc] any one of a , b or c [Aa]bc Abc or abc [^abc] not containing a or b or c 8. References 8. References A reference is similar to a pointer in C. The reference itself is a scalar, it can be assigned to a scalar variable, printed, added to, passed to subroutine etc. To find out what the reference points to, we can dereference the reference. Creating a reference for a scalar: $str=This is a string; $strref=\$str; References References Dereferencing a scalar reference: $originalstr=$$strref; Creating a reference for an Array: @array=(1..10); $arrayref=\@array; Dereferencing an array reference: @originalarray=@$arrayref; To refer to individual elements: $originalarray[0] or $$arrayref[0]; References References Creating a reference for a hash: %hash=(a=>255,b=>355,c=>455); $hashref=\%hash; Dereferencing a hash reference: %originalhash=%$hashref; To refer to individual keys: $originalhash{a} or $$hashref{a}; References References Creating a reference for a subroutine: $subref=\&mysub; Dereferencing a subroutine: $result=&$subref(3,5); Creating anonymous subroutines: $subref=sub {.}; Dereferencing a subroutine: $result=&$subref(); References References Anonymous data: The term anonymous means without a name. Anonymous data refers to data (usually arrays, hashes and subroutines) that we can only access through a reference i.e. the data does not have an associated variable name. Using anonymous data, we can create nested data structures such as, 1.Arrays of Arrays 2. Hashes of Arrays 3. Hashes of Hashes References References Arrays of Arrays: @array= ( [0,1,2], [23,33,43], [53,54,55] ); Referring to individual elements: $array[0][1] or $array[0]->[1] References References Hashes of Arrays: %hash= ( b=>[0,1,2], g=> [23,33,43], r=>[53,54,55] ); Referring to individual elements: $hash{b}[1] or $array{b}->[1] References References Hashes of Hashes: %hash=(a=>{x=>0,y=>1,z=>2}, b=>{l=>3,m=>4,n=>5}, c=>{p=>53,q=>54,r=>55} ); Referring to individual elements: $hash{a}{x} or $array{a}->{x} 9. Debugging in Perl 9. Debugging in Perl Perl comes with a source level debugger. The debugger can help us track down subtle problems in our code. We shall see, 1. how to start and run the debugger 2. step through the execution of the script 3. trace the execution of the script 4. list the source in various ways Debugging in Perl Debugging in Perl 5. print out the values of the variables 6. set the breakpoints To invoke the debugger, $ perl d temp.pl After the display of some messages, we see main::(temp.pl:1): system (cls); DB<1> Debugging in Perl Debugging in Perl DB <1> debugger prompt with command number main package name temp.pl script name 1 line no. of the script system (cls) line in the script to be run Debugging in Perl Debugging in Perl Debugger Commands: n runs a line of the script, silently executes subroutines s runs a line of the script, steps into subroutines l list 10 lines after the current line - list the lines before the current line x prints the value of any scalar, list Debugging in Perl Debugging in Perl Debugger Commands: X print out all the variables in the current package V same as X except it takes an optional name of a package r stops stepping thru the subroutine, executes rest of it, returns to the calling place. c runs the script without stepping thru Debugging in Perl Debugging in Perl Debugger Commands: b to set a break point S prints all the available subroutines t turns tracing on or off h help |h pause help with breaks !3 refers to the command no. 3 H -3 lists last 3 commands Debugging in Perl Debugging in Perl Debugger Commands: T show the stack trace w shows a window around the current line L lists all break points d deletes a break point D deletes all set break points 10.PACKAGES & MODULES 10.PACKAGES & MODULES What is a Package? A package is a namespace It is a space that provides its own global scope for identifiers. It functions as a private programming space. How to create Packages? We can place the code for a Package in its own file, or in multiple files, or even create several packages in the same file. To switch into another package, we use the package statement. PACKAGES & MODULES PACKAGES & MODULES $ vi packages.pl package package1; BEGIN {} $x=1; sub subroutine1 { print one\n ;} return 1; END {} PACKAGES & MODULES PACKAGES & MODULES $ vi prg1.pl require packages.pl; package1::subroutine1(); print $package1::x; To run, $ perl prg1.pl To know in which package we are, print __PACKAGE__; PACKAGES & MODULES PACKAGES & MODULES Splitting a Package across files: $ vi file1.pl package package1; BEGIN {} sub sub1 { print hello\n; } return 1; END {} PACKAGES & MODULES PACKAGES & MODULES $ vi file2.pl package package1; BEGIN {} sub sub2 { print bye\n; } return 1; END {} PACKAGES & MODULES PACKAGES & MODULES $ vi original.pl require file1.pl; require file2.pl; package1::sub1(); package1::sub2(); The result is, hello bye PACKAGES & MODULES PACKAGES & MODULES Multiple Packages in a single file: $ vi packages.pl package package1; BEGIN{} sub sub1 { print hello\n; } return 1 END {} package package2; BEGIN{} sub sub2 { print bye\n; } return 1; END {} PACKAGES & MODULES PACKAGES & MODULES Our Declaration: Our declaration sets global scope across packages, i.e. variable of one package can be accessed in another package. $ vi packages.pl package package1; our $data=1; sub sub1 { print hello\n; } return 1 END {} PACKAGES & MODULES PACKAGES & MODULES package package2; sub sub2 { print $data\n; } return 1; END {} To run this, package1::sub1(); package2::sub2(); PACKAGES & MODULES PACKAGES & MODULES Creating Modules: A Perl Module is just a package in which the package is defined in a file with the same name as the package and has the extension pm. This allows subroutine names automatically exported to the code when we include a Module. PACKAGES & MODULES PACKAGES & MODULES $ vi Module1.pm package Module1; BEGIN { use Exporter(); @ISA=qw (Exporter); @EXPORT=qw (&subroutine1 &subroutine2); } sub subroutine1 { print hello\n ;} sub subroutine2 { print thanks\n; } return 1; END {} PACKAGES & MODULES PACKAGES & MODULES $ vi final.pl use Module1; subroutine1(); subroutine2(); $ perl final.pl; CPAN:There are literally hundreds of modules that have been developed for use with Perl. PACKAGES & MODULES PACKAGES & MODULES Many are available with the Perl Distribution itself. CPAN (Comprehensive Perl Archive Network) serves as a repository for user developed modules. An up-to-date survey of the contents of CPAN can be found at www.perl.com However, there are two drawbacks to the CPAN modules. The first is that we have to download, build and install modules before they can be used. Some modules may require PACKAGES & MODULES PACKAGES & MODULES compilation which means that we need a working C compiler. Secondly, most of the CPAN modules are developed for UNIX Perl. For installing Windows specific modules, there is a special tool called ppm. C:\> ppm PRAGMAS:These are the modules that PACKAGES & MODULES PACKAGES & MODULES provide instructions for Perl to behave at both compile time and run time. ex: subs: allows you to pre-declare subroutine names. vars: allows you to pre-declare global variables so that they can be accepted under strict pragma. PACKAGES & MODULES PACKAGES & MODULES strict: disallows bare words, global variables and symbolic references. integer: allows only integer arithmetic. constant: allows to create constant variables at compile time. PACKAGES & MODULES PACKAGES & MODULES Global variable: Any variable that is not explicitly declared with my or local, automatically becomes a global variable and is available at any point in that script. Any variable declared in a block with my or local becomes a local variable to that block. Difference between my and local: PACKAGES & MODULES PACKAGES & MODULES A my variable is only available to the code up until the nearest enclosing block or subroutine definition. If we call another subroutine within that one, the second subroutine will not have access to those variables. Local variables declared with local are available to the code inside that block and subroutine and to the nested subroutines called from the same subroutine. MANAGING FILES & DIRECTORIES MANAGING FILES & DIRECTORIES Renaming files: rename myfile myfile.bak; Creating and following links: link file1, file2; (for hard links) symlink file1, file2; ( for soft links) # follow the link (symbolic links) if ( -l $file) { $realfile = readlink $file; } MANAGING FILES & DIRECTORIES MANAGING FILES & DIRECTORIES Removing files & links: unlink temp, config, foo; Other file commands: chmod changing the file permissions chown changing the owner of the file fileno returns the file descriptor utime changes the time stamp MANAGING FILES & DIRECTORIES MANAGING FILES & DIRECTORIES Directory Commands: chdir: chdir images; use Cwd; $curr=cwd(); print $curr\n; File Listing (File Globbing): @files=<*.pl>; while (<*.pl>) { print $_ , \n; } MANAGING FILES & DIRECTORIES MANAGING FILES & DIRECTORIES Directory Commands: mkdir: mkdir temp, 0777; rmdir: rmdir temp; Perl and the Environment: Perl environment variables are stored in a special hash called %ENV. MANAGING FILES & DIRECTORIES MANAGING FILES & DIRECTORIES Environment Variables are commonly in upper case. To view all the Environment Variables, foreach $key (keys %ENV) { print $key -> $ENV {$key} \n; } MANAGING FILES & DIRECTORIES MANAGING FILES & DIRECTORIES To run OS commands, system (ls); $ls=`ls`; WORKING WITH FILES & I/O WORKING WITH FILES & I/O Creating a file: open (FH,>myfile) or die can not create\n; Reading a file: while (<FH>) { . } WORKING WITH FILES & I/O WORKING WITH FILES & I/O Writing to a file: print FH$input\n; Closing a file: close FH; open options: > writing < reading +> read and write WORKING WITH FILES & I/O WORKING WITH FILES & I/O File Tests: -d directory ? -e file exists ? -f plain file ? -l symbolic link ? -r readable ? -s ? Big is the file ? -w writable ? WORKING WITH FILES & I/O WORKING WITH FILES & I/O File Tests: -x executable ? -z empty file ? -B binary file ? -T text file ? Command line arguments: Command line arguments are stored in a special list @ARGV. WORKING WITH FILES & I/O WORKING WITH FILES & I/O foreach $arg (@ARGV) { print $arg \n; } #!/usr/local/bin/perl use DBI; Use the DBI Module #!/usr/local/bin/perl use DBI; $db_handle = DBI->connect(DBI:mysql:sanskar_club:sql.njit.edu, sanskar, pswd) || die (Couldnt connect\n); Syntax of DBI->connect() DBI->connect(data_source, username, password); data_source will be DBI:driver_name:driver_parameters For MYSQL at NJIT driver_name = mysql driver_parameters = database_username:sql.njit.edu username = ucid/club account name password = afs password Establish connection and get a handle to it #!/usr/local/bin/perl use DBI; $db_handle = DBI->connect(DBI:mysql:sanskar_club:sql.njit.edu, sanskar, pswd) || die (Couldnt connect\n); $stmt_handle = $db_handle->prepare("SELECT name, email, numpeople, accomdetails FROM user_detail"); Prepare an SQL query #!/usr/local/bin/perl use DBI; $db_handle = DBI->connect(DBI:mysql:sanskar_club:sql.njit.edu, sanskar, pswd) || die (Couldnt connect\n); $stmt_handle = $db_handle->prepare("SELECT name, email, numpeople, accomdetails FROM user_detail"); $stmt_handle ->execute; Execute the SQL query #!/usr/local/bin/perl use DBI; $db_handle = DBI->connect(DBI:mysql:sanskar_club:sql.njit.edu, sanskar, pswd) || die (Couldnt connect\n); $stmt_handle = $db_handle->prepare("SELECT name, email, numpeople, accomdetails FROM user_detail"); $stmt_handle ->execute; while (@row = $stmt_handle->fetchrow()) { foreach $field (@row){ print "$field "; } print \n; } $stmt_handle->finish(); Finish with statement handle #!/usr/local/bin/perl use DBI; $db_handle = DBI- >connect(DBI:mysql:sanskar_club:sql.njit.edu, sanskar, pswd) || die (Couldnt connect\n); $stmt_handle = $db_handle->prepare("SELECT name, email, numpeople, accomdetails FROM user_detail"); $stmt_handle ->execute; Close the connection while (@row = $stmt_handle->fetchrow()) { foreach $field (@row){ print "$field "; } print \n; } $stmt_handle->finish(); $db_handle->disconnect(); Contd..