1 Introduction to Perl and Perl Syntax Learning Perl, Schwartz

Click here to load reader

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

of 24

  • date post

  • Category


  • view

  • download


Embed Size (px)

Transcript of 1 Introduction to Perl and Perl Syntax Learning Perl, Schwartz

  • *Introduction to Perl and Perl SyntaxLearning Perl, Schwartz

  • *Next Reading Assignment"How Perl Saved the Human Genome Project" -- by Lincoln Stein


  • XKCD*

  • *OutlinePerl overviewPerl -- first line of program conventionCommentsScalar dataStrings -- single and double quotesWarning flagsScalar variable namesInterpolation

  • *Perl

    Perl is an interpreted languageperl programs require an interpreteronce the interpreter is installed on a system, all (most) perl programs should run on itContrast to a compiled program (such as C or C++)compiled to a binary, or opaque binarya binary is so close to machine language, it is extremely difficult to reverse engineer (at a high-level) the algorithms implementedonly binaries need to be transferrednot portable across architectures (Windows program will not run on a Mac, etc)PerformanceGenerally -- 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 dirtyHowever, well written perl can be elegant and beautiful too

  • PerlPerl is also a popular tool for system administrationSimple file I/O syntax"Pipes" and file re-directionPerl is often the "glue" between complex software systems

    Example SAN server load tester*

  • *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 applicationallows for automatic invocationtells the UNIX operating system where to find the Perl interpreterEx)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 its a different mechanism. I suspect windows simply interprets the .pl extension -- but I was unable to find this in the docs ).

  • *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%


    print #This is A CoMment completely ignored!!Hello, world!\n; # Notice how formatting andCommentsCanMake #itMoreDifficult/EasierToRead

  • *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;

  • *Scalar Datascalar 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

  • *Stringscontains 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'

  • *StringsDouble-quoted literalsAllows 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

  • *StringsSpecial characters\nnewline\t tab\r carriage return

    many others

  • *Stringsconcatenation 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 string4 x 5.6 # equals 44444 (5.6) truncated to 5

  • *Auto Conversion between Numbers and StringsPerl 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 105 . 5 # equals '55'"12" * "3" # equals 36"12fred34" * '3' # equals 36"fred" * "3" # equals 0 (these are all silly cases)

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

    int i = 3;int j = 0;

    j = c*i;



  • *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?

  • *Scalar VariablesA variable contains/holds a scalar value (number/string).Cannot start with a numberCase sensitiveAll scalar variables are demarcated by $


  • *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?

  • *InterpolationScalar 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

  • *ComparisonComparisonnumericstring

    equal==eqnot equal!=neless thangtless than/=geequal to

  • *Control StructureWant to make decisions next lectures

  • #!/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

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

    $in=""; while($line = ) { $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;}


    ****Now, finally, we will switch over to the second portion of my talk, and begin covering the Perl programming language. To being, Perl is an interpreted language. This means that perl programs require an interpreter to be installed on any system that they will be run on. However, once the interpreter is installed on a system, every perl program should run. This is in contrast to compiled languages such as C, where each program must be compiled into a binary. And then only the binary program must be tranferred between computers. However, binary programs are not portable across different computer architectures. Thus a program that runs on an Intel platform wont run on a PowerPC platform.

    *The first line of a Perl program in UNIX is #!/usr/bin/perl (a pointer to the location of the perl interpreter). This is a special notation supported by the UNIX operating system, that allows automatic invocation of the appropriate interpreter, as specified by the program. Without this line, a perl program would be started by perl my_program.pl.

    As a brief note, the # character is used to delimit comment fields within perl programs. Any characters after a # will be ignored by the interpreter.

    **Missing first forward slash*************Write 10,000 files, and turn around and read file created. Used to stress test our SAN*