1

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/site/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. ,

Johan
  • 74,508
  • 24
  • 191
  • 319
Joshwaa
  • 840
  • 1
  • 11
  • 22
  • 1
    You should install CGI::Simple module from the CPAN. – clt60 May 08 '11 at 10:59
  • I'm not going to lie, I have NO idea what I'm doing, I found "CGI::Simple" on a forum somewhere, I originally only had `use Mysql;` but that errored.. – Joshwaa May 08 '11 at 11:02
  • 4
    Tizag's tutorial isn't any good because it instructs to use the Mysql module. You should use [DBI](http://p3rl.org/DBI) and [DBD::mysql](http://p3rl.org/DBD::mysql) instead, and once you have learnt the basics, move to [DBIx::Class](http://p3rl.org/DBIx::Class). – daxim May 08 '11 at 12:45
  • From the [Stack Overflow Perl FAQ](http://stackoverflow.com/questions/tagged/perl?sort=faq): [What's the easiest way to install a missing Perl module?](http://stackoverflow.com/questions/65865/whats-the-easiest-way-to-install-a-missing-perl-module) – daxim May 08 '11 at 12:50

4 Answers4

4

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.

Dave Cross
  • 68,119
  • 3
  • 51
  • 97
1

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

  1. Click Start
  2. Click Run
  3. Type cmd and press enter
  4. Type ppm and press enter
  5. 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.

Johan
  • 74,508
  • 24
  • 191
  • 319
  • Run the same ppm thingy and type `search CGI*` after the `ppm>` prompt to see if module `CGI:Simple` is installed. – Johan May 08 '11 at 11:11
  • What's CGI::Simple used for anyway? I found it on some random forum. It's installed though. – Joshwaa May 08 '11 at 11:12
  • I did google it, found that page, and was completely confused as I'm a Perl beginner. – Joshwaa May 08 '11 at 11:21
  • Aside from code, there is very little there that is Perl specific. – Quentin May 08 '11 at 11:36
  • @joshwaa, back to your problem: search for the file `Simple.pm` on your harddisk. Then ask a question like: `perl: how do I change the include path to include` and insert the location you found in the question. – Johan May 08 '11 at 11:44
  • @Johan: (Referring to your comment that was just deleted as spam/offensive.) It's not just about bluntness. There's a reason that Let Me Google That For You links are *specifically blocked*. You should have figured out something was wrong when you had to use a URL obfuscation service to get around the limitation... – Cody Gray - on strike May 08 '11 at 13:47
  • @Cody Gray, I know, but I did not think it was offensive in this context, I did answer the question and tried to help the OP, so I felt that the link was a correct one. And besides, sometimes knowing the terms to google for are insightful in and of themselves (because you're googling for the wrong terms) and this is where lmgtfy shines. – Johan May 08 '11 at 13:56
  • I don't think "shines" is quite accurate. I understand it is sometimes beneficial to suggest better search terms, but you could have just as well put those in a comment. Most people know how to find the Google site by themselves. It's no biggie; comment removals don't cost you any reps or anything. But I just thought I'd make sure you know. – Cody Gray - on strike May 08 '11 at 13:57
  • Just for the record, I know how to google and I found that page myself, my problem was not understanding it. – Joshwaa May 09 '11 at 19:24
0

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
clt60
  • 62,119
  • 17
  • 107
  • 194
  • I'm on a Windows machine. I have no idea what CGI::Simple is, I just want to be able to query a MySQL database – Joshwaa May 08 '11 at 11:03
0

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.

Joel
  • 3,435
  • 2
  • 23
  • 33