PERL Ronald L. Ramos Proglan. What is PERL? Perl stands for Practical Extraction and Reporting Language. (or Pathologically Eclectic Rubbish Lister).

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Introduction to C Programming
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
14/11/11.  These words have special meanings in themselves  These should NOT be used as Identifiers.  For example:  Break, do, function, case, else,
CS311 – Today's class Perl – Practical Extraction Report Language. Assignment 2 discussion Lecture 071CS Operating Systems I.
Loops – While, Do, For Repetition Statements Introduction to Arrays
Perl Basics A Perl Tutorial NLP Course What is Perl?  Practical Extraction and Report Language  Interpreted Language Optimized for String Manipulation.
Guide To UNIX Using Linux Third Edition
JavaScript, Third Edition
Introduction to C Programming
Programming Concepts MIT - AITI. Variables l A variable is a name associated with a piece of data l Variables allow you to store and manipulate data in.
20-753: Fundamentals of Web Programming Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 8: Perl Basics Fundamentals of Web Programming.
PERL Variables and data structures Andrew Emerson, High Performance Systems, CINECA.
Introduction to Python
General Computer Science for Engineers CISC 106 Lecture 02 Dr. John Cavazos Computer and Information Sciences 09/03/2010.
1 An Introduction to Perl Part 1 CSC8304 – Computing Environments for Bioinformatics - Lecture 7.
Perl Tutorial Presented by Pradeepsunder. Why PERL ???  Practical extraction and report language  Similar to shell script but lot easier and more powerful.
Nael Alian Introduction to PHP
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
2440: 211 Interactive Web Programming Expressions & Operators.
Print 'Hello world.'; Tren Griffith. Outline:  Perl introduction  Scalar Data  Variables  Operators  Control Structures  Input  Lists and Arrays.
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
20-753: Fundamentals of Web Programming 1 Lecture 12: Javascript I Fundamentals of Web Programming Lecture 12: Introduction to Javascript.
1 System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007 References:  chapter 1, The Unix Programming Environment, Kernighan.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Class 2Intro to Databases Goals of this class Include & Require in PHP Generating Random Numbers in PHP Arrays – Numerically Indexed and Associative Program.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
CPS120: Introduction to Computer Science Decision Making in Programs.
What is PHP? PHP stands for PHP: Hypertext Preprocessor PHP is a server-side scripting language, like ASP PHP scripts are executed on the server PHP supports.
Chapter 9: Perl Programming Practical Extraction and Report Language Some materials are taken from Sams Teach Yourself Perl 5 in 21 Days, Second Edition.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Prof. Alfred J Bird, Ph.D., NBCT Office – McCormick 3rd floor 607 Office Hours – Tuesday and.
Perl Tutorial. Why PERL ??? Practical extraction and report language Similar to shell script but lot easier and more powerful Easy availablity All details.
Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals.
IT ELECTIVE 2.  Web server Can refer to either the hardware (the computer) or the software (the computer application) that helps to deliver content that.
Scripting Languages Diana Trandab ă ț Master in Computational Linguistics - 1 st year
More Perl Data Types Scalar: it may be a number, a character string, or a reference to another data type. -the sigil $ is used to denote a scalar(or reference)
Department of Electrical and Computer Engineering Introduction to Perl By Hector M Lugo-Cordero August 26, 2008.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
 History  Ease of use  Portability  Standard  Security & Privacy  User support  Application &Popularity Today  Ten Most Popular Programming Languages.
Variables  A piece of memory set aside to store data  When declared, the memory is given a name  by using the name, we can access the data that sits.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
Chapter 6: Looping. Objectives Learn about the loop structure Create while loops Use shortcut arithmetic operators Create for loops Create do…while loops.
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
Dr. Abdullah Almutairi Spring PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages. PHP is a widely-used,
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Unit – 3 Control structures. Condition Statements 1.If.…..else :- Has someone ever told you, "if you work hard, then you will succeed"? And what happens.
PHP Tutorial. What is PHP PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
PYTHON PROGRAMMING. WHAT IS PYTHON?  Python is a high-level language.  Interpreted  Object oriented (use of classes and objects)  Standard library.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
PHP Condtions and Loops Prepared by Dr. Maher Abuhamdeh.
PHP using MySQL Database for Web Development (part II)
© 2016 Pearson Education, Ltd. All rights reserved.
Variables A piece of memory set aside to store data
Variables, Expressions, and IO
JavaScript: Functions.
PHP Introduction.
JavaScript: Control Statements.
While loops The while loop executes the statement over and over as long as the boolean expression is true. The expression is evaluated first, so the statement.
Arrays, For loop While loop Do while loop
PHP.
T. Jumana Abu Shmais – AOU - Riyadh
HYPERTEXT PREPROCESSOR BY : UMA KAKKAR
Chapter 2 Programming Basics.
There many situations comes in real life when we need to make some decisions and based on these decisions, we decide what should we do next. Similar situations.
PROGRAM FLOWCHART Iteration Statements.
PHP an introduction.
Presentation transcript:

