Download presentation
Presentation is loading. Please wait.
Published byCory Merritt Modified over 9 years ago
1
Introduction to Perl Giorgos Georgakilas Graduated from C.E.I.D.Graduated from C.E.I.D. M.Sc. degree in ITMBM.Sc. degree in ITMB Ph.D. student in DIANA-LabPh.D. student in DIANA-Lab geo2mandos@gmail.com Perl>
2
What is Perl? Perl is a Portable Scripting LanguagePerl is a Portable Scripting Language No compiling is needed.No compiling is needed. Runs on UNIX, Linux, MacOS and WindowsRuns on UNIX, Linux, MacOS and Windows Fast and easy text processing capabilityFast and easy text processing capability Flexible syntaxFlexible syntax Flexible data structuresFlexible data structures “Perl is the language for getting your job done.”“Perl is the language for getting your job done.” Larry Wall
3
How to Access Perl Perl comes by default on Linux, Cygwin, MacOSXPerl comes by default on Linux, Cygwin, MacOSX 1.kwrite 2.kate (the best!) A good implementation for windows is Strawberry perlA good implementation for windows is Strawberry perl 1.notepad++ To check if Perl is working and the version numberTo check if Perl is working and the version number >perl –v
4
Perl programs always start with the line: #!/bin/perlPerl programs always start with the line: #!/bin/perl The next 2 lines should always be:The next 2 lines should always be: use warnings; use strict; So let's see a first listing:So let's see a first listing:#!/bin/perl #This is a Perl comment! use warnings; use strict; print "Hello world\n"; Hello World!! @ perl helloWorld.pl Perl Basics
5
Perl supports all the basic arithmetic functions: Double quotes interpret special characters like \n, \tDouble quotes interpret special characters like \n, \t print "5 * 2 = ", 5 * 2, "\n"; print "5 + 2 = ", 5 + 2, "\n"; warn "5 - 2 = ", 5 - 2, "\n"; print "5 squared = ", 5 ** 2, "\n"; print "5 mod 2 = ", 5 % 2, "\n"; Single quotes treat the contents literally:Single quotes treat the contents literally: print '5 * 2 = ', 5 * 2, '\n'; Print formatted strings, like in C.Print formatted strings, like in C. printf ("7 / 3 = %4.2f\n", 7/3); Get data from keyboardGet data from keyboardgetc(); Perl Basics
6
To declare and initiate a scalar variable, use the syntax:To declare and initiate a scalar variable, use the syntax: my $var = 15; ## an integer my $var2 = 1.5; ## a float my $var3 = "hello"; ## a string my $var3 = 'a'; ## a character my ($var4, $var5, $var6) = (1,2,3); ## define several at once. ($var4, $var5) = ($var5, $var4); ## a useful way to swap 2 values. You can also suppress interpolation with a backslash.You can also suppress interpolation with a backslash. print "\$var * \$var2 = ", $var * $var2, "\n"; Scalars
7
Perl Basics To test for definition:To test for definition: if (defined ($var1)) {... To test for truth:To test for truth: if ($var1) {... To test for falsity:To test for falsity: if (!$var1) {... StatusMeaningExample exists Variable has been declared but has no value my $var1; defined Variable has a value, which may be true or false my $var1 = 0; #false NumbersStrings>gt <lt ==eq !=ne <=le >=ge Variable Status & Conditional expressions
8
Perl Basics Strings my $string = "atcgtagctagctgc"; Get length of stringGet length of string print "$string has ", length($string), " characters\n"; Reverse a stringReverse a string my $reversed = reverse($string); print "$string reversed is $reversed\n"; Concatenate strings using '.'Concatenate strings using '.' my $palindrome = $string. $reversed; print "\$palindrome is $palindrome \n"; Get a substring...Get a substring... print "1st 6 letters of $string are ", substr($string, 0, 6), "\n"; print "last 6 letters of $string are ", substr($string, -6, 6), "\n";
9
Perl Basics Arrays A Perl array is simply a list of Perl scalars and is a core data type in Perl. my @array1 = (1, 2, 3, 4, 5); my @array2 = ("string1", "string2", "string3");A Perl array is simply a list of Perl scalars and is a core data type in Perl. my @array1 = (1, 2, 3, 4, 5); my @array2 = ("string1", "string2", "string3"); Using the 'qw' operator just removes the need for quotes around strings: my @quoted_Str = qw(string1 string2 string3);Using the 'qw' operator just removes the need for quotes around strings: my @quoted_Str = qw(string1 string2 string3); Arrays resize automatically as required. To access an element we use the syntax: my $first_el = $array1[0];Arrays resize automatically as required. To access an element we use the syntax: my $first_el = $array1[0]; Let's examine some useful things we can do with arrays.Let's examine some useful things we can do with arrays. my $stringing = "atcgtagctagctgc"; my @a1 = ( 1, 2, 3, 4, 5, 6, 7); my @str = ("a", "b", "c", "d"); To convert an array to a string, use 'join‘To convert an array to a string, use 'join‘ my $string = join ",", @str; print " joined array is $string\n";
10
Perl Basics Arrays Add /remove elements to/from the start of the array..Add /remove elements to/from the start of the array.. unshift @str, "new_1st_element"; print " after unshifting, \@str is ", join",",@str,"\n"; my $new_1st_element = shift @str; And the endAnd the end push @str, "e"; print " after pushing, \@str is ". join",",@str,"\n"; pop @str; print " after popping, \@str is ". join",",@str,"\n"; To get length of array use scalar(@a1)To get length of array use scalar(@a1) print "\@str contains ", scalar @str," elements\n"; Array resizes automaticallyArray resizes automatically $str[500] =56; print "\@1 now contains ", scalar @str," elements\n";
11
Perl Basics Loops The usual…The usual… for (my $i = 0; $i < 10; $i++) { code} If you're looping over every element in an array you can writeIf you're looping over every element in an array you can write for my $i (@array) { code} foreach (@array) { if ($_ > 4)....} 'sort' creates a sorted array. By default it sorts in ascending order.'sort' creates a sorted array. By default it sorts in ascending order. my @sorted = sort @a1; print "\@sorted is ", join ",", @sorted, "\n\n"; E.g., sort the strings alphabetically by their last letter.E.g., sort the strings alphabetically by their last letter. my @str = ("adf", "bde", "crt", "dra"); my @sorted_st = sort{substr($a, -1, 1) cmp substr($b, -1, 1)} @str; print "\@sorted_st is ", join ",", @sorted_st, "\n"; To execute while statements is very similar to other languages:To execute while statements is very similar to other languages: while ($i >0){ do something} As is the do..while statement:As is the do..while statement: do { something at least once }while ($i > 0 );
12
Perl Basics Hashes To declare a hashTo declare a hash my %hash = (key1 => 1, key2 => 2, key3 =>3 ); my $v1 = $hash{'key1'}; $hash{'key1'} = "new value"; To get all the keys in the hash use “keys”To get all the keys in the hash use “keys” my @keys = keys %hash; delete $hash{'key1'}; if(exists( $hash{'key1'} ) ){...}; To iterate through the hash, iterate through the keys:To iterate through the hash, iterate through the keys: for my $key (keys %hash) {print "$hash{$key}\n";} Sort keys before retrieving valuesSort keys before retrieving values for my $key ( sort keys %hash) {print "$hash{$key}\n";}
13
Web Sources for Perl www.perl.com www.perldoc.com www.perl.org www.perlmonks.org
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.