You are on page 1of 13

OPEN SOURCE LAB MANUAL (CS2406 & CS77

)

Open Source Lab Exercise : 9
Text processing with Perl: simple programs, connecting with database e.g., MYSQL Aim : To do some common text processing task using perl Structure of perl program Perl program is a text file. You can use any text editor to create the program. Normally following line will be the first line #! /usr/bin/perl This tells Linux to use /usr/bin/perl executable to interpret rest of the lines in the program. This line may vary depending on the location of perl binary. Sometimes it may be /usr/local/bin/perl or some other place. Commonly .pl extension is used, however you can write without extension also. Pre-requisites : Normally Perl is installed by default Linux Installation. [fosslab@fosslab ~]$ rpm -qa perl perl-5.12.2-136.fc14.i686 [fosslab@fosslab ~]$ The following packages are required to connect Perl with Mysql [fosslab@fosslab ~]$ rpm -qa perl-DBI perl-DBI-1.613-1.fc14.i686 [fosslab@fosslab ~]$ rpm -qa perl-DBD-MySQL perl-DBD-MySQL-4.017-1.fc14.i686

Leenux Technologies , 1st Floor, Almarkz Complex, 222 – Arcot Road, Kodambakkam, Chennai – 24. www.leenux.in

in . 222 – Arcot Road. Almarkz Complex. Chennai – 24. chomp($name).pl Type the following code.leenux. 1st Floor. www.Create a new directory for Perl programs [fosslab@fosslab ~]$ mkdir perlprog [fosslab@fosslab ~]$ cd perlprog/ [fosslab@fosslab perlprog]$ 1. Leenux Technologies . save and close gedit #!usr/bin/perl # the above line is shebang directive $name=<STDIN>. Kodambakkam. print "$name\n". Sample PERL Program Create a new file using the gedit text editor [fosslab@fosslab perlprog]$gedit first.

222 – Arcot Road. # list all elements in array print "$#animal\n". Chennai – 24.leenux. print "@animal\n"."Buffalo". 1st Floor."Camel"). print "$count". print "The square of &ans". print "$ans\n". Array in PERL fosslab@fosslab perlprog]$gedit array.pl Type the following code. Kodambakkam.pl This is my first perl program This is my first perl program [fosslab@fosslab perlprog]$ 2. # count no of elements in array Leenux Technologies . www. #list 0th position element $count=@animal.$ans*$ans. #this is similar to 42243 print "$animal\n".pl Camel 42243 3. Scalar variables in PERL fosslab@fosslab perlprog]$gedit scalar.in ."\n". Almarkz Complex. save and close gedit #!/usr/bin/perl my $animal="Camel".pl Type the following code. save and close gedit #!usr/bin/perl my @animal=("cow". Now run the program as follows [fosslab@fosslab perlprog]$ perl scalar. # list last element position print "$animal[0]\n". # this variable is lexically scoped ie local my $ans=42_243.Now run the program as follows [fosslab@fosslab perlprog]$ perl first.

foreach $key (@keys) { print "$color{$key}\n". Hash in PERL fosslab@fosslab perlprog]$gedit hash. Chennai – 24.pl red 5.leenux.'orange'=>"orange"). Almarkz Complex.in .pl cow Buffalo Camel 2 cow 3 4. # to display all the values in hash @keys=keys %color. print "$color{'apple'}\n". 1st Floor. www.pl Type the following code. Kodambakkam. save and close gedit #!usr/bin/perl %color=('apple'=>"red". save and close gedit #!usr/bin/perl %color=('apple'=>"red". 222 – Arcot Road. To display all values in hash fosslab@fosslab perlprog]$gedit hashvalues.'banana'=>"yellow". Now run the program as follows [fosslab@fosslab perlprog]$ perl hash. } Now run the program as follows [fosslab@fosslab perlprog]$ perl hashvalues.pl Type the following code.Now run the program as follows [fosslab@fosslab perlprog]$ perl array.pl red yellow red orange Leenux Technologies .'orange'=>"orange").'banana'=>"yellow". print "$color{'apple'}\n".

in . Now run the program as follows [fosslab@fosslab perlprog]$ perl ifloop. } Leenux Technologies .6. until($a>10) #is equal to while { print "$a\n". $condition=1. } While loop (until) in perl #!usr/bin/perl $a=0. if($condition) { my $y=100.pl Type the following code. Kodambakkam. print "$a\n". Chennai – 24. print "$y\n". Almarkz Complex. 1st Floor. } print "$a\n". If loop in perl fosslab@fosslab perlprog]$gedit ifloop. print "$y\n". 222 – Arcot Road. save and close gedit #!usr/bin/perl my $a=10. www.leenux.pl 10 100 10 Sample programs If not loop (unless)in perl #!usr/bin/perl $a=5. $a++. unless($a>10) #is equal to if not { print "a less than 10".

