introduction to perl day1
DESCRIPTION
Basic perlTRANSCRIPT
2
What is Perl? Perl is a Portable Scripting Language
No compiling is needed. Runs on Windows, UNIX, LINUX and cygwin
Fast and easy text processing capability Fast and easy file handling capability Written by Larry Wall “Perl is the language for getting your job done.”
3
How to Access Perl
To install at home Perl Comes by Default on Linux, Cygwin, MacOSX www.perl.com Has rpm's for Linux www.activestate.com Has binaries for Windows
Latest Version is 5.18 To check if Perl is working and the version number
% perl -v
4
Resources For Perl Books:
Learning Perl By Larry Wall Published by O'Reilly
Programming Perl By Larry Wall,Tom Christiansen and Jon Orwant Published by O'Reilly
Web Site http://safari.oreilly.com
Contains both Learning Perl and Programming Perl in ebook form
6
The Basic Hello World Program
which perl Program:
#! /…path…/perl -wprint “Hello World!\n”;
Save this as “hello.pl” Give it executable permissions
chmod a+x hello.pl Run it as follows:
./hello.pl or perl hello.pl
7
“Hello World” Observations
“.pl” extension is optional but is commonly used The first line “#!/usr/local/bin/perl” tells UNIX where
to find Perl “-w” switches on warning : not required but a really
good idea
9
Numerical Literals Numerical Literals
6 Integer 12.6 Floating Point 1e10 Scientific Notation 6.4E-33 Scientific Notation 4_348_348 Underscores instead of
commas for long numbers
10
String Literals String Literals
“There is more than one way to do it!” 'Just don't create a file called -rf.‘ “Real programmers can write assembly in any
language.”
Quotes from Larry Wall
11
Types of Variables
Types of variables: Scalar variables : $a, $b, $c Array variables : @array Hash variables : %hash File handles : STDIN, STDOUT, STDERR
Variables do not need to be declared Variable type (int, char, ...) is decided at run time
$a = 5; # now an integer $a = “perl”; # now a string
12
Operators on Scalar Variables
Numeric and Logic Operators Typical : +, -, *, /, %, ++, --, +=, -=, *=, /=, ||, &&, ! etc
…
String Operators Concatenation: “.” - similar to strcat
$first_name = “Larry”; $last_name = “Wall”;$full_name = $first_name . “ “ . $last_name;
13
Equality Operators for Strings Equality/ Inequality : eq and ne
$language = “Perl”;if ($language == “Perl”) ... # Wrong!if ($language eq “Perl”) ... #Correct
Use eq / ne rather than == / != for strings
14
Relational Operators for Strings
Greater than Numeric : > String : gt
Greater than or equal to Numeric : >= String : ge
Less than Numeric : < String : lt
Less than or equal to Numeric : <= String : le
15
String Functions Convert to upper case
$name = uc($name); Convert only the first char to upper case
$name = ucfirst($name);
Convert to lower case $name = lc($name);
Convert only the first char to lower case $name = lcfirst($name);
16
A String Example Program Convert to upper case
$name = uc($name); Convert only the first char to upper case
$name = ucfirst($name);
Convert to lower case $name = lc($name);
Convert only the first char to lower case $name = lcfirst($name);#!/usr/bin/perl$var1 = “larry”;$var2 = “moe”;$var3 = “shemp”;……Output: Larry, MOE, sHEMP
17
A String Example Program
#!/usr/local/bin/perl$var1 = “larry”;$var2 = “moe”;$var3 = “shemp”;
print ucfirst($var1); # Prints 'Larry'print uc($var2); # Prints 'MOE'print lcfirst(uc($var3)); # Prints 'sHEMP'
18
Variable Interpolation
Perl looks for variables inside strings and replaces them with their value
$stooge = “Larry”print “$stooge is one of the three stooges.\n”;
Produces the output:Larry is one of the three stooges.
This does not happen when you use single quotesprint '$stooge is one of the three stooges.\n’;
Produces the output:$stooge is one of the three stooges.\n
19
Character Interpolation
List of character escapes that are recognized when using double quoted strings \n newline \t tab \r carriage return
Common Example :
print “Hello\n”; # prints Hello and then a return
20
Numbers and Strings are Interchangeable
If a scalar variable looks like a number and Perl needs a number, it will use it as a number
$a = 4; # a numberprint $a + 18; # prints 22$b = “50”; # looks like a string, but ...print $b – 10; # will print 40!
22
If ... else ... statements
if ( $weather eq “Rain” ) {
print “Umbrella!\n”; }
elsif ( $weather eq “Sun” ) { print “Sunglasses!\n”;}else { print “Anti Radiation Armor!\n”;}
23
Unless ... else Statements
Unless Statements are the opposite of if ... else statements.
unless ($weather eq “Rain”) { print “Dress as you wish!\n”;}else { print “Umbrella!\n”;}
And again remember the braces are required!
25
Until Loop
The until function evaluates an expression repeatedly until a specific condition is met.
Example: $i = 0; until ($i == 1000) { print “$i\n”;
$i++;}
26
For Loops
Syntax 1: for ( $i = 0; $i <= 1000; $i=$i+2 )
{ print “$i\n”; }
Syntax 2: for $i(0..1000)
{ print “$i\n”; }
27
Moving around in a Loop
next: ignore the current iteration last: terminates the loop.
What is the output for the following code snippet:for ( $i = 0; $i < 10; $i++)
{ if ($i == 1 || $i == 3) { next; } elsif($i == 5) { last; }
else {print “$i\n”;} }
29
Exercise Use a loop structure and code a program that
produces the following output:
AAAAAAAAABAAABAAAABAAAAABAAAAAABAAAB
…..
TIP: $chain = $chain . “A”;
30
Exercise
#! /usr/bin/perl
for ($i=0, $j=0; $i<100; $i++){
if ( $j==3){$chain.=“B”;$j=0;}else {$chain.=“A”; $j++;}print “$chain\n”;
}
31
Exercise: Generating a Random Sample
A study yields an outcome between 0 and 100 for every patient. You want to generate an artificial random study for 100 patients:
Patient 1 99Patient 2 65Patient 3 89….
Tip:-use rand 100 to generate values between 0 and 100 :
rand 100