perl - PowerPoint PPT Presentation

1 / 30
About This Presentation
Title:

perl

Description:

perl – PowerPoint PPT presentation

Number of Views:35
Avg rating:3.0/5.0
Slides: 31
Provided by: rml6
Category:
Tags: perl | var

less

Transcript and Presenter's Notes

Title: perl


1
perl
  • Comprehensive Perl Archive Network
    http//www.cpan.org/
  • http//www.perl.org/
  • http//www.perlmonks.com/

2
perl (http//www.perlmonks.com)
  • Perl is a high-level programming language with an
    eclectic heritage written by Larry Wall and a
    cast of thousands. It derives from the ubiquitous
    C programming language and to a lesser extent
    from sed, awk, the Unix shell, and at least a
    dozen other tools and languages.

3
perl (http//www.perlmonks.com)
  • Perl's process, file, and text manipulation
    facilities make it particularly well-suited for
    tasks involving quick prototyping, system
    utilities, software tools, system management
    tasks, database access, graphical programming,
    networking, and world wide web programming.

4
perl (http//www.perlmonks.com)
  • Perl strengths make is especially popular with
    system administrators and CGI script authors, but
    mathematicians, geneticists, journalists, and
    even managers also use Perl.

5
cat p0.pl print "hello \n" perl
p0.pl hello cat p1.pl !/usr/bin/perl print
"hello \n" chmod x p1.pl p1.pl hello
6
Scalars
  • Basic type of data in Perl they can represent a
    number(int, float) or a string of text. Here are
    some examples of scalar assignments.

7
cat p2.pl !/usr/bin/perl a 42
an integer pi 3.14159265 a real
number x 6.02e23 scientific
notation pet "dog" string words
"I love my pet" string with
interpretation cost 'It costs 100'
string without interpretation var a
another variable's value cmd pwd
output of command print "a a \t pi pi \t
x x \n" print "pet pet \t words
words \n" print "cost cost \t var var
\n" print "cmd cmd \n"
8
Output
p2.pl a 42 pi 3.14159265 x
6.02e23 pet dog words I love my dog cost
It costs 100 var 42 cmd
/usr/people/lacambra/perl
9
Arrays
  • Arrays are basically a collection of scalars
    stored next to each other and accessed by indices
    from zero to the number of elements minus one.
    Here are examples of some arrays in action.
  • Note when we are referring to an entire array we
    use the _at_ at the beginning of its name. If we're
    referring to only one of its elements(which is a
    scalar) we use a .

10
!/usr/bin/perl _at_a(1,2,3) _at_simpsonsfamily("home
r","marge","lisa","maggie","bart") Arrays
can store either strings or numbers or both.
Now lets see how we can get at an individual
element of an array. print " a0 \n" This
returns the first element in _at_a which is 1 print
" simpsonsfamily4 \n" This returns the fifth
element a34 This sets the 4th element in _at_a
to 4. print "a3 \n"
p3.pl 1 bart 4
Output
11
cat p4.pl !/usr/bin/perl _at_a(1,2,3) _at_breverse
_at_a print "a0 a1 a2 \n" print "b0
b1 b2 \n" print "_at_b \n" p4.pl 1 2 3 3 2
1 3 2 1
The reverse function simply takes a list or
array and reverses it.
12
Arithmetic operations
Example Type Result ab Addition Sum of a
and b a-b Substraction Result of b
subtracted from a ab
Multiplication Product of a and
b a/b Division Result of a divided by
b ab Modulus Remainder when a is divided by
b ab Exponentiation a to the power of
b
13
cat p5.pl !/usr/bin/perl a4 b5 print
ab, "\n" arithmetic operator print a.b,
"\n" string operator print ab, "\n"
arithmetic operator print a x b, "\n"
string operators print '-' x 80
print row of dashes p5.pl 9 45 20 44444
-------------------------------------------------
--------
More operators
14
List assignments
cat p6.pl !/usr/bin/perl _at_pets("dog","cat","t
arantula") (a, b, c)_at_pets print a,
"\n" print b, "\n" print c, "\n"
p6.pl dog cat tarantula
15
List assignments
cat p7.pl !/usr/bin/perl _at_array("dog",1,5.6789
4) (a, b, c)_at_array print a, "\n" print
b, "\n" print c, "\n"
p7.pl dog 1 5.67894
16
Control statements and looping
  • Perl offers an abundance of looping and control
    statements. Some quick notes if you're a C, C,
    or Java programmer Perl requires curly braces
    around blocks of code even if your for loop has
    only one line of code

17
  • your basic if
  • if(value1)
  • print "value is equal to 1\n"
  • your basic if/else
  • if(value1)
  • print "value is equal to 1\n"
  • else
  • print "value is not equal to 1\n"

18
your basic if/elsif/else if(value1)
print "value is equal to 1\n"
elsif(value2) print "value is equal to
2\n" elsif(value3) print "value is
equal to 3\n" else print "value is not
equal to 1,2, or 3\n"
19
cat p8.pl !/usr/bin/perl num25 while(numlt30
) print "num is num\n"
numnum1 p8.pl num is 25 num is 26 num
is 27 num is 28 num is 29
20
cat p9.pl !/usr/bin/perl year1900
until(year2000) print "Mission-critical
electronics working in the year year\n"
it works year print "Not working
anymore\n p9.pl Mission-critical electronics
working in the year 1900 Mission-critical
electronics working in the year 1901
.... Mission-critical electronics working in the
year 1998 Mission-critical electronics working in
the year 1999 Not working anymore
21
cat p10.pl !/usr/bin/perl for(i1 ilt100
i) print "i\n"
This for loop will print the numbers 1 through
100 on separate lines
22
cat p11.pl !/usr/bin/perl _at_numbers(1,7,3,8,9)
foreach(_at_numbers) print "_,"
p11.pl 1,7,3,8,9,
23
Files
cat p12.pl !/usr/bin/perl FILE"catalog" open
(FILE) _at_array ltFILEgt close (FILE) foreach
line (_at_array) print "line"
p12.pl PC IBM 400.00 PC HP
300.00 Macintosh900.00 Macintosh700.00
24
cat p13.pl !/usr/bin/perl open(OUTPUT,
"gtgt/tmp/file") a "some text..." print OUTPUT
a close(OUTPUT) cat /tmp/file some
text... p13.pl cat /tmp/file some
text...some text...
Files
25
Perl file access revisited
  • The most basic way to legally open a file in Perl
    was covered
  • We will see way to open a file using a file
    handle
  • Ways to verify permission/characteristics of a
    specific file

26
Simplest
  • !/usr/bin/perl
  • FILE"catalog"
  • open (FILE)
  • _at_array ltFILEgt
  • close (FILE)
  • foreach line (_at_array)
  • print "line"
  • p12.pl
  • PC IBM 400.00
  • PC HP 300.00
  • Macintosh900.00
  • Macintosh700.00

27
using a file handle
  • !/usr/bin/perl
  • open (FILE,"catalog")
  • _at_array ltFILEgt
  • close (FILE)
  • foreach line (_at_array)
  • print "line"
  • p12bis.pl
  • PC IBM 400.00
  • PC HP 300.00
  • Macintosh900.00
  • Macintosh700.00

28
Filetest operators
  • -w File is writable by effective uid/gid.
  • -r File is readable by effective uid/gid.
  • -x File is executable by effective uid/gid.
  • -o File is owned by effective uid.
  • -R File is readable by real uid/gid.
  • -W File is writable by real uid/gid.
  • -X File is executable by real uid/gid.
  • -O File is owned by real uid.

29
Filetest operators
  • -e File exists.
  • -z File has zero size.
  • -s File has nonzero size (returns size).
  • -f File is a plain file.
  • -d File is a directory.
  • -l File is a symbolic link.
  • -b File is a block special file.
  • -c File is a character special file.
  • -t Filehandle is opened to a tty.

30
Filetest operators
  • cat filetest.pl
  • !/usr/bin/perl
  • filename"catalog"
  • print "Can do.\n" if -r a -w _ -x _
  • stat(filename)
  • print "Readable\n" if -r _
  • print "Writable\n" if -w _
  • print "Executable\n" if -x _
  • print "Text\n" if -T _
  • print "Binary\n" if -B _
Write a Comment
User Comments (0)
About PowerShow.com