Introduction to perl
This presentation is the property of its rightful owner.
Sponsored Links
1 / 21

Introduction to Perl PowerPoint PPT Presentation


  • 131 Views
  • Uploaded on
  • Presentation posted in: General

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.

Download Presentation

Introduction to Perl

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

or

Pathologically Eclectic Rubbish Lister

or


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 myprog.pl should be written to contain a Perl program.

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

  • C:\perl myprog.pl

  • perl is the Perl interpreter that runs myprog.pl


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!

$dummy=<STDIN>

NEXT:store the above as simple1.pl


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 myprog.pl

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

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


Example development and execute

Example Development and Execute

Double Click


Command line execution

Command Line execution

type to exec


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!';

$dummy=<STDIN>

#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 hello.pl

    • 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;


Variables

Variables

  • 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


Scalars

Scalars

  • 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…';


Program using integer variables

Program using integer variables


Strings

Strings

  • 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

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.

    WHAT DO YOU SEE???

    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: “;

$y=<STDIN>;

chomp($y);

$z=$x+$y;

# Note the double quotes in the following command!

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

print ‘HIT return to exit!’;

$dummy=<STDIN>;


Example error missing

Example Error(Missing ;)

NOTE: command

line execution


  • Login