You are here:

Perl & CGI/Perl scripting to invoke html

Advertisement


Question
Hi Jason,

I need to fetch data from a particular file and then display it

in html format.Also I need to maintain the same data in a SQL

database. Please guide me with sample example..

Answer
You want to first open the file, and then read it.
I'm not sure what you mean by HTML format, but I assume the file is already in HTML?


open FILE, ">file.txt" or die $!;
   print FILE $str;
close FILE;


You then want to inject the data into a database.

In Perl, use DBI to do this. Here's an example of DBI:

use DBI;
my $dbh = DBI->connect('DBI:Oracle:payroll') or die "Couldn't connect to database: " . DBI->errstr;
my $sth = $dbh->prepare('SELECT * FROM people WHERE lastname = ?') or die "Couldn't prepare statement: " . $dbh->errstr;
print "Enter name> ";
while ($lastname = <>) {         # Read input from the user
  my @data;
  chomp $lastname;
  $sth->execute($lastname)       # Execute the query
     or die "Couldn't execute statement: " . $sth->errstr;
  # Read the matching records and print them out       
  while (@data = $sth->fetchrow_array()) {
     my $firstname = $data[1];
     my $id = $data[2];
     print "\t$id: $firstname $lastname\n";
  }
  if ($sth->rows == 0) {
     print "No names matched `$lastname'.\n\n";
  }
  $sth->finish;
  print "\n";
  print "Enter name> ";
}
$dbh->disconnect;

Perl & CGI

All Answers


Answers by Expert:


Ask Experts

Volunteer


Jason Silver

Expertise

I can answer questions about script installation problems. I can give hints and tips for writing code. I can suggest the best approaches for writing web applications, and other similar topics.

Experience

I've been writing Perl scripts since 1999. I sell my scripts at www.intelliscript.net

Education/Credentials
I'm self taught in computer programming.

Awards and Honors
I've been quoted in a computer programming publication, called CGI-101.

©2016 About.com. All rights reserved.