123. Function with parameter #!usr/bin/perl sub add() { my ($a.$b.leenux. print $str x 80."buffalo". Chennai – 24. } &add(10. foreach $key(@animals) { print "$key\n".. Function #!usr/bin/perl sub sayHello() { print "Hello\n".25).$b)=@_. print $a. } String operation: #!usr/bin/perl $a="hello".300). Leenux Technologies ."\n".15). 222 – Arcot Road.100. $str="-".for each loop (upper limit is not fixed )in perl #!usr/bin/perl my @animals=("cow". 1st Floor. $b="world". } &sayHello().in . #the values will be stored in @_ array variable for dynamically print $a+$b. Almarkz Complex."/n". www. print "@a\n". Kodambakkam. @a=(10."camel".243.

} $d=&max(10. program to check greater among 3 number #Greatest among 3 number print "Enter A value : ". # shift function remove the first element in array and the remaining element will be stored in $max foreach $v (@_) # $v is the second values in the @_ { $max=$v if $max<$v. return $e. print "The maximum value is $d\n". $b=<>.1050. www. 222 – Arcot Road.in . Symbolic reference That is no values and name for function (anonymous function) Anonymous function is used only when need to use the code only once #!usr/bin/perl $b=sub #anonymous { my ($c. } return $max. Kodambakkam.leenux.76.246. Chennai – 24.800.#De reference the function print "the product is $a\n". 1st Floor. print "Enter b value : ".$d)=@_. $a=&$b(20.76.278.30). Almarkz Complex.345). Leenux Technologies . $a=<>. $e=$c*$d.Find Maximum value using function #!usr/bin/perl sub max() { $max=shift @_. print "Enter c value : ".

} elsif(($b > $c)&&($b > $a)) { print "B is greater".in . Almarkz Complex. Kodambakkam. } Create the table in perl that will be automatically updated in database using mysql First create the database as follows in mysql [fosslab@fosslab ~]$ mysql -u root -p Enter password: mysql> Leenux Technologies .$c=<>. if(($a > $b)&&($a > $c)) { print "A is greater". 1st Floor. Chennai – 24. } else { print "C is greater".leenux. www. 222 – Arcot Road.

00 sec) Now the table is empty . } Leenux Technologies . #to use the build in package we use "Use"."root".leenux.00 sec) mysql> use student. save and close gedit #!usr/bin/perl use DBI. Kodambakkam. #connect to database if(!$dbh) { die("error:$!"). 222 – Arcot Road. Database changed mysql> connect. Connection id: 3 Current database: student mysql> show tables.pl Type the following code. 1st Floor. Chennai – 24.in .To view existing databases mysql> show databases. DBI is the build in Package in perl my $dbh=DBI->connect("dbi:mysql:student". 1 row affected (0. www. Empty set (0. Almarkz Complex. +--------------------+ | Database +--------------------+ | information_schema | | mysql | test | | | +--------------------+ 3 rows in set (0.01 sec) mysql> Create a new database mysql>create database student.""). Query OK. To create the table through PERL program we use the following procedure Open a new terminal [fosslab@fosslab ~]$ cd perlprog/ [fosslab@fosslab perlprog]$ gedit connect.

222 – Arcot Road.in . Kodambakkam.sname varchar(50))").00 sec) Leenux Technologies . $dbh->disconnect. To view the table go back to the other terminal (which is already opened for mysql) mysql> show tables. 1st Floor.pl [fosslab@fosslab perlprog]$ Now the table is created. Now run the program as follows [fosslab@fosslab perlprog]$ perl connect. $sth->execute(). Almarkz Complex. www. +-------------------+ | Tables_in_student | +-------------------+ | students | +-------------------+ 1 row in set (0.leenux. Chennai – 24.$sth=$dbh->prepare("create table students(rollno int.

1st Floor. save and close gedit #!usr/bin/perl use DBI. Chennai – 24. #connect to database if(!$dbh) { die("error:$!"). Almarkz Complex.Insert the values in perl that will be automatically updated in database using mysql as follows [fosslab@fosslab perlprog]$ gedit dbinsert. www.pl Type the following code."root". DBI is the build in package in perl my $dbh=DBI->connect("dbi:mysql:student". # create the table $sth->execute().leenux. 222 – Arcot Road. Kodambakkam.in . } $sth=$dbh->prepare("insert into students values(100. Leenux Technologies . #to use the build in package we use "Use".'thamarai')"). $dbh->disconnect."").

222 – Arcot Road.pl [fosslab@fosslab perlprog]$ Leenux Technologies . Kodambakkam.in . Chennai – 24.Now run the program as follows [fosslab@fosslab perlprog]$ perl dbinsert. Almarkz Complex. www.leenux. 1st Floor.

00 sec) References: http://fls.nrcfoss. Almarkz Complex. +--------+----------+ | rollno | sname +--------+----------+ | 100 | thamarai | +--------+----------+ 1 row in set (0. Chennai – 24. 1st Floor.Now the record is inserted.in/cs2406-manual Leenux Technologies . www. To view the record go back to the other terminal (which is already opened for mysql) mysql> select * from students. 222 – Arcot Road.org.au-kbc.leenux.in . Kodambakkam.