1 / 12

Introduction to Perl

Introduction to Perl. Nicole Vecere. Background. General Purpose Language Procedural, Functional , and Object-oriented Developed for text manipulation Currently used for web development, administrative systems, etc. Recommended program setup: #!/ usr /bin/ perl use strict;

roy
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. Introductionto Perl Nicole Vecere

  2. Background • General Purpose Language • Procedural, Functional, and Object-oriented • Developed for text manipulation • Currently used for web development, administrative systems, etc. • Recommended program setup: #!/usr/bin/perl use strict; use warnings;

  3. Scalar Data • Scalar (or basic) data types hold a single data item • This data item may be a number, a string, or a reference • A scalar may not hold multiple values, but it may contain a reference to an array, list, hash, etc. • Initially, the value undef is assigned to all variables • This acts as 0 when applied to a number • Or as an empty string (“”) when applied to a string

  4. Variables and Operators • Variables are defined using $ $number = 4; $string = “Hello, world”; • Assignment operator: = • Arithmetic Operators: +, -, *, / • String Operators: . –string concatenation (a dot) • x –string repetition • Numeric Comparison Operators: ==, !=, <, >, <=, >= • String Comparison Operators: eq, ne, lt, gt, le, ge

  5. Special Features • Interpolation of scalars into strings • Any scalar variable inside a double-quoted string is replaced by its value $name = “Nicole”; print “My name is $name”; # Prints My name is Nicole • Automatic conversion between numbers and strings $cat = 2; $dog = 4; print $cat * $dog; # Prints 8 print $cat x $dog; # Prints 2222

  6. If Else Control Structure • Similar to C++, but curly braces are required. • Example: if (…) { … } else { … }

  7. While Control Structure • Similar to C++, but curly braces are required. • Example: while (…) { … }

  8. Standard Input • Perl allows for user input from the standard input device • This is generally the user’s keyboard • <STDIN> can be used to read a full line from standard input $line = <STDIN>; # Reads line and stores it in a variable. while(defined($line=<STDIN>)){ print “This line was: $line \n”; } # Prints lines of input right after inputting.

  9. Standard Input (Continued) while(<STDIN>) { print “This line was: $_ \n”; } # Each line of input is automatically stored inside # Perl’s default variable $_. while(<>) { print “This line was: $_ \n”; } # The diamond operator has the advantage of allowing command # line arguments.

  10. Lists and Arrays • List Representations • (1,2,3,4,5,6,7,8,9,10) # A list consisting of numbers 1,2,…,10. • (1..10) # Same list as above, created by the range operator. • ($a ..$b) # Range determined by current values of $a and $b. • List Assignment • ($cat, $dog) = ($dog, $cat); # Values are swapped. • @numbers = (1..10); # List (1..10) is stored inside an array.

  11. Accessing List Elements • Examples: • @values = (1..100); • print $values[0]; # Prints 1 • print $values[35]; # Prints 36 • print $values[100]; • # Nothing is printed. Value of $values[100] is undef. • print $values[$#values]; • # $#values is the largest index. • # Value 99 is printed since it’s the last list element.

  12. Reading into a List • Example: while(<>){ $lines[$index] = $_; $index = $index + 1; } print $#lines+1; print @lines; • # Lines of input become elements of a list stored inside • # array @lines. After reading we print out the number of • # input lines, then the whole list.

More Related