+ All Categories
Home > Documents > 1 Introduction to Perl and Perl Syntax Learning Perl, Schwartz.

1 Introduction to Perl and Perl Syntax Learning Perl, Schwartz.

Date post: 17-Jan-2016
Category:
Upload: lorin-lewis
View: 283 times
Download: 2 times
Share this document with a friend
Popular Tags:
24
1 Introduction to Perl and Perl Syntax Learning Perl, Schwartz
Transcript
Page 1: 1 Introduction to Perl and Perl Syntax Learning Perl, Schwartz.

1

Introduction to Perl and Perl Syntax

Learning Perl, Schwartz

Page 2: 1 Introduction to Perl and Perl Syntax Learning Perl, Schwartz.

2

Next Reading Assignment

"How Perl Saved the Human Genome Project" -- by Lincoln Stein

http://www.foo.be/docs/tpj/issues/vol1_2/tpj0102-0001.html

Page 3: 1 Introduction to Perl and Perl Syntax Learning Perl, Schwartz.

XKCD

3

Page 4: 1 Introduction to Perl and Perl Syntax Learning Perl, Schwartz.

4

Outline

• Perl overview• Perl -- first line of program convention• Comments• Scalar data• Strings -- single and double quotes• Warning flags• Scalar variable names• Interpolation

Page 5: 1 Introduction to Perl and Perl Syntax Learning Perl, Schwartz.

5

Perl• Perl is an interpreted language

– perl programs require an interpreter– once the interpreter is installed on a system, all (most) perl programs

should run on it• Contrast to a compiled program (such as C or C++)

– compiled to a “binary”, or “opaque binary”– a binary is so close to machine language, it is extremely difficult to reverse

engineer (at a high-level) the algorithms implemented– only binaries need to be transferred– not portable across architectures (Windows program will not run on a Mac,

etc)• Performance

– Generally -- compiled programs are faster than interpreted programs (Perl, Java)

– However, processor speeds and available memory sizes have increased to the point that in many bioinformatics applications "Perl is good enough."

– Perl is notorious for being “quick and dirty”– However, well written perl can be elegant and “beautiful” too

Page 6: 1 Introduction to Perl and Perl Syntax Learning Perl, Schwartz.

Perl

• Perl is also a popular tool for system administration

• Simple file I/O syntax• "Pipes" and file re-direction• Perl is often the "glue" between complex software

systems

• Example – SAN server load tester

6

Page 7: 1 Introduction to Perl and Perl Syntax Learning Perl, Schwartz.

7

Perl -- first line of a program (optional)#!/usr/bin/perl

OR #!/usr/local/bin/perlOR#!/usr/bin/apps/perl …. etc. – typical locations, but may be anywhere

• first line of a perl application– allows for automatic invocation– tells the UNIX operating system where to find the Perl interpreter– Ex)

my_program.pl

– without this line we could also explicitly specify interpreter/usr/bin/perl ./my_program.plORperl my_program.pl

• The Windows version supports this too (according to the docs -- but it’s a different mechanism. I suspect windows simply interprets the .pl extension -- but I was unable to find this in the docs ).

Page 8: 1 Introduction to Perl and Perl Syntax Learning Perl, Schwartz.

8

PerlAfter the first line, the interpreter will ignore all lines that begin with: #

#!/usr/local/bin/perlprint “Hello, world!\n”;

Newline = \n = carriage return

Hello, world!server06%

#!/usr/local/bin/perl

print #This is A CoMment – completely ignored!!“Hello, world!\n”

; # Notice how formatting andCommentsCanMake #itMoreDifficult/EasierToRead

Page 9: 1 Introduction to Perl and Perl Syntax Learning Perl, Schwartz.

9

Perl#!usr/local/bin/perlprint “Hello, world!\n”;

% ./test.plinterpreter “usr/local/bin/perl” not found./test.pl: Command not found.

%perl test.pl -- this works

Note that most statements are followed by the pesky semicolon:

print “Hello, world!\n”;

Page 10: 1 Introduction to Perl and Perl Syntax Learning Perl, Schwartz.

10

Scalar Data

scalar – simplest, most basic kind of data in perl (as opposed to a list, or array that is composed of multiple scalars)

Numbersfloating point: 1.25, 255.00, -3.124E-24integers: 0 69 236123321 236_123_321

$i = 236_123_321;print “$i\n”;236123321

Page 11: 1 Introduction to Perl and Perl Syntax Learning Perl, Schwartz.

11

Strings

• contains any combination of any charactersSingle quoted string literals [" '] key (not the [~`]

key):'fred''barney''' # null string'Don\'t isn\'t end\'d \'till here''last character is backslash\\''no newline here\n'

Page 12: 1 Introduction to Perl and Perl Syntax Learning Perl, Schwartz.

12

StringsDouble-quoted literals

Allows for variable interpolation – the substitution of variables within a string

$i = "barney";$j = "fred and $i";"new line here\n""last character is a dble quote\""

Note in MSWord/Poweroint you have to turn off Tools->AutocorrectOptions->AutoFormatAsYouType->(uncheck "replace straight quotes with smart quotes")or you get this: “test” and ‘test’

Page 13: 1 Introduction to Perl and Perl Syntax Learning Perl, Schwartz.

13

Strings

• Special characters\n newline

\t tab

\r carriage return

many others

