Ex No: 3 Text Processing With Perl Date

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

EX NO DATE

:3 :

TEXT PROCESSING WITH PERL

________________________________________________________________________________________ 1.OBJECTIVE: To create a server side PERL script to retrieve dynamic information from a website .To use MySQL to store the retrieved information and to perform some text processing operation on the retrieved information. 2.INSTALLATION PROCEDURE: 2.1 PERL: To install Perl : yum install perl To Save A Perl File : Write a perl file and save it in the extension .pl To uninstall perl : #yum remove perl 2.2MySQL: Command to install mysql: #yum install mysql mysql-server Location of mysql: Filesystem/usr/share/mysql To start mysql service: #/etc/init.d/mysqld start(or) #service mysqld start To login to mysql: #mysql -u root -p Enter password Syntax for mysql connection in php: use LWP::Simple; use DBI; my $dbh = DBI->connect( "dbi:mysql:dbname=mydb", "root", "saranya", { RaiseError => 1 }, ) or die $DBI::errstr;

To exit from mysql: exit To stop mysql service: #/etc/init.d/mysqld stop. (or) #service mysqld stop. To reset password in mysql: 1).Stop the mysql service. 2).Restart mysql in safe mode: #mysqld_safe skip-grant-tables& 3).Login to mysql at the command line: #mysqld -u root. 4).To create new password: >> update user set password=PASSWORD(new_password) where user='root'. 5).Exit from mysql. To unistall mysql: #yum remove mysql 2.3 Aditional Information: To go to root user: su Enter root password. Network preference setting: Switch to System->Preference->Network Proxy. Enable manual proxy configuration. Set HTTP proxy to proxy.ssn.net and port to 8080. To change the access permission of a file: chmod -R 777 file location3. Application: 3.1 Description: The application retrieves the informations such as time,date,sunset,sunrise for a country from a website and displays it to the user. It also updates the retrieved country name in the database. 3.2 Procedure: 1)Mysql connection is established. If connection is not established ,error message is displayed. 2)Proxy is set up using the procedure mentioned above. 3)The source code is retrieved from the website: my $url = "http://www.sunrisesunset.com/"; my $str=get($url);

4)The required position in the source code is identified and the country information particular country is obtained using substr() 5)The retrieved information is then written into the html file which is then rendered to the user. 3.3 MySQL commands: To drop the Database : drop database mydb; To Create a Database : create database mydb; To use the mysql database: use mydb; To create a Table: create table country ( country varchar(50) ); MYSQL updation through perl: $sql1 = "insert into country values('$obtain2')"; $sql2 = "insert into country values('$obtain3')"; $sql3 = "insert into country values('$obtain4')"; $sql4 = "insert into country values('$obtain5')"; $sql5 = "insert into country values('$obtain6')"; my $dbvar = $dbh->prepare($sql1);$dbvar->execute(); my $dbvar1 = $dbh->prepare($sql2);$dbvar1->execute(); my $dbvar2 = $dbh->prepare($sql3);$dbvar2->execute(); my $dbvar3 = $dbh->prepare($sql4);$dbvar3->execute(); my $dbvar4 = $dbh->prepare($sql5);$dbvar4->execute(); 3.4.TEXT PROCESSING IN PERL: (APPLICATION NAME : COUNTRY INFORMATION RETRIEVAL) Perl has always been known for its text processing and manipulation abilities. String functions to (among other things) print and format strings, split and join string values, alter string case, and perform regular expression searches are some of the text processing functions. Here we have performed substring retrieval : Substring retrieval: $obtain1 = substr($str,49650,3780); $obtain2 = substr($str,49864,15); $obtain3 = substr($str,50612,18); $obtain4 = substr($str,51367,17); $obtain5 = substr($str,52197,12); $obtain6 = substr($str,52942,16);

PROGRAM CODING : INTERFACE file:[CLIENT] <html> <head> <script type="text/javascript"> function func() { window.location="file1.html"; } </script> </head> <body bgcolor="orange"><center><br/><br/> <b>Country Information : </b><br/><br/> <table border="3"> <tr><td> <form action="file.html"> <input type="button" value="Retrieve Countries Information" onclick="func();"> </form> </td></tr></table> </center> </body> </html> PERL FILE : #!/usr/bin/perl use LWP::Simple; use DBI; my $dbh = DBI->connect( "dbi:mysql:dbname=mydb", "root", "saranya", { RaiseError => 1 }, ) or die $DBI::errstr; #$proxy = 'http://proxy.ssn.net:8080'; #my $ua=new LWP::UserAgent; #$ua->proxy(['http', 'https'], $proxy); my $url = "http://www.sunrisesunset.com/"; my $str=get($url); $obtain1 = substr($str,49650,3780); $obtain2 = substr($str,49864,15); $obtain3 = substr($str,50612,18); $obtain4 = substr($str,51367,17); $obtain5 = substr($str,52197,12); $obtain6 = substr($str,52942,16);

print print print print print print print print print print

$obtain2; "\n"; $obtain3; "\n"; $obtain4; "\n"; $obtain5; "\n"; $obtain6; "\n";

my $myfile = '/var/www/file1.html'; open(FILE, "> $myfile") || die "Could not open $myfile: $!\n"; print FILE '<html><body><center><b><i><u><br/><br/>'; print FILE 'Information Retrieved are as follows => :'; print FILE '</b><u></i><br/><br/></center></body></html>'; print FILE $obtain1; close(FILE); $sql1 = "insert into country values('$obtain2')"; $sql2 = "insert into country values('$obtain3')"; $sql3 = "insert into country values('$obtain4')"; $sql4 = "insert into country values('$obtain5')"; $sql5 = "insert into country values('$obtain6')"; my $dbvar = $dbh->prepare($sql1);$dbvar->execute(); my $dbvar1 = $dbh->prepare($sql2);$dbvar1->execute(); my $dbvar2 = $dbh->prepare($sql3);$dbvar2->execute(); my $dbvar3 = $dbh->prepare($sql4);$dbvar3->execute(); my $dbvar4 = $dbh->prepare($sql5);$dbvar4->execute(); OUTPUT : TERMINAL EXECUTION :

Interface: [CLIENT] :

OUTPUT : [SERVER]

MySQL: (DATABASE OUTPUT) :

4.CONCLUSION: In fedora environment, MySQL , Perl were installed. Perl file is created to retrieve the information such as sunset,sunrise,time,date for a country from a website "http://www.sunrisesunset.com/". The country information is retrieved and is written to the HTML file and is then updated in the database. The text processing operations namely substring retrieval are also performed on the string retrieved from the website.

You might also like