Perl MySQL Problem
I'm a complete Perl noob, I'm a PHP programmer and I'm trying to learn Perl, currently using Tizag's tutorial.
I am running it on a Windows Xampp Install located at c:\xampp - I have this code, which is giving me an error.use strict; use warnings;
use CGI::Simple;
use DBI;
$host = "localhost";
$database = "store";
$tablename = "inventory";
$user = "root";
$pw = "";
$connect = Mysql->connect($host, $database, $user, $pw);
@databases = $connect->listdbs;
foreach $database (@databases) {
print "$database<br />";
}
The error:
Can't locate CGI/Simple.pm in @INC (@INC contains: C:/xampp/perl/si开发者_C百科te/lib/ C:/xampp/perl/lib C:/xampp/perl/site/lib C:/xampp/apache) at C:/xampp/htdocs/testing/learn_perl/index.pl line 5. BEGIN failed--compilation aborted at C:/xampp/htdocs/testing/learn_perl/index.pl line 5. ,
You don't need CGI::Simple for any of the code that you have shown us. If you are planning to turn your program into a CGI application at a later date, then I recommend that you use CGI rather than CGI::Simple. One of the most obvious advantages of CGI is that it is included with the standard Perl installation so you don't need to install it yourself.
But that's just the start of your problems. It looks like you've found a terrible tutorial. You would probably do better using the resources linked to from learn.perl.org.
Copy paste from: http://startperl.blogspot.com/2007/11/perl-module-how-to-install-perl-module.html
There is a ppm utility provided with every active state perl installation, atleast I got it with perl Vesion 5.8.X All you have to do is follow the below steps
- Click Start
- Click Run
- Type cmd and press enter
- Type ppm and press enter
- Once you get the ppm utility prompt
like ppm> type install modulename
eg. install CGI::Simple
The ppm utility will carry the installation of the perl module for you.
You should install CGI::Simple module from the CPAN.
One easy way how to install modules from the CPAN is:
curl -L http://cpanmin.us | perl - --sudo App::cpanminus
and after it you can install modules with simple:
cpanm Some::Module
e.g.
cpanm CGI::Simple
Ouch, that tutorial does not look very good. Try this instead, and let me explain what it does:
#!c:\strawberry\perl\bin\perl.exe -T
use strict;
use warnings;
use CGI;
use CGI::Carp qw(fatalsToBrowser);
use DBI;
my $host = 'localhost';
my $database = 'store';
my $tablename = 'inventory';
my $user = 'root';
my $pw = '';
my $dbh = DBI->connect("DBI:mysql:$database;host=$host",
$user, $pw, { RaiseError => 1 })
or die "Unable to open database: $DBI::errstr";
my $stmt = $dbh->prepare('SHOW DATABASES');
$stmt->execute();
my $databases = $stmt->fetchall_arrayref();
$dbh->disconnect();
my $page = new CGI;
print $page->header();
print $page->start_html();
foreach $database (@$databases) {
print $database->[0] . '<br />';
}
print $page->end_html();
The first line is a standard script header to let the web server know what binary to run. In this case, I added the '-T' flag to turn on taint mode in Perl. This will help for security.
You did add 'use strict' and 'use warnings', very good. Get into the practice of always having these at the top of your scripts.
The next three use statements are libraries from CPAN that will be included in this script. In this case I've used CGI for HTML generation, CGI::Carp for outputing error messages to the browser (this can help with debugging), and DBI for using databases. If you ever get the error message you've posted ('Can't locate ...') then that means you're trying to use a library that you haven't installed from CPAN yet. You can look at this document to get help on installing libraries.
Next I setup local variables. Since I've turned on strict mode above, every new variable must be prefixed by 'my' or 'our', depending on the scope that I want.
Now I connect to the database using the DBI module. Here I'm using the mysql library, so make sure that DBD::mysql is installed. I've also passed an optional parameter called 'RaiseError'; this will ensure that any SQL errors will cause an error. If I did not turn this on I would have to check the return variable for any SQL call I made. I've also decided to terminate the script using the 'die' operation if there is any issues with connecting. You may want to change this to a redirect for production; anything after the 'or' keyword will be executed on errors.
Then I perform my actual SQL operations. Using my database handle, I prepare a statement and execute it. I then retreive all of my results into a array reference for later use. Then I disconnect from the database.
The last block actually prints out the HTML. I create a new CGI object, then print out the header. I start the HTML, and then print out each database found. In the foreach statement, I need to let Perl know that $databases is actually an array reference, so I include the @ symbol before the variable. Then, since each row is another array reference, I get the first column using the '->[0]' operator. Finally I finish off the HTML.
精彩评论