Introduction to Perl Software Tools. Slide 2 Introduction to Perl l Perl is a scripting language that makes manipulation of text, files, and processes.

Slides:



Advertisements
Similar presentations
A Guide to Unix Using Linux Fourth Edition
Advertisements

Introduction to C Programming
 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Perl I/O Learning Objectives: 1. To understand how to perform input from standard Input & how to process the input 2. To understand how to perform input.
Perl Process Management Software Tools. Slide 2 system Perl programs can execute shell commands (Bourne shell) using the system function. system("date");
Shell Programming Software Tools. Slide 2 Shells l A shell can be used in one of two ways: n A command interpreter, used interactively n A programming.
Perl Process Management Learning Objectives: 1. To learn the different Perl’s commands for invoking system process 2. To learn how to perform process management.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Scalar Variables Start the file with: #! /usr/bin/perl –w No spaces or newlines before the the #! “#!” is sometimes called a “shebang”. It is a signal.
Perl Functions Software Tools. Slide 2 Defining a Function l A user-defined function or subroutine is defined in Perl as follows: sub subname{ statement1;
Introduction to Perl Learning Objectives: 1. To introduce the features provided by Perl 2. To learn the basic Syntax & simple Input/Output control in Perl.
Perl I/O Software Tools. Lecture 15 / Slide 2 Input from STDIN Reading from STDIN is easy, and we have done it many times. $a = ; In a scalar context,
Introduction to Perl. How to run perl Perl is an interpreted language. This means you run it through an interpreter, not a compiler. Your program/script.
Shell Programming Learning Objectives: 1. To understand the some basic utilities of UNIX File 2. To compare UNIX shell and popular shell 3. To learn the.
Guide To UNIX Using Linux Third Edition
Introduction to scripting
Shell Scripting Awk (part1) Awk Programming Language standard unix language that is geared for text processing and creating formatted reports but it.
Computer Programming for Biologists Class 2 Oct 31 st, 2014 Karsten Hokamp
Introduction to C++ - How C++ Evolved Most popular languages currently: COBOL, Fortran, C, C++, Java (script) C was developed in 1970s at AT&T (Richie)
Introduction to Perl Practical Extraction and Report Language or Pathologically Eclectic Rubbish Lister or …
Message from Greg You recently covered fopen, fread and fwrite. There are also unbuffered versions, open,read,write. The quiz: will be last ½ hour of class.
Gator Engineering 1 Chapter 2 C Fundamentals Copyright © 2008 W. W. Norton & Company. All rights reserved.
– Introduction to the Shell 10/1/2015 Introduction to the Shell – Session Introduction to the Shell – Session 2 · Permissions · Users.
An Introduction to Unix Shell Scripting
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Input, Output, and Processing
Computer Science 101 Introduction to Programming.
Introduction to Bash Programming Ellen Zhang. Previous three classes What have we learnt so far ?
CS465 - UNIX The Bourne Shell.
1 System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007 References:  chapter 1, The Unix Programming Environment, Kernighan.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Shell Programming. The UNIX Shell  The UNIX shell listens to what you type and executes commands at your request. User command: lpr file UNIX Shell UNIX.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Introduction to Unix – CS 21
1 Introduction to Perl CIS*2450 Advanced Programming Techniques.
Introduction to Perl “Practical Extraction and Report Language” “Pathologically Eclectic Rubbish Lister”
Shell Programming Learning Objectives: 1. To understand the some basic utilities of UNIX File 2. To compare UNIX shell and popular shell 3. To learn the.
Perl Tutorial. Why PERL ??? Practical extraction and report language Similar to shell script but lot easier and more powerful Easy availablity All details.
Scripting Languages Course 3 Diana Trandab ă ț Master in Computational Linguistics - 1 st year
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
Introduction to Perl. What is Perl Perl is an interpreted language. This means you run it through an interpreter, not a compiler. Similar to shell script.
Basic Variables & Operators Web Programming1. Review: Perl Basics Syntax ► Comments: start with # (ignored by Perl) ► Statements: ends with ; (performed.
Python Let’s get started!.
Agenda The Bourne Shell – Part II Special Characters Ambiguous File Reference Variable Names and Values User Created Variables Read-only Variables (Positional.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
Dept. of Animal Breeding and Genetics Programming basics & introduction to PERL Mats Pettersson.
1 Types of Programming Language (1) Three types of programming languages 1.Machine languages Strings of numbers giving machine specific instructions Example:
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
Linux Administration Working with the BASH Shell.
CSE 303 Concepts and Tools for Software Development Richard C. Davis UW CSE – 10/9/2006 Lecture 6 – String Processing.
Organization of Programming Languages Meeting 37 April 18, 2016.
Scripting Languages Course 7 Diana Trandab ă ț Master in Computational Linguistics - 1 st year
Topics Designing a Program Input, Processing, and Output
CS 330 Class 7 Comments on Exam Programming plan for today:
Chapter 6 JavaScript: Introduction to Scripting
Introduction Python is an interpreted, object-oriented and high-level programming language, which is different from a compiled one like C/C++/Java. Its.
Variables, Expressions, and IO
Getting Started with C.
PHP Introduction.
Introduction to Python
Introduction to C++ Programming
T. Jumana Abu Shmais – AOU - Riyadh
Perl I/O Learning Objectives:
Topics Designing a Program Input, Processing, and Output
Shell Programming.
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Introduction to Perl Learning Objectives:
Perl Process Management
Presentation transcript:

Introduction to Perl Software Tools

Slide 2 Introduction to Perl l Perl is a scripting language that makes manipulation of text, files, and processes easy. l Perl is a cross between shell programming and the C programming language. C (numbers) Shell programming (text) Smalltalk (objects) C++ (numbers, objects) Perl (text, numbers) Java (objects)

Slide 3 Introduction to Perl l Perl provides a more concise and readable way to do many tasks compared to C++ and shell scripts. l Perl has replaced shell programming as the most popular programming language for text processing and Unix system administration. l Perl was originally designed under Unix, but now also runs under all operating systems (including Windows). l Perl is also a popular language for CGI and GUI programming.

Slide 4 Introduction to Perl The perl command on our system automatically invokes Perl 5.0. Some systems use Perl 4.0 as the default, but you also can run a perl 5.0 script explicitly with perl5: $ cat simple $ perl5 simple $ You can run the script directly if you make the script executable, and the first line is of the following form ( #!/usr/... must start from the first column): $ chmod +x simple $ cat simple #!/usr/local/bin/perl5 -w $ simple $

Slide 5 Basic Syntax The -w option tells Perl to produce extra warning messages about potential dangers. Use -w in all your Perl programs for now. #!/usr/local/bin/perl5 -w Whitespace doesn't matter in Perl (like C++), except for #!/usr/local/bin/perl5 -w which must start from column 1 on line 1.

Slide 6 Basic Syntax All perl statements end in a semicolon ; (like C++) In Perl, comments begin with # (like shell scripts) everything after the # to the end of the line is ignored. # need not be at the beginning of the line. n there are no C++-like multiline comments: /* */

Slide 7 Perl Example 1 l Here is a “hello world” Perl program: $ ls -l -rwxr-xr-x 1 horner cs 52 Mar 2 15:50 hello* $ cat hello #!/usr/local/bin/perl5 -w # comment lines start with the # character print "Hello world\n"; $ hello Hello world $ The print command sends the string to the screen, and “ \n “ adds a newline.

Slide 8 Perl Example 1 You can optionally use parenthesis around the argument in print : print ("Hello world\n"); or, if your prefer the C++ function style: print("Hello world\n");

Slide 9 Scalar Variables l A scalar variable can hold a single number or string (like shell variables), including integers and floating-point numbers (unlike shell variables). Scalar variables begin with “$” followed by a letter, and then possibly more letters, digits, or underscores. (e.g., $n, $n1, $name, $first_name ). l Scalar variables are case sensitive.

Slide 10 Assigning Scalar Variables Scalars are assigned using “ = “ $scalar = expression; l To assign a value to a scalar variable: $number = 25; $name = "Bill Gates"; Unlike shell scripts, use the “$” both when the variable is used and assigned: $ cat test1 #!/usr/local/bin/perl5 -w $number = 25; $name = "Bill Gates"; print "$number $name\n"; $ test1 25 Bill Gates $

Slide 11 Numerical Scalar Variables l Internally, all numerical scalar values are stored as floats (so you don’t have to worry about integer division in Perl like you do in C++). l Perl supports the usual C++ numerical operations: $a = 25;# $a is now 25 $a += 5; # $a is now 30 $a *= 3; # $a is now 90 $a++; # $a is now 91 --$a; # $a is now 90 $result = ($a + 2) * 3.4; # $result is 312.8

Slide 12 User Input Use to get input from the user: $ cat test2 #!/usr/local/bin/perl5 -w print "Enter name: "; $name = ; chomp ($name); print "How many girlfriends do you have? "; $number = ; chomp($number); print "$name has $number girlfriends!\n"; $ test2 Enter name: Bill Gates How many girlfriends do you have? more than you Bill Gates has more than you girlfriends!

Slide 13 User Input grabs one line of input, including the newline character. So, after: $name = ; if the user typed “Bill Gates[ENTER]”, $name will contain: “ Bill Gates\n ”. To delete the newline, the chomp() function takes a scalar variable, and removes the trailing newline if present. (If there is no newline at the end, it does nothing.) l A shortcut to do both operations in one line is: chomp($name = );

Slide 14 $ l As with the shell scripts, use a backslash before $ if you really want to print the dollar sign: $ cat test4 #!/usr/local/bin/perl5 -w print "Enter amount: "; $cost = ; print "The total is: \$$cost"; $ test4 Enter amount: The total is $18.50 No need to use chomp() if the newline on $cost can be used when it is printed.

Slide 15 Numerical Example $ cat test6 #!/usr/local/bin/perl5 -w print "Enter height of rectangle: "; $height = ; print "Enter width of rectangle: "; $width = ; $area = $height * $width; print "The area of the rectangle is $area\n"; $ test6 Enter height of rectangle: 10 Enter width of rectangle: 5 The area of the ractangle is 50 $ test6 Enter height of rectangle: 10.1 Enter width of rectangle: 5.1 The area of the rectangle is 51.51

Slide 16 Backquotes : Command Substitution l You can use command substitution in Perl like in shell scripts: $ whoami gates $ cat test7 #!/usr/local/bin/perl5 -w $user = `whoami`; chomp($user); $num = `who | wc -l`; chomp($num); print "Hi $user! There are $num users logged on.\n"; $ test7 Hi gates! There are 6 users logged on. Command substitution will usually include a newline, so use chomp().