Introduction to perl
1 / 21

Introduction to Perl - PowerPoint PPT Presentation

  • Uploaded on

Introduction to Perl. Practical Extraction and Report Language or Pathologically Eclectic Rubbish Lister or …. Perl? perl? PERL?. The name of the language is "Perl". Any expansion you may read was made up after the fact. "PERL" is *never* correct.

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
Download Presentation

PowerPoint Slideshow about 'Introduction to Perl' - clive

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
Introduction to perl

Introduction to Perl

Practical Extraction and Report Language


Pathologically Eclectic Rubbish Lister


Perl perl perl
Perl? perl? PERL?

  • The name of the language is "Perl".

  • Any expansion you may read was made up after the fact. "PERL" is *never* correct.

  • The executable program that interprets Perl code is "perl"

  • "Only perl can parse Perl"

  • See also: perldoc –q difference

Basic structure
Basic Structure

  • Bares resemblances (but not much) to C or Java

  • semi-colons separate executable statements

  • { } delimit blocks, loops, subroutines

  • Comments begin with # and extend to end of line

  • No main() function – code executed top-down.

  • Function arguments are comma-separated

    • parentheses are (usually) optional

What is perl
What is Perl?

  • Perl is a FREE, open source programming language created by Larry Wall

  • General philosophy: practical and quick

  • Non philosophy: structured or elegant

  • Know for Text processing of strings and files

  • Uses regular expressions heavily

  • Attractive for writing short and quick solutions to many text processing tasks

Running a perl program
Running a Perl Program

  • A Perl program is just a text file (ie pure ASCII)

  • We write these files using text editors such as notepad, notepad++ etc.

  • The extension for a Perl program is .pl so a file such as should be written to contain a Perl program.

  • Within DOS ( or Unix command line ) we can execute using the following command

  • C:\perl

  • perl is the Perl interpreter that runs

A very simple program using print
A very simple program using Print

# Type and run thisprint "Hello", " ", "World", "\n";print ("Five plus three: ", 5+3);

# This is a comment.

# The following command reads what you

# type and does nothing with the input

# WHY? Run it with and without the command!


NEXT:store the above as

Easy way to run the programs
Easy way to run the programs

  • Open two windows in XP

    • The first is for the editor

    • The second is the directory window.

  • Leaving these two windows open simplifies the programming considerably

  • Step 1: Write the program in Notepad

  • Save the program as say

  • Click on the directory window that contains and then double click on

  • This will execute the program automatically since .pl files are associated with the interpreter perl.

Another simple perl program
Another Simple Perl program

#!/usr/bin/perl –w # only for Unix programs

#This is your first Perl program

# It prints data to the screen and reads information from the keyboard

print "Hello mister user\n"; # Print introduction

print 'What is your name:';

$line = <STDIN>; # Grabs everything up to and including the \n

chomp($line);# removes the \n

print 'Well ',$line, " welcome to Perl \n\n\n";

print 'HIT return to exit!';


#The above line keeps the window open so you can look at the output

First complete program
First Complete Program

  • print "Hello World\n";

  • A few ways to execute this code:

    • "interactive mode"

      • start the perl interpreter

      • type the code

      • send end-of-file (CTRL-D in Unix)

    • Put code in a file, give perl interpreter the filename

      • perl

    • Tell the OS what program should execute the contained code

      • shebang + chmod . . .

Before we develop more complicated progs
Before we develop more complicated progs…

  • Perl is a very lenient language. It will allow you to do a whole host of things that you probably shouldn't be able to.

    • printing a variable that you never assigned to

    • trying to add 5 + 'foo'

  • If you want Perl to warn you about this sort of thing (and you often do) add the following command to the start of your program

    use warnings;


  • Three major types of Perl variables

    • Scalars – single values

    • Arrays – contain lists of values

    • Hashes – "associative arrays"

  • There are others (filehandles, typeglobs, subroutines)… We will cover some of these later


  • A Scalar variable contains a single value.

  • Scalar variable types include

    • int ( examples, 21, 231 , -4221)

    • Float( examples 21.345, 43.101)

    • Strings(examples “Richard”, ‘Reality what a concept!’)

  • Scalar variable name starts with a $

  • Next character is a letter or underscore

  • Remaining characters: letters, numbers, or underscores.

Example variable initializations
Example variable initializations

  • $num = 42;

  • $letter = 'a';

  • $name = 'Paul';

    • NOTE! Strings are *single values*!

  • $grade = 99.44;

  • $Big_String = 'The Quick Brown Fox…';


  • String constants are enclosed within

    • Double quotes( “Hello there”, “You’ve got to be kidding\n”)

    • Single quotes( ‘Now is the time”, ‘for all good men’)

  • The double quotes are used when you want control directives , \n,\t etc, to be processed.

    • \n represents a new line. ie carriage return!

    • \t is a tab operation. There are others

  • Question:

    • How do you print the following

      • Now you’ve done it.

Introduction to perl

When Perl sees an expression that doesn’t make sense, such as a variable that has not been given a value, it tends to just silently pass over the problem and use some default value such as undef.

Perl may make the wrong assumption! So make sure it does what you want it to!

Perl is known to compile all kinds of gobbly gook.

Error comments
Error Comments

  • If the program you type in has a syntax error then a comment will be printed and the program will exit.

  • If you are using notepad++ and click on the .pl file to execute it you will have a problem if there is an error.

  • The output will flash and disappear .

    Example: Take the previous program and remove the first ;

    Then resave it and double click on the .pl file.


    Well , What can we do to see its comments.

Program that needs input
Program that needs input

#Type this in and run it using the dos command line

# Make several errors and see what happens.

print “Enter the value of x: “;

$x=<STDIN>;# Reads line up to and including the \n

chomp($x);# Removes the \n

print “Enter the value of y: “;




# Note the double quotes in the following command!

print “The sum of $x and $y is “, $z,”\n”;

print ‘HIT return to exit!’;


Example error missing
Example Error(Missing ;)

NOTE: command

line execution