SlideServe
Menu
Préférences
© SlideServe · v2026

Introduction to Perl

roy avatar
12 diapositives · 290 Views · Sep 12, 2014

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;

Introduction to Perl - slide 1
01 / 12Introduction to Perl
1 / 17

Télécharger la présentation

Introduction to Perl
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  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.