Page 14: 1 Introduction to Perl and Perl Syntax Learning Perl, Schwartz.

14

Strings

• concatenation of strings (.) period"Go" . 'hawks'. ' '."\n" # Gohawks - with newline

String repetition (x) :$i = "Hawks" x 3; # $i = HawksHawksHawks

5 x 4; # equals 5555# the integer 5 is converted to a string

4 x 5.6 # equals 44444 (5.6) truncated to 5

Page 15: 1 Introduction to Perl and Perl Syntax Learning Perl, Schwartz.

15

Auto Conversion between Numbers and Strings

Perl is useful for rapid prototyping, so it converts strings/numbers to numbers/strings depending on operator context (this is not necessarily a good thing -- so you have to be careful)

5 + 5 # equals 10

5 . 5 # equals '55'

"12" * "3" # equals 36

"12fred34" * '3' # equals 36

"fred" * "3" # equals 0 (these are all silly cases)

Page 16: 1 Introduction to Perl and Perl Syntax Learning Perl, Schwartz.

16

C example – char is still an intmain(){char c = 'f';

int i = 3;int j = 0;

j = c*i;

printf("%d\n",j);

}

Page 17: 1 Introduction to Perl and Perl Syntax Learning Perl, Schwartz.

17

Warnings% perl –w program.pl

OR#!/usr/local/bin/perl –w

Argument "12fred34" isn't numeric in multiplication (*) at ./testl.pl line 18.

Warnings are very, very good – but note that the program still executes.

Checks for ‘unsafe’ constructs/mistyping.

How do you specify "-w" in Eclipse?

Page 18: 1 Introduction to Perl and Perl Syntax Learning Perl, Schwartz.

18

Scalar Variables

A variable contains/holds a scalar value (number/string).

Cannot start with a numberCase sensitiveAll scalar variables are demarcated by $

$i$This_is_aVariable259-1$A$a

Page 19: 1 Introduction to Perl and Perl Syntax Learning Perl, Schwartz.

19

Choose good Variable Names$i$line_length #underscore is convention$lineLength #works too

Assignment$fred = 17; # $fred variable has interger 17$word = "Word"; $word = 3 * $fred; # word == 51$words = $fred . "Word". $word; # "17Word51"

$word = "$fred is prime"; ## does this work??? $word = '$fred is prime'; #how about this?

Page 20: 1 Introduction to Perl and Perl Syntax Learning Perl, Schwartz.

20

Interpolation

Scalar variable name in string is replaced with current value.

$isu = "a team that needs to improve";$hawks = "will the hawks beat $isu";# $hawks == "will the hawks beat a team that

needs to improve"$hawks = 'will the hawks beat' . $isu;

print 'I like $money'; # prints dollar-sign

Page 21: 1 Introduction to Perl and Perl Syntax Learning Perl, Schwartz.

21

Comparison

Comparison numeric string

equal == eqnot equal != neless than < ltgreater than > gtless than/ <= leequal togreater than/ >= geequal to

Page 22: 1 Introduction to Perl and Perl Syntax Learning Perl, Schwartz.

22

Control Structure

Want to make decisions … next lectures

Page 23: 1 Introduction to Perl and Perl Syntax Learning Perl, Schwartz.

#!/usr/bin/perl

$sleep_count=24;

$in = "";

$out = "";

$|=1; # flush the output buffer

#$path = "/Volumes/ReadWriteTest";

$path = "/Volumes/Macintosh HD/Users/tabraun/perl/ReadWriteTest";

print "Testing SAN read/write on $path\n";

$host = `hostname`;

chop($host);

while($sleep_count>0) {

#$arraySize=100_000_000; #made shorter -- just for demo

$arraySize=10_000; #made shorter -- just for demo

$count=10_000;

print "allocating big array\n";

while($arraySize > 0) # just allocate a big array to

{ # stress test memmory $array[$arraySize]=$arraySize;

$arraySize=$arraySize-1;

print "arraySize = $arraySize\r";

}

while($count>0) {

$file = 'write-'.$host.'-'.$count;

open(WRITE,">$path"."/"."$file.txt");

$out="";

for($j=0;$j<100;$j++) # 100 lines

{

for($k=0;$k<6;$k++) #60 characters per line (6 * 10)

{

for($i=0;$i<10;$i++) # digits 0-9

{

$out=$out.$i;

print WRITE $i;

}

}

$out=$out."\n";

print WRITE "\n";

}

close WRITE;

23

Page 24: 1 Introduction to Perl and Perl Syntax Learning Perl, Schwartz.

open(READ,"$path/$file.txt") || die ("Cannot open $path/$file.txt\n");

$in="";

while($line = <READ>)

{

$in = $in.$line;

}

if($in == $out) {

print "No failures iteration $count\n";

}

else{

print "Error -- writing differences to errorWrite-$file.txt and errorRead-$file.txt\n";

open E1,">$path/errorWrite-$file.txt";

open E2,">$path/errorRead-$file.txt";

print E1 "$out";

print E2 "$in";

close E1;

close E2;

}

close(READ);

$count=$count-1;

} # end while loop

printf("\n");

$x=10;

while($x>0)

{

print "sleeping for $x seconds\r";

$x--;

sleep(1); #sleep for an hour to keep the process alive

# for at LEAST 24 hours.

}

printf("\n");

$sleep_count=$sleep_count-1;

}

24


Recommended