Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Perl Yupu Liang cbio at MSKCC

Similar presentations


Presentation on theme: "Introduction to Perl Yupu Liang cbio at MSKCC"— Presentation transcript:

1 Introduction to Perl Yupu Liang cbio at MSKCC liang@cbio.mkscc.org

2 what is perl ‘A general-purpose programming language originally developed for text manipulation and now used for a wide range of tasks including system administration, web development, network programming, GUI development, and more.’ Practical Extraction and Report Language And: ‘The language is intended to be PRACTICALl (easy to use, efficient, complete) rather than BEAUTIFUL (tiny, elegant, minimal).’ And Some More: ‘Many earlier computer languages, such as Fortran and C, were designed to make efficient use of expensive computer hardware. In contrast, Perl is designed to make efficient use of expensive computer programmers. Perl has many features that ease the programmer's task at the expense of greater CPU and memory requirements. These include automatic memory management; dynamic typing; strings, lists, and hashes; regular expressions; introspection and an eval() function. Larry Wall was trained as a linguist, and the design of Perl is very much informed by linguistic principles. Examples include Huffman coding (common constructions should be short), good end-weighting (the important information should come first), and a large collection of language primitives. Perl favors language constructs that are natural for humans to read and write, even where they complicate the Perl interpreter.’

3 Largely for the same reasons other people use perl! Simplify repetitive Why do Biologists Use Perl? Largely for the same reasons other people use perl Relatively easy to learn (and easier to make a mess too). Tones of reusable code exists (bioperl). incredibly flexible coding style (some argues it is too flexible).

4 How to Use Perl Define the problem Search for existing code Plan your solution Write the code Modify ->Debug ->Modify Run the code

5 Unix Basic Terminal Directories structure Files

6 Unix Basics: Commands ‘ls’ - list contents of directory ‘cd’ - change directory ‘pwd’ - path of current directory ‘cp’ - copy ‘mv’ - move ‘more’ - list contents of file ‘touch’ - create empy file ‘chmod’ - change file permission *note, to get detailed help on any command you can usually type ‘man COMMAND’, ‘COMMAND -h’, or ‘COMMAND --help’. *google is very good on finding the right command

7 Unix Basic: Text Editors Text editors are used to view/create/edit text files You may use any text editor during the course Emacs is a popular choice among perl programmers

8 First program: “Hello World” Open a terminal Make a perl dir(directory) in your home dir Move into the perl directory Create a file named ‘hello_world.pl’ Open the file in your text editor Code the program Save the file Make the program executable Test the program.

9 Perl script #!/usr/bin/perl -w use strict; my $name = ‘yupu’; print “\n hello world from $name.\n”;

10 Perl Data Types Scalar: a single piece of information. Scalars can hold numbers or text $ is the identifier of scalar in perl There are special variables for the system: $ARGV,$! List: an ordered collection of scalars @array = ( 1, 2 ) @words = ( "first", "second", "third" ) Hash: special arrays with words as index.

11 Scalars $num = 42 $num2 = “42” $name = ‘joe’ $color = “blue” $string_w_num = “I have 10 apples” $foo = undef $foo = “” $foo =‘’

12 Scalar Functions: length reverse index http://perldoc.perl.org/perlfunc.html#Perl-Functions-by-Category

13 Quoting: double quotes(“), single quotes(‘) and multi- line quoting(qq) : $var =10; $text = “There are $var apples” $text2 = ‘There are $var apples’ $text3 = qq{ There are $var apples};

14 Functions/Subtroutines Functions are blocks of code which perform specific task #takes no input and returns no output… common practice to use #‘main’ as the starting point in a script. sub main { … } #Takes 2 *scalars* as input sums them and returns one scalar. sub sum_2_numbers { my ($numA,$numB) = @_; #get passed in values my $sum = $numA+$numB; #sum numbers return($sum); #return sum }

15 Control Structures: conditional statements if/else If statements are conditional statements used to test whether or not some condition is met and then continue executing the script. The syntax for an if statement has a specific flow to it - if(conditional_statment) { code to execute; } Logical operators: ==, !=, &&, ||, eq and ne unless

16 Boolean Undefined values are treated as false Scalars are evaluated as: numbers are evaluated as true if non-zero strings are evaluated as true if non-empty $var = “false”; if($var) { print “$var is true!\n”; }

17 Handling user inputs Scripts can take inputs in two ways: Arguments./print_args.pl ARG1 ARG2 Prompted inputs from users $user_text =

18 Error/Exception Handling Things don’t always come out as expected. It is good to check the output of important functions for errors, it is highly recommended to validate any input from users or external sources Die Warn

19 Write some scripts!


Download ppt "Introduction to Perl Yupu Liang cbio at MSKCC"

Similar presentations


Ads by Google