PERL Ronald L. Ramos Proglan

What is PERL? Perl stands for Practical Extraction and Reporting Language. (or Pathologically Eclectic Rubbish Lister). Perl is a general-purpose, high level, interpreted and dynamic computer programming language with a vast number of uses. PERL is a language for doing what you want to do

PERL Strengths Perl runs on all platforms and is far more portable than C. Perl and a huge collection of Perl Modules are free software (either GNU General Public License or Artistic License). Perl is very much efficient in TEXT and STRING manipulation i.e. REG_EXP. It is a language that combines the best features from many other languages and is very easy to learn if you approach it properly. Dynamic memory allocation is very easy in PERL, at any point of time we can increase or decrease the size of the array.

PERL Weaknesses 1) You cannot easily create a binary image ("exe") from a Perl file. It's not a serious problem on Unix, but it might be a problem on Windows. 2) Moreover, if you write a script which uses modules from CPAN, and want to run it on another computer, you need to install all the modules on that other computer, which can be a drag. 3) Perl is an interpretative language, so its comparatively slower to other compiling language like C. So, it s not feasible to use in Real time environment like in flight simulation system.

Hello World print "Hello World!\n";

VARIABLES

Scalar Variables Scalar data is the one of the most basic and simplest data in Perl. Scalar data can be number or string. In Perl, string and number can be used nearly interchangeably. Scalar variables are used to hold scalar data. Scalar variables start with dollar sign ($) followed by Perl identifier. Perl identifiers can contain alphanumeric characters and underscores. You cannot start with a digit.

Numbers #floating-point values $x = 3.14; $y = -2.78; #integer values $a = 1000; $b = -2000; Perl also accepts string literal as a number for example: $s = "2000"; # similar to $s = 2000;

String $str = "this is a string in Perl“; $str2 = 'this is also as string too‘;

Operations on Scalars $x = 5 + 9; # Add 5 and 9, and then store the result in $x $x = ;# Subtract 4 from 30 # and then store the result in $x $x = 3 * 7; # Multiply 3 and 7 and then store the result in $x $x = 6 / 2; # Divide 6 by 2 $x = 2 ** 8;# two to the power of 8 $x = 3 % 2; # Remainder of 3 divided by 2 $y = ++$x; # Increase $x by 1 and store $x in $y $y = $x++; # Store $x in $y then increase $x by 1 $y = --$x; # Decrease $x by 1 and then store $x in $y $y = $x--; # Store $x in $y then decrease $x by 1 $x = $y; # Assign $y to $x $x += $y; # Add $y to $x $x -= $y; # Subtract $y from $x $x.= $y; # Append $y onto $x

String Operations $x = 3; $c = "he "; $s = $c x $x; $b = "bye"; # $c repeated $x times print $s. "\n"; #print s and start a new line # similar to print "$s\n"; $a = $s. $b; # Concatenate $s and $b print $a;

Scalar Interpolation $x = 10; $s = "you get $x"; print $s;

CONTROL STRUCTURES

