Introduction to Perl
Week 2
These are the topics that we shall cover during the second week.
Why Perl?
- Interpretive language; no compilation no linking. Type-and-go!
- Power; speed
- Portability; runs on ANY platform; UNIX, Windows, Mac
- Popularity and hence extensive support library
- Cost
Hello world!
- Logon to smb.ctp.bilkent.edu.tr (You should have obtained an account on
this computer by now. If you have not yet done so, see Mr. Satuk Erculasun
in the CTP systems room)
- Use vi to enter the following code into a file (hello.pl) ( See "Just
enough vi commands to survive" and the "vi
tutorial" for some help on using the "vi" editor)
print "Hello world!\n";
- Run your program with the command "perl ./hello.pl"
- Modify the hello.pl file so that it contains the lines
#!/usr/bin/perl
print "Hello world\n";
- Modify the access rights to allow execution by everyone (Hint : use the chmod
command)
- Run the new program with the command "./hello.pl"
Perl Variables
- Scalars : $var_name
- Arrays : @array_name
- Indexes start at 0
- Single elements are addressed as scalars (name starts with $) and index
is given between [ ].
- Indexes can be anything; single elements are addressed as scalars (name
starts with $) and index must be enclosed with { }.
Basic I/O
I/O From Standard I/O Devices :
print STDOUT, "text to print";
print "text to print";
$name = <STDIN>;
chomp($name);
print "You have entered $name\n";
Flow Control
- if - else - elsif
- while, last, next
- foreach
- function calls, return
Conversions
- String to numerics, numerics to strings
- Scalars to arrays
- Arrays to scalars