Showing posts with label cgi. Show all posts
Showing posts with label cgi. Show all posts

Sep 13, 2012

Perl connect to Database

#!c:/perl/bin/perl
  use CGI qw(:all);   
  use HTML::Template;   
  use DBI;   
   
  my $dbh = DBI->connect('dbi:mysql:perltest','root','password')  or die "Connection Error: $DBI::errstr\n";      ####MYSQL
  my $db=DBI->connect("dbi:Oracle:local", "scott", "tiger"); ###Oracle
 
  my $sql = "select * from languages";   
  my $sth = $dbh->prepare($sql) or die "SQL Error: $DBI::errstr\n";   
  $sth->execute();   
   
  my $languages = $sth->fetchall_arrayref(
    {   
     language_name => 1,    
     description   => 1
    }   
  );   
   
=cut
while (@row = $sth->fetchrow_array) {
    print "@row\n";
}
=cut   
   
  print header;   
  my $template = HTML::Template->new( filename => 'template2.tmpl' );   
  $template->param( language => $languages );   
  print $template->output(); 

template2.tmpl
#################
    <html>  <br> 
    <head>  <br> 
    <title>Template 3</title>  <br> 
    </head>  <br> 
    <body>  <br> 
    <table>  <br> 
        <tmpl_if name=language>  <br> 
      <tr>  <br> 
        <th>Language</th>  <br> 
        <th>Description</th>  <br> 
      </tr>  <br> 
      <tmpl_loop name="language">  <br> 
      <tr>  <br> 
        <td><tmpl_var name="language_name"></td>  <br> 
        <td><tmpl_var name="description"></td>  <br> 
      </tr>  <br> 
      </tmpl_loop>  <br> 
        <tmpl_else>  <br> 
      Sorry, no languages were found  <br> 
        </tmpl_if>  <br> 
    </table>  <br> 
    </body>  <br> 
    </html>   

HTML::Template using tmpl_loop

template2.cgi
############
#!c:/perl/bin/perl  
  use CGI qw(:all); 
 
  my $q = CGI->new;
  print $q->header();
     
 my @languages = (   
      {   
          language_name => 'Perl',   
          description   => 'Practical Extraction and Report Language'   
      },   
      {   
          language_name => 'PHP',   
          description   => 'Hypertext Preprocessor'   
      },   
      {   
          language_name => 'ASP',   
          description   => 'Active Server Pages'   
      },   
  );   

  my $template = HTML::Template->new( filename => 'template2.tmpl' );   
  $template->param( language => \@languages );     # Array ref You have to pass => [ {a=>10, b=>20}, {a=>30, b=>40}, {a=>50, b=>60} ]
  print $template->output();    

template2.html
################

<head>   
<title>Template 2</title>   
</head>   
<body>   
<table>   
  <tr>   
    <th>Language</th>   
    <th>Description</th>   
  </tr>   
  <tmpl_loop name="language">   
  <tr>   
    <td><tmpl_var name="language_name"></td>   
    <td><tmpl_var name="description"></td>   
  </tr>   
  </tmpl_loop>   
</table>   
</body>   
</html> 

HTML::Template example 1 in MVC

template1.cgi
############
    #!c:/perl/bin/perl  
      use CGI qw(:all);  
      use HTML::Template;  
      use POSIX;
     
      my $q = CGI->new;
      print $q->header();
     
      my $template = HTML::Template->new(filename => 'template1.tmpl');
      $template->param(day => strftime('%A', localtime()) );
      print $template->output(); 

template1.html
################
<html>  
<head>  
  <title>Template 1</title>  
</head>  
<body>  
Today is <tmpl_var name=day>  
</body>  
</html> 

Apr 27, 2012

CPAN - Perl Modules Repository



   



You can find lots and useful perl modules in the site www.cpan.org
The commonly used modules are :
1) strict
2) warnings
3) Time::Local
4) CGI
5) Data::Dumper
6) Carp etc.,