Link to home
Start Free TrialLog in
Avatar of Luxana
LuxanaFlag for Australia

asked on

CGI with PERL - quick start

Hello Experts,


I'm starting with CGI with perl and I need to some hints such as:

1) Write some scripts just for example (I'm not asking you copy and paste scripts here but rather then that help me to create them from beginning)
2) Make them run on my local apache server with debian 3.1 ( apache is runnig now without problem )
3) Some links for goog tutorial pages regarding CGI with perl programming
4) Create small example of connection between my new CGI with some kind of database
5) Recommend some good books for beginners regarding this topic

I got some skills in perl and in some other languages as well such as java,asp,visual basic ... so I will do my best.

thanks for help

Luxana
ASKER CERTIFIED SOLUTION
Avatar of tomaugerdotcom
tomaugerdotcom
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Oh yeah, databases. simple answer: use MySQL and use DBI

#! /usr/bin/per

use DBI; # to access your MySQL database

# connect to your MySQL database (substitute appropriate database name, username and password)
my $dbh = DBI->connect("DBI:mysql:database_name", "username", "password") || die ("Couldn't connect to DB: $!");

# create an SQL query to get at some of your data
my $sth = $dbh->prepare("SELECT * FROM customer WHERE country=? AND province=?");

# run the SQL query
$sth->execute('Canada', 'Ontario');

# work with the resulting data
while (my $rowHash = $sth->fetchrow_hashref) {
  print "first name: $rowHash->{'firstname'}  last name: $rowHash->{'lastname'}";
}

The rest is up to your knowledge of SQL.

Good luck

Tom
Avatar of Luxana

ASKER

tom this is a quick start? :))) this study for my entire life:))

Just joking today I got some books such as :

CGI Prgramming with Perl ( O'Reilly )
CGI Prigramnng 101 Perl for the World Wide WEB ( Hamilton)
Custom CGI Scripting with Perl
Instant CGI/PERL

so it seems I will not need yor help ( hope ) but just in case I'll keep this question open untill I finish my project if you don't mind.

thanks tom

Luxana
Avatar of Luxana

ASKER

HI Tom,

I went throught book and I'm trying to make one of the scripts there using DBI database. This script is address book but I got a problem there with creating query there and I'm not realy clear what I'm doing there. The book wich I'm following have a lot of mistakes so for beginner as I'm it is hell:-) I fixed lots of mistakes by my self using just common sense but my problem is:

Can't call method "prepare" on an undefined value at /usr/lib/cgi-bin/address_book.cgi line 264.

here is part of script and line 264:

my $where_clause;
if ($cgi->param('exactmatch')) {
      $where_clause = join(" and",
                              map ($_
                                    . " = \""
                                    . $criteria{$_} . "\"", (keys %criteria)));
}      else      {
      $where_clause = join(" and",
                              map ($_
                                    . " like \"%"
                                    . $criteria{$_} . "\"", (keys %criteria)));
}
$where_clause =~ /(.*)/;
$where_clause = $1;

$sql = $sql . " where " . $where_clause if ($where_clause);

my $sth = $dbh->prepare($sql)      # here is line 264
      or die "Cannot prepare: " . $dbh->errstr();

no idea what I'm missing there. Thanks for your help if you need more info please let me know pls....

thanks tom

lubo
No problems.

The line my $sth = $dbh->prepare($sql) queries the database using the database handle ($dbh) that you need to set up first. This is usually done somewhere BEFORE this line using something like:

my $dbh = DBI->connect($data_source, $username, $auth);

$data_source is the name of the database (you might need to talk to your server administrator or web service to get the exact information)
$username is the username of the DATABASE user. This is sometimes the same as your logon for your website, but not always. Again, check with you web service provider.
$auth is the password for this username.

so usually the line looks something like:

my $dbh = DBI->connect("DBI:mysql:databaseName", "myUserName", "myPassword");

Replace the stuff in quotes with the appropriate information of course.

And don't forget, near the start of your script, make sure you have

use DBI;

Hope this helps! For more information on the DBI module, check out cpan (www.cpan.org). http://cpan.uwinnipeg.ca/htdocs/DBI/DBI.html


Cheers

Tom
Tom did a great job!
I have nothing else to add - but just to say - tom has set you up for a quick start.
Without that information - you wont go anywhere...
Avatar of Luxana

ASKER

yes I know and he did more than gerat job :) that is why https://www.experts-exchange.com/questions/21157261/points-for-tomaugerdotcom.html
and I would like to keep this discusion here and I know that it is beyoud my quick start question and try to somehow fix my problem with my script:-)

thanks Tom

lubo
Thanks guys. I appreciate it - it makes a big difference when people appreciate a detailed answer.

Best of luck!

Tom