1 / 21

Introduction to Perl

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.

clive
Télécharger la présentation

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to Perl Practical Extraction and Report Language or Pathologically Eclectic Rubbish Lister or …

  2. 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

  3. 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

  4. 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

  5. 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

  6. 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

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

  8. Example Development and Execute Double Click

  9. Command Line execution type to exec

  10. 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

  11. 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 . . .

  12. 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;

  13. 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

  14. 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.

  15. Example variable initializations • $num = 42; • $letter = 'a'; • $name = 'Paul'; • NOTE! Strings are *single values*! • $grade = 99.44; • $Big_String = 'The Quick Brown Fox…';

  16. Program using integer variables

  17. 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.

  18. 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.

  19. 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.

  20. 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>;

  21. Example Error(Missing ;) NOTE: command line execution

More Related