Introduction to Perl Practical Extraction and Report Language

  • Slides: 21
Download presentation
Introduction to Perl Practical Extraction and Report Language or Pathologically Eclectic Rubbish Lister or

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

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 • • • Bares resemblances (but not much) to C or Java

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? • Perl is a FREE, open source programming language created by

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 • A Perl program is just a text file (ie

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 # Type and run this print "Hello", "World",

A very simple program using Print # Type and run this print "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 simple 1. pl

Easy way to run the programs • Open two windows in XP • The

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 Double Click

Example Development and Execute Double Click

Command Line execution type to exec

Command Line execution type to exec

Another Simple Perl program #!/usr/bin/perl –w # only for Unix programs #This is your

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 usern"; # 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 nnn"; print 'HIT return to exit!'; $dummy=<STDIN> #The above line keeps the window open so you can look at the output

First Complete Program • print "Hello Worldn"; • A few ways to execute this

First Complete Program • print "Hello Worldn"; • 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… • Perl is a very lenient language. It

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 • Three major types of Perl variables • Scalars – single values •

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 • A Scalar variable contains a single value. • Scalar variable types include

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 • $num = 42; • $letter = 'a'; • $name =

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 • String constants are enclosed within • Double quotes( “Hello there”, “You’ve got

Strings • String constants are enclosed within • Double quotes( “Hello there”, “You’ve got to be kiddingn”) • 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.

Perl When Perl sees an expression that doesn’t make sense, such as a variable

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 • If the program you type in has a syntax error then

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 #Type this in and run it using the dos command

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 ; ) NOTE: command line execution

Example Error(Missing ; ) NOTE: command line execution