Structure of Perl Program: An Introduction To Perl Language
Structure of Perl Program: An Introduction To Perl Language
P.Raman Licence: This document is released under Creative Commons-By Attribution-Share alike This tutorial on perl programming is written with curriculum of Anna University, Coimbatore for BE -CSE in mind. The details are: Anna University Coimbatore BE -CSE Curriculum - Semester VI- Open source Software. Perl backgrounder Perl overview Perl parsing rules Variables and Data Statements and Control structures Subroutines, Packages, and Modules- Working with Files Data Manipulation. This tutorial is not aimed at text-book standard. It is to be treated as a quick guide for hands-on experience. I have omitted most of Perl backgrounder, overview and parsing rules - as they can be studied easily with little to practice. perl is simple and yet powerful scripting language. It can be used anywhere in the software eco-system system administration, network, desktop or web. In this small notes we will learn how to write simple programs for practice in a lab. Assumptions and limitations a.It is assumed that perl is already installed in your system b.It is assumed that you are running Linux c.Error checking, debugging, best practices are not covered here d. Anything given between two lines is to be practised by the student.
Assuming that you have saved it as prog.pl, we will see how to run the program.
$ perl prog.pl
Method b: Grant executable permission using chmod command and then invoking program name
$ /home/ram/prog.pl
Note: For method b to work #! line is compulsory and ensure that #! occupies first and second character in the file. If every thing goes well you will see hello world in your prompt. Now that we know how to create and run let us go into language details.
1.Comments:
# symbol is used for comments. All text from # till end of line is treated as comment. e.g
2. Statement terminator ;
All Statements end with ; like C language.
3.print
print is simple function to display/output something on monitor/stdout. e.g
4.Variables
Variables are memory location to store information. Variables are type less i.e there is no data type like int,char. Every variable is a string and depending on the context will be treated as int, float etc. There are 4 kinds of variables namely scalars,lists,arrays,hashes.
5 .Scalars
Scalar variables contain singular value like 10,hello etc Name of scalar variable is prefixed with $ symbol. eg.
# in string context # in numerical context #treated as numeric #treated as string. '.'(dot) is a concatenate operator
6.Handling quotes
" (double quote) is used when interpolation/substitution is required. e.g
will substitute $name with its value and output 'hello Raman'. ' (single quote) is used when it is a literal string. Special characters will not be interpreted. e.g
7.Lists
List variables are noted by symbol (). List is just a list of values - may be constants, scalars etc e.g (a,b,c) or ($name,$age,$sex) They can be referred with index also. The index are specified inside a square bracket [ ]. e.g
8.Conditionals - IF
The syntax of if statement is if ( condition) { } elsif (condition){ } else { } The if statement is similar to if in C language, except * flower brace is required even for single statement * else if is noted by elsif (note missing e). e.g
$mark=40; if ($mark>75){ print "passed with distinction\n"; } elsif ($mark<35){ print "failed\n"; } else { print "passed\n"; }
9.Accepting input
Keyboard inputs can be accepted using <STDIN>. e.g
print "enter your name "; $name=<STDIN>; chomp $name; #chomp function is similar to fflush in C. #It removes trailing newlines if any print "Welcome $name\n";
Exercise:
Accept age. Type child if age below 12, type senior citizen when age above 60,otherwise type adult
10.Loops
for for loop syntax is similar to c. It can also be used for iterating on a list. foreach is same as for. Both for and foreach are used interchangeably. for readability. Classical for as in 'C' e.g.
Explanation: foreach will execute the body once for every element in the list - 3 times in this case. Each time the variable $i will get the value it is iterating ie. $i will be 'a' first time 'b' second time and 'c' the third time. uc - is a perl function to change a string into upper case. You can combine functions like 'print uc $i' instead of print(uc($i)). Also note that brackets are optional for passing arguments to functions like uc, print. The output will be ABC. while while loop is used to iterate and has syntax similar to C. e.g
The above foreach is similar to what is given under section 10, however $i is omitted. Still perl will output same i.e 'ABC'. This is because perl uses default variable $_ to store and expands the lines as
Similarly $_ is used in the following case where '..' the generator function is used.
12.Arrays
Arrays are used to store multiple ordered values. Array variables should have prefix @. The size of array need not be specified beforehand. Each element of the array is scalar. Index starts with zero. Whenever the whole array is required @array will be used. Suppose we want only an element then $array[ ] will be used as every element is a scalar, thus the prefix $. e.g
Assigning element
New element can be appended at the end using push function e.g.
$#array is a special variable containing index of last element. It will be -1 for an empty array. In the above example $#array will be 2.
Classical for can be used for iterating on array like this e.g
@array=(1,2,3); for ( $i=0; $i<scalar(@array); $i++ ) { print "i=$i array element=$array[$i]\n"; } for ( $i=0; $i<=$#array; $i++ ) { print "i=$i array element=$array[$i]\n"; }
13.Hashes
Hash is associative/named array.They are key-value pairs. It is similar to array, except that we can use strings as index instead of 1..n. Hash variables will have % as prefix. The contents of hash are called values and index is called key. e.g
%fruits= ( 'apple' =>'red', 'banana'=>'yellow', 'grape' =>'black' ); print "colour of apple =$fruits{apple}\n";
Here the list should contain even number of values. First element will be treated as key, second element value, third element key, fourth value and so on and so forth. In short odd elements will be keys, even elements will be values. Individual elements of hash are accessed by means of $hash{key} e.g.
Note the { } instead of [ ] as in the case of array; Looping on hashes - keys function e.g
Explanation: keys is a function which return a list of key values.The list ( ) will contain apple,banana,grape while running.
14. Subroutines
Subroutines can be defined using sub keyword. The arguments passed will be in a default array @_; e.g
This should give output 30. You can return value using return statement.
$v1=10; $v2=30; #v1,v2 global $v3=30; $v3=add( $v1,$v2 ); sub add{ my ($i,$j)=@_; print "inside add sub value of i=$i j=$j\n"; print "inside add sub value of globals v1=$v1 v2=$v2 v3=$v3\n"; return $i+$j; } print " Value of globals v1=$v1 v2=$v3\n"; print " Value of scoped variables v3=$v3\n"; print " Value of variables inside sub i=$i j=$j\n";
for (my $i=0; $i<10; $i++ ) { print "inside for i=$i\n"; } print "outside for i=$i\n";
16.use strict
In perl you need not define variables before using. By default all variables are global. However, this may lead to errors due scope conflict or errors in naming. 'use strict' is a pragma which will help in avoiding such problems. Once use strict is used, every variable has to be declared with proper scope using my. e.g
The above code will not run and produce error. The corrected one will be like this e.g
use strict; my $v1=10; my $v2=20; add ( $v1,$v2 ) ; sub add { my ($a, $b)=@_; print $a+$b; }
17. References
References are address of the variable, similar (but not exactly) to pointers in C. You can take a reference by using \ . It can be dereferenced by using $$ e.g
$a=10; $ref_toa=\$a; print "value of a using reference = $$ref_toa\n"; print "Value of using directly=$a\n Reference of a= $ref_toa \n";
18.File handling
File handling can be done after opening a file and getting handle similar to C. e.g open( $fh, '<' , 'data.txt'); here $fh - file handle which is a scalar variable. It is also common to use uppercase variable like FH.
< - open read only data.txt - name of the file. Full path name should be given it it is not in current directory File reading line by line can be done like $line=<$fh>; File writing can be done using print and filehandle like this print $fh "hello"; We will see file handling by means of an example. Problem: Open data.txt file. Copy contents to udata.txt duly converting into upper case e.g
open ( $fh, "<", "data.txt" ); #open file read only open ($fh1,">","udata.txt"); #Open file write mode while ( $line = <$fh> ) { #read line by print "line=$line"; #display content on screen print $fh1 uc($line); #write upper cased content to new file } close($fh); close($fh1);
Similarly lc will convert into lowercase. split: split is function which will split a string into components based on delimiter specified, and return a list of values. eg.
In the above example split will use / as delimiter. Note that / is special character. If we want literal / then we should use \/. length: length returns the number of characters in a string.
20.Context
In perl many operations/functions perform differently based on context. For example @array will have different meaning like
In the above line perl will print contents of the array in two different ways,first one in string context and second in list context.
In the above example perl will put 3 into $size i.e the number of elements in @array because it is used in scalar context (singular value). Similarly split will return different values according to context. In the previous split returned a list of values. If used in scalar context split will return number of values i.e count
"\n";
package nameofthepackage; BEGIN{ any startup processing} all functions/subroutines return 1; END{ any winding up processing} Assume we have three functions add, subtract and multiply which we frequently use in our program. We can create a package like this
package arithmetic; BEGIN {} sub add { my ($a,$b)=@_; return $a+$b; } sub subtract { my ($a,$b)=@_; return $a-$b; } sub multiply { my ($a,$b)=@_; return $a*$b; } return 1; END {}
The above can be stored in a file say arithmetic.pl. We can use this package in other program like this
Modules are same as packages, with few extra conditions imposed. Only one package can be in a file and name of the file should have packagename.pm . The functions which are exported can be used without specifying the package name i.e. without ::. The above arithmetic.pl can be copied can be converted into a module like this. (store it as Arithmetic.pm).Note filenames are case sensitive.
package Arithmetic; BEGIN { use Exporter; @ISA=qw(Exporter); @EXPORT=qw(&myadd &mysubtract);} sub myadd {
my ($a,$b)=@_; return $a+$b; } sub mysubtract { my ($a,$b)=@_; return $a-$b; } sub mymultiply { my ($a,$b)=@_; return $a*$b; } return 1; END {}
use Arithmetic; $v=1; $x=10; $z=myadd($v,$x); print "sum=$z\n"; $z=mysubtract($v,$x); print "sum=$z\n"; $z=Arithmetic::mymultiply($v,$x); print "sum=$z\n";
WEB DEVELOPMENT with perl cgi perl offers good features for web development. The simplest way to create web pages is through using CGI modules. CGI helps us to generate html on the fly. It takes care of passing parameters between different calls. Using cgi involves following steps a.Identify the location in the web server hierarchy, where cgi scripts should be placed b.Create the script using any editor c.Copy the script to the cgi directory and grant executable permission to the web server d.Point your browser to this location and start viewing the page We will understand the above process by creating a hello world page Step 1 Use your editor and create the following script and name it hello.cgi
Explanation: Line 1: #! /usr/bin/perl - This line tells the OS to use perl binary to run the program. The location of perl may vary in some systems Line 3: use CGI lets us get all CGI module which handles the communication with web server and the client. Line 4: my $cgi=new CGI : This creates a new cgi object. Line 5: print $cgi->header(): This outputs header information of the html page. In cgi we use print to push the html generated, to the webserver for onward transmission to the client. CGI has methods whose names are similar to the html tags/elements. Line 6: print $cgi->start_html: This prints top of html page with optional information like Language etc. Line 7: print $cgi->h1("Hello World"): This generates an H1 header with hello world as the content Line 8: print $cgi->end_html(): The ends html by print /body and /html tags Step 2: Save this script in /var/www/cgi-bin directory if you are on Fedora/Redhat Linux. If you are in Debian/Ubuntu save in /usr/lib/cgi-bin. Note: If you do not have permission to this directory ask your lab system administrator to provide suitable solution. Step 3: Grant executable permission to this script using chmod command e.g. chmod 755 /var/www/cgibin/hello.cgi Step 4: Open your browser and goto http://localhost/cgi-bin/hello.cgi . You should get the Hello World page. Now we will see how to create interactive pages. We will create two scripts test1.cgi and test2.cgi . The first one will display a form, accept the user name and link to another page
#! /usr/bin/perl use CGI; $cgi=new CGI; print $cgi->header(); print $cgi->start_html( -title=>'My first cgi programme' ); print $cgi->h1("This is header"); print $cgi->h2("This is second header"); print "This is a simple text"; print $cgi->p; print $cgi->startform( -method=>'POST', -action=>"http://localhost/cgi-bin/test2.cgi" ); print "Please Enter your name"; print $cgi->textfield('name'); print $cgi->p;
Explanation: Line 1 to Line 11: Similar to last example Line 12-15: Creates a form with POST method and action pointing to test2.cgi script Line 17: print $cgi->textfield('name'): This create a text box whose content will be stored in variable name. Line 19: print $cgi->submit: Creates a submit button Line 20: print $cgi->reset: Creates a reset button Line 21: print $cgi->endform: Ends the form with /form tag Store the above script in test1.cgi under cgi-bin directory. When you point the browser to this a form will be displayed where name can be entered. Upon pressing submit button it should link to test2.cgi (which is our next example) In the second part we will create script which will display the name entered using test1.cgi script. It also check if name is entered. Let us call this script test2.cgi
#! /usr/bin/perl use strict; use CGI; my $cgi= new CGI; print $cgi->header(); print $cgi->start_html(-title=>'response'); my $name=$cgi->param('name'); if ($name){ print "The name you entered in the last page is : $name"; } else{ print "You have not visited previous page. Here is the reference "; print $cgi->a({href=>"http://localhost/cgi-bin/test1.cgi"},"Click here"); } $cgi->end_html();
Explanation: Line 1- 6: same as in previous examples Line 7: $name =$cgi->param('name'): param method retrieves parameters passed to the script from the browser through server. Remember in our test1.cgi we assigned name to the textarea. This name can now be accessed using param('name) method. We retrieve this and store in $name variable
Line 8-14: We use if condition to print the message with name. If name is empty we provide a link to our test1.cgi scrip using cgi->a() method Store this script in cgi-bin directory.Now if you run test1.cgi, feed some name, press submit, then test2.cgi should be executed and name will be displayed in the browser
$_='hello how are you'; if (/hello/){ print "default variable =$_\n"; print "found hello\n"; } # i modifier ignores case difference if (m/HELLO/i){ print "found HELLo\n"; } #match with negation if (! /hallo/){ print "not found hallo\n"; } print "\n\n";
Bind Operator: In the above example we used the default variable $_. You can use any variable for any RE using =~ which is called bind operator.
#bind operator #above example with a variable $line='hello how are you'; print "line=$line\n"; if ($line =~ /hello/){ print "found hello\n"; } print "\n\n";
Matching single character : . (dot) dot matches any single character except new line.
$line="help how are you"; print "line=$line\n"; if ($line=~ /hel.p/){ print "hel.p was found\n"; } else { print "hel.p was not found\n"; } if ($line=~ /he.p/){ print "he.p was found\n"; } else { print "he.p was not found\n"; } print "\n\n";
In the above example you will notice that first if condition fails. This is because there is no character after hel followed by p. The second if condition succeeds because in 'help' he is followed by a character and 'p'. Matching any single character zero or one time - ? Question mark matches any character zero or one time in a string.
$line="help how are you"; print "line=$line\n"; if ($line=~ /hel?p/){ print "hel? was found\n"; } else { print "hel? was not found\n"; } #another example $line="help how are yu"; print "line=$line\n"; if ($line=~ /y?u?/){ print "you? was found\n"; } else { print "you? was not found\n"; } print "\n\n";
$line= "hel\np how are you"; print "line=$line\n"; if ($line=~ /hel\sp/){ print 'hel\s was found'."\n"; } else { print ' hel\s was not found'."\n"; } print "\n\n";
quantifier: zero or more times Quantifiers are symbols that will tell perl to match n number of times a particular pattern. * star is a quantifier to match zero or more times
$line = "hello how are you"; print "line=$line\n"; if ($line=~ /hel*o/){ print 'hel*o found'."\n" ; } else { print 'hel*o not found'."\n"; } $line = "helo how are you"; print "line=$line\n"; if ($line=~ /hel*o/){ print 'helo found'."\n"; } else { print 'hel*o not found'."\n"; } $line = "heo how are you"; print "line=$line\n"; if ($line=~ /hel*o/){ print 'hel*o found '."\n"; } else { print 'hel*o not found'."\n"; } print "\n\n";
Match one or more time - + (plus) plus quantifies one or more time;
$line = "hello how are you"; print "line=$line\n"; if ($line=~ /hel+o/){ print 'hel+o found '."\n"; } else { print 'hel+o not found '."\n";
} $line = "heo how are you"; print "line=$line\n"; if ($line=~ /hel+o/){ print ' hel+o found '."\n"; } else { print 'hel+o not found '."\n"; } print "\n\n";
$line ="hhellohhelloh how are you"; print "line=$line\n"; if ($line=~ /h(hello)+h/){ print '(hello)+ found'."\n"; } else { print '(hello}+ not found '."\n"; } $line ="hellohello how are you"; print "line=$line\n"; if ($line=~ /h(hello)*h/){ print '(hello)* found'."\n"; } else { print '(hello}* not found '."\n"; } print "\n\n";
$line ="vanakkam how are you"; print "line=$line\n"; if ($line=~ /(hello)|(vanakkam)/){ print '(hello)|(vanakkam) found '."\n"; } else { print '(hello)|(vanakkam) not found '."\n"; } print "\n\n";
$line="pc180a07"; print "line=$line\n"; if ($line=~ /pc[0-9]+[a-z]07/){ print 'pc[0-9]+[a-z] occurred'."\n"; } else { print 'pc[0-9]+[a-z] not occurred'."\n"; } $line="pc180b07"; print "line=$line\n"; if ($line=~ /pc[0-9]+[a,b]07/){ print 'pc[0-9]+[a,b] occurred'."\n"; } else { print 'pc[0-9]+[a,b] not occurred'."\n"; }
$line="pcaa0b07"; print "line=$line\n"; if ($line=~ /pc[^0-9]+a/){ print 'pc[^0-9]+ found'."\n"; } else { print 'pc[^0-9]+ not found '."\n"; } print "\n\n";
$line="pc180a07"; print "line=$line\n"; if ($line=~ /pc\d+[a-z]07/){ print 'pc\d+[a-z]07 occurred'."\n"; } else { print 'pc\d+[a-z]07 not occurred'."\n"; }
$line="original string";
print "line=$line\n"; $line=~ tr/r/xx/; print 'after applying tr/r/xx the result is '."\n"."$line\n"; print "\n"; $line="original string"; print "line=$line\n"; $line=~ tr/rin/RIN/; print 'after applying tr/rin/RIN/ the result is '."\n"."$line\n"; print "\n\n";
$line="original string"; print "line=$line\n"; $line=~ s/r/xx/g; print 'after applying s/r/xx the result is '."\n"."$line\n"; print "\n";
Perl has more features than shown above as far as regex is concerned. With what we saw above let us do few exercises.
Exercise
1.Read a file. Count number of words in it. To keep it simple let us define word as a set of characters followed by a space. Thus 'hello how' will be two words and 'hello how .' will be three words.
#! /usr/bin/perl print "Enter File Name "; $filename=<STDIN>; chomp $filename; #First open the file for reading open ($fh,"<",$filename); #read line by line until end of file while ($line=<$fh>){ @words=split ' ',$line; #break lines in words and store in an array. $nw = $nw + scalar(@words); #no.of element in array = no of words. } print "Number of Words in the file : $nw \n"; close ($fh);
Practice : 1a. Modify above program to omit punctuation marks like ., etc and then count 1b. Modify above program without using scalar().
1c. Modify the above exercise to count distinct words 1d. Modify the exercise to count all distinct words whose length is 5 or more 1e. Modify the exercise to accept a word and then count occurrence 1e. Modify the exercise to accept a word and then count occurrence 2. A text file contains following data in a comma separated method 101,ram,manager,20000 102,baskar,ceo,40000 103,ravi,executive,12000 104,arun,executive,10000 It represents employee details in a firm viz. empid,name,designation,salary. Generate an output containing following details Designatio No.of.Employee Total n s Salary ceo 1 40000 22000 20000
executive 2 manager 1
#! /usr/bin/perl use strict; #declare variables my ($fname,$fh); my %desig_salary; #Hash to store summary of salary my %desig_empno; #Hash to store number of employees per designation my @names; #array to store names
my ($line,$empid,$name,$desig,$salary); #variables to store data from file print "enter data file name "; $fname=<STDIN>; chomp($fname); open( $fh,"<",$fname) or die "could not open file "; #open file for reading while ($line=<$fh>){ #read one line ($empid,$name,$desig,$salary)=split /,/,$line; #break line into values #add 1 to no.of.employees for this designation $desig_empno{$desig} = $desig_empno{$desig} + 1; #here explicit addition is used #add salary to desig hash $desig_salary{$desig} += $salary; # Here += style addition is used. Note += is similar to C push @names,$name; } close($fh); #desig_salary has contains a hash whose keys are # designation and values are the sum of salary #iterating on this hash will print first part of our problem printf "%15s\t%15s\t%15s\n",'Designation','No.of.Emp','Total Salary'; foreach my $key (keys %desig_salary){ printf "%15s\t%15d\t%15f\n",$key,$desig_empno{$key},$desig_salary{$key}; } #Second part printing names in alphabetical order print "Name\n"; foreach $name(sort @names){ print "$name\n"; }
3.Accept a host name from the user. Scan through the hosts file and find out the IP number. Ensure that comments are not processed. Normally the hosts will contain ipnumber and hostname one entry per line. Comments will start with # and end at the line. e.g 127.0.0.1 localhost 192.168.1.1 mygateway 192.168.1.2 myserver # This is my server #hosts in another LAN 192.168.2.1 anothergateway
#! /usr/bin/perl print "enter hostname "; $hname=<STDIN>; chomp $hname; #remove newline character open($fh,'<','hosts'); while ($line=<$fh>){ #first remove any comments in the line by
#substituting with null $line=~ s/(\#.*\n)//; #print "line after=$line\n"; ($no,$name)=split ' ',$line; #note split ' '. This is a special #delimiter with a space between single quote,which takes care of multiple #whitespaces. #if hostname is found print the ipno and get out of while loop if ($name eq $hname){ print "hostname=$name IP No.:=$no\n"; last; } } close($fh);
Example 1:
1. List all records in the table and find out average age Let us start the code. (codes are given in italics and bold) Step 1: Connecting to the database use DBI; my $dbh=DBI->;connect('DBI:mysql:database=testdb;host=localhost','test','test123'); Connect requires three arguments : datasource, username,password First argument -datasource gives information about the database server like type of dbms, location etc. In our example datasource is specified as DBI:mysql:database=testdb;host=localhost Here DBI:mysql means use mysql driver. database=testdb means use the database testdb. host=localhost means the host in which the database is running. Other two arguments are username and password which need no explanation. Step 2 Run the select query on the server. First store sql in a variable like this my $query='select * from name '; Then send the sql to the server for parsing and checking my $sth=$dbh->prepare($query) or die "could not prepare $query\n"; In the above statement $dbh is the database connection handle we got while using DBI->connect earlier. $sth is the statement handle returned upon successful preparation.
$query refers to the sql statement. The query can be given directly as string also. Here we do some error checking by using die. The $sth that is returned will be required for any further operation on this query. Now we will run the query on the server $sth->execute(); Note here, we are simply using $sth to run the query. Once we call execute, the server runs the query and keeps the result set ready for retrieval. Step 3 Get results from the server one row at a time. fetchrow_array() is a function that will return one row of data and store result in an array. We will use a while loop to fetch all rows from the server. while (($id,$name,$age)=$sth->fetchrow_array()){ print "id=$id name=$name age=$age\n"; } $sth->fetchrow will return a null when there are no more rows. Thus this loop will run until null. ($id,$name,$age)=$sth->fetchrow_array() is used to equate the rows returned to a set of variables. Step 4 Close the statement handle $sth->finish(); Step 5 Close the database connection $dbh->disconnect(); Here is the output of running the script. id=1 name=RAMAN age=45 id=2 name=RAVI age=35 For the sake convenience I am repeating program listing here.
use DBI;
my $dbh=DBI->connect('DBI:mysql:database=testdb;host=localhost','test','test123'); my $query='select * from name '; my $sth=$dbh->prepare($query) or die "could not prepare $query\n"; $sth->execute(); while (($id,$name,$age)=$sth->fetchrow_array()){ print "id=$id name=$name age=$age\n"; } $sth->finish(); $dbh->disconnect()
Example 2.
Insert a record accepting input from terminal Here is the code to insert a row into name table. Step 1: Establishing connection with database. For explanation see previous example use DBI; my $dbh=DBI->connect('DBI:mysql:database=testdb;host=localhost','test','test123'); Step 2 Accept input from keyboard print 'Enter id:'; $id=<STDIN>; print 'Enter Name:'; $name=<STDIN>; print 'Enter age'; $age=<STDIN>; use chomp to remove any newlines chomp $id; chomp $age; chomp $name;
Explanation : The print statement is simple - just shows message on the screen. $id=<STDIN> means accept value from standard input (by default keyboard) and store the value in the variable $id; Step 3 Create the Sql statement $query=sprintf "insert into name(id,name,age) values(%d,%s,%d)",$id,$dbh->quote($name),$age; Explanation: Here we create a string using sprintf function, by variable substitution. The sprintf in perl is similar to sprintf in C. $dbh->quote() function is used on string values, so that quotes are properly taken care of -e.g names like D'Silva. It is a best practice to use this. At the end of this line the $query variable will have an sql which is an insert statement. Step 4. Run the query on the server and insert data. $dbh->do($query) or die "could not do $query\n"; The sql execution in the previous example was done in three stages namely prepare, execute,fetchrow_array. In the case of insert statement no rows are returned. Hence, we can use do(), which combines all three stages into one. It will return number of rows affected. Step 5. Disconnect from the server $dbh->disconnect(); For the sake of convenience the code is repeated here.
use DBI; my $dbh=DBI->;connect('DBI:mysql:database=testdb;host=localhost','test','test123'); print 'Enter id:'; $id= <STDIN>>; print 'Enter Name:'; $name=<STDIN>; print 'Enter age'; $age=<STDIN>; chomp $id; chomp $age; chomp $name; $query=sprintf "insert into name(id,name,age) values(%d,%s,%d)",$id,$dbh->quote($name),$age; $dbh->do($query) or die "could not do $query\n"; $dbh->disconnect();
As you can see from the above examples dbi is pretty simple. Same code can be used for any database. Only change required will be in connection step. The performance of dbi is very good, and it has lot features like fetching column names, types etc. Exercise 1.Accept a name from the user. Then list all records matching that name. 2. Accept id and delete the record matching this id.