0

I'm using Eclipse with Strawberry Perl and Eclipse EPIC plugin.

I'm new to perl and am trying to connect to a mysql db I have setup. When I try to use the use MySql; I get an error that:

Can't locate MySql.pm in @INC (you may need to install the MySql module) (@INC contains: C:/strawberry/perl/site/lib C:/strawberry/perl/vendor/lib C:/strawberry/perl/lib .)

I'm trying to do a simple tutorial for a query from: http://www.tizag.com/perlT/perlmysqlquery.php

Doing some googling, it looks like strawberry perl is suppose to have the DBD::mysql already installed.

I'm currently at a loss, perhaps I need to update my path, or eclipse environment or perhaps there is a different use I need to use?

Some details of my setup:

  • Windows 8 OS
  • Strawberry perl v5.18.2
  • Eclipse Kepler Service Release 2
halfer
  • 19,824
  • 17
  • 99
  • 186
James Oravec
  • 19,579
  • 27
  • 94
  • 160
  • 3
    Don't use that tutorial, not only has `MySql.pm` been deprecated for years, the code examples don't even `use strict; use warnings;`! Yuck. Take a look at the documentation for [`DBI`](https://metacpan.org/pod/DBI) instead. A good tutorial on modern Perl practices wouldn't hurt either. – ThisSuitIsBlackNot Sep 16 '14 at 16:15
  • @ThisSuitIsBlackNot, thanks for the info, it was a big help. The link you gave provided me with a bunch of insight. I found a simple example on cpan as well that I was able to get working with DBI ( http://search.cpan.org/~capttofu/DBD-mysql-4.028/lib/DBD/mysql.pm ) thanks! – James Oravec Sep 16 '14 at 16:35
  • Good to hear. I wrote an answer for any other poor saps who may stumble on that tutorial in the future. Fortunately, `Mysql.pm` is unlikely to be on any current systems, so even if someone does try to use the code from the tutorial, it won't compile. – ThisSuitIsBlackNot Sep 16 '14 at 16:44

2 Answers2

3

Yikes! That tutorial is crap. Perhaps at one time it was cutting-edge Perl, but it is way out of date. The module Mysql.pm was deprecated way back in 2006! You should be using DBI instead.

Also, you should include the statements use strict; and use warnings; at the top of every Perl script you ever write. If you find a tutorial that doesn't include those directives in example scripts, stop reading and look for a new tutorial immediately.*

The following shows how to connect to a MySQL database using the DBI module and print out the results of a SELECT statement. See the documentation for details.

use strict;
use warnings;
use 5.010;

use DBI;

my $dsn = "DBI:mysql:test;localhost";
my $user = 'user';
my $pass = 'pass';

my $dbh = DBI->connect($dsn, $user, $pass, {
    PrintError => 0,
    RaiseError => 1
});

my $statement = <<'SQL';
    SELECT foo
    FROM bar
SQL

my $sth = $dbh->prepare($statement);
$sth->execute;

while (my $row = $sth->fetch) {
    say "@$row";
}

$dbh->disconnect;

* Short code snippets don't need these but if an example script includes a shebang line (e.g. #!/usr/bin/perl), it had better have strict and warnings, too.

ThisSuitIsBlackNot
  • 23,492
  • 9
  • 63
  • 110
0

This is a summary based on ThisSuitIsBlackNot's suggest to me. His suggestions was correct, the example I was doing was not using DBD, but instead deprecated code. Reading the link he provided gave me insight into the cpan and a couple more googles gave me an example from cpan that let me connect to my MySQL db without any issues. Here is a link to the example in case anyone else runs into similar issues: http://search.cpan.org/~capttofu/DBD-mysql-4.028/lib/DBD/mysql.pm

James Oravec
  • 19,579
  • 27
  • 94
  • 160