Search code examples
mysqleclipseperlstrawberry-perl

Strawberry Perl/Eclipse/EPIC - Can't locate MySql.pm in @INC (you may need to install the MySql module)


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

Solution

  • 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.