Code blocks A block is surrounded by a pair of curly braces and can be nested within a block: { #statements here { # nested block # statements here }

If If control structure is used to execute a block of code based on a condition. The syntax of If control structure is as follows: if(condition){ statements; } If the condition is true the statements inside the block will be executed, for example: $x = 10; $y = 10; if($x == $y){ print "$x is equal to $y"; }

If Else If you need an alternative choice, Perl provides if-else control structure as follows: if(condition){ if-statements; } else{ else-statements; }

If Else If the condition is false the else-statements will be executed. Here is the code example: $x = 5; $y = 10; if($x == $y){ print "x is equal to y"; } else{ print "x is not equal to y"; }

If Else If Perl also provides if-else-if control structure to make multiple choices based on conditions. if(condition1){ } else if(condition2){ } else if(condition3){ }... else{ }

If Else If Here is the source code example: $x = 5; $y = 10; if($x > $y){ print "x is greater than y"; } else if ($x < $y){ print "x is less than y"; } else{ print "x is equal to y"; }

For In order to run a block of code iteratively, you use Perl for statement. The Perl for statement is used to execute a piece of code over and over again. The Perl for statement is useful for running a piece of code in a specific number of times. The following illustrates Perl for statement syntax: for(initialization; test; increment){ statements; }

For Here is a code snippet to print a message 10 times. for($counter = 1; $counter <= 10; $counter++){ print "for loop #$counter\n"; } Perl allows you to omit the initialization and increment part of for statement. You can initialize outside the loop and increase control variable inside the block as follows: $counter = 1 for(; $counter < 10;){ print "for loop #$counter\n"; $counter++; }

While Perl While statement is a Perl control structure that allows to execute a block of code repeatedly. The Perl while statement is used when you want to check a boolean condition before making a loop. The following illustrates the Perl while statement syntax: while(condition){ #statements; }

While $x = 0; while($x < 5){ print "$x\n"; $x++; }

Do While Beside while loop, Perl also provides do while loop statement. do while loop statement is similar to while loop but it checks the loop condition after executing the block therefore block executes at least one time. Here is a code snippet to demonstrate do while statement: $x = 0; do{ print "$x\n"; $x++; }while($x < 5);

ARRAYS

Lists Scalar variable allows you to store single element such as number or string. When you want to manage a collection or a list of scalar data you use list. Here are some examples of lists. ("Perl","array","tutorial"); (5,7,9,10); (5,7,9,"Perl","list"); (1..20); ();

Array Variables To store lists of values to use throughout your program you need array variables. Array variable is used to hold a list of data. Different from scalar variables, array variables are prefixed with the at For example we have five array variables to store five lists above as = = = = = ();

Operations on Arrays Here is the code snippet to demonstrate each function =(1,3,5,2); #add 10 print $last = #remove 10 print #add 0 print $start = # add 0 print

Operations on Arrays You can add or remove elements to/from an array. Here is a list of common operations on arrays: add $element to the end of remove the last element of and returns it. add $element to the start of remove the first element from and returns it.

HASH

What is hash? When number of item in an array is bigger, the performance is degraded also. Perl createdhash - before that is it called associative array - to overcome this limitation. Hash is another kind of collective data type. Hash can hold as many as scalar like array but the way you accessing hash element is different. You access hash element in has by using a key.Each has element has two parts: key and value or key-value pair.

Hash The key identifies the value of element associated with it. To define a hash variable, you use percentage sign (%) as follows: %websites = ( "perltutorial.org" => "Perl tutorial", "Perl.org" => "Perl directory" );

Accesing Hash Elements To access a hash element, you use the key of that element to retrieve the value as follows: print $websites{"perltutorial.org"}; #Pe rl tutorial

Filling Hash Elements You can define hash as above example or use a list as follows: %websites = ( "perltutorial.org" => "Perl tutorial", "Perl.org" => "Perl directory" ); Another way to fill the hash element by using key-value pair and assignment operator as follows: $websites{"Perl.com"} = "Perl.com homepage"; print $websites{"Perl.com"}; #Perl.com homepage

Testing a Key To test whether a key exists in a hash, you use exist function provided by Perl. Here is the syntax: if ( exists $hash{key} ){ #retrieve value here } For example, we can test whether key "perltutorial.org" exists in %websites hash by using the above pattern: %websites = ( "Perltutorial.org","Perl tutorial", "Perl.org", "Perl directory" ); if(exists $websites{"Perltutorial.org"}){ #Perl tutorial print $websites{"Perltutorial.org"}; }

Remove a Key from Hash To remove a single key from hash, you use delete function as follows: delete $hash{key}; To remove all keys and values from a hash, you just assign hash to an empty list: %hash = ();

Looping through a hash To loop through a hash you use foreach statement. First you retrieve all keys of the hash by using keys function. Based on this list you can find all elements of the hash. Here is the syntax: foreach $element (keys %hash){ #process hash element here }

Looping through a hash For example, we can loop through %websites hash: %websites = ( "Perltutorial.org","Perl tutorial", "Perl.org", "Perl directory" ); foreach $website (keys %websites){ print "$website is about $websites{$website}\n"; } #output: #Perltutorial.org is about Perl tutorial #Perl.org is about Perl directory

Array and Hash You can assign an hash to an array and vice versa. For example: %websites = ( "Perltutorial.org" =>"Perl tutorial", "Perl.org" => "Perl directory" = %websites;

SUBROUTINES

Define a Subroutine To define subroutine you use the keyword sub followed by subroutine name and its body. Here is the syntax of subroutine definition. sub subroutine_name{ #subroutine body } For example, here is a simple subroutine to print a message. sub print_message{ print "Perl subroutine tutorial"; }

Call a Subroutine To invoke or call subroutine, you use the subroutine name with the ampersand (&) prefixed as follows: &print_message;

Parameters Parameters are passed to a subroutine as a list which is available in a subroutine as - an array variable. For example:array sub print_list{ print } #print: this is Perl subroutine tutorial &print_list("this","is","Perl","subroutine","tut orial"); #print: subroutine tutorial 8&print_list("subroutine","tutorial");

Parameters In the line 2, we printed the list variable which was the input parameters of the subroutine. In line 5 and 7, we called subroutine with different list and we got different output. You can refer to each parameter in a list by using scalar $_[index] for example: sub max{ if($_[0] > $_[1]){ $_[0]; } else{ $_[1]; } $m = max(10,20); $m2 = max(50,30); print "$m\n"; print "$m2\n";

Returning Values Subroutine returns values which is the last expression evaluated. For example : sub cal_sum{ print "sum of two numbers\n"; $_[0] + $_[1]; } $result = &cal_sum(10,20); # result is 30 print $result;

Returning Values If you want to return value immediately from subroutine, you can use returnoperator, for example: sub min{ if($_[0] < $_[1]){ return $_[0]; } else{ return $_[1]; } $m = &min(10,20); # result is 10 print $m;

Returning a List of Values Subroutine returns not only scalar value but also a list of values. Let's take a look at an example below: sub get_list{ return 5..10; = &get_list; print #

FIN