[H-GEN] Re: [H-CHAT] mysql perl problem

Jason Henry Parker jasonp at uq.net.au
Fri Dec 1 18:26:47 EST 2000


[ Humbug *General* list - semi-serious discussions about Humbug and  ]
[ Unix-related topics.  Please observe the list's charter.           ]
[ Worthwhile understanding: http://www.humbug.org.au/netiquette.html ]

Darrin Mison <staeci at yahoo.com> writes:

> [staeci at serra staeci]$ rpm -qa | grep sql
> mysqlgui-static-1.7.1-1
> perl-Msql-Mysql-modules-1.2210-2
> 
> [staeci at serra staeci]$ rpm -qa | grep SQL
> MySQL-shared-3.22.32-1
> MySQL-3.22.32-1
> MySQL-client-3.22.32-1
> MySQL-devel-3.22.32-1

First and foremost, the -i flag to grep makes it behave
case-insensitively.

> #!/usr/bin/perl
> use mysql;
> $dat= Mysql->Connect(127.0.0.1, user, password);

Whoa!

*Always* start perl with warnings on, and ``use strict;''.

> [staeci at serra magic_database]$ ./test_prog.pl
> Can't locate object method "Connect" via package "Mysql" at ./test_db.pl line 3.

This error is reporting that perl is unable to find the method
``Connect'' in the package ``Mysql''.  There are two reasons this
could be happening.  One is that the method ``Connect'' does not exist
in this package.  The other is that the interpreter thinks the
``Mysql'' package doesn't exist.

A quick test on the redhat box:

$ rpm -qa | grep -i sql
postgresql-devel-6.5.3-1
postgresql-jdbc-6.5.3-1
postgresql-6.5.3-1
postgresql-perl-6.5.3-1
postgresql-server-6.5.3-1
MySQL-client-3.23.22-1
MySQL-3.23.22-1
MySQL-devel-3.23.22-1

In the next test, we see that the `mysql' package was not even found.
I don't know why it's being found on your host---have a ferret about
in /usr/lib/perl5, look for a file called `mysql.pm', and find out
which package owns it.

$ perl
use mysql;
Can't locate mysql.pm in @INC (@INC contains:
/usr/lib/perl5/5.00503/i386-linux /usr/lib/perl5/5.00503
/usr/lib/perl5/site_perl/5.005/i386-linux
/usr/lib/perl5/site_perl/5.005 .) at - line 1.
BEGIN failed--compilation aborted at - line 1.

*This* test works just fine---it doesn't have the error you're
reporting, since the Mysql::Connect method is being found (the OO
propellor-heads would probably call this a static method).

$ perl
use Mysql;
$dat= Mysql->Connect(127.0.0.1, user, password);
Mysql->connect(database=user;host=1270.1) failed: Unknown MySQL Server
Host '1270.1' (22) at - line 2

However, there are two more problems.  The first is that the sequence
of characters `127.0.0.1' is being interpreted as a series of string
operations creating the number 1270.1 (if I had a perl interpreter
with -DDEBUGGING, I could be more precise).  What you *want* is the
sequence of characters `` '127.0.0.1' '', which will be
interpreted---correctly---as a string, not a number, or sequence of
string concatenations.

The second is that you're not using any form of lexical scoping.  use
strict; will prevent you from doing this (and quite a lot of other
very silly things).  In total, I think your code should look like
this:

        #!/usr/bin/perl -w
        use strict;

        use Mysql;

        ...

        my $dat = Mysql->Connect('127.0.0.1', $uname, $pass);

        ...

Note the mixed use of a single-quotish string for the IP address, and
no quoting around the scalar variables $uname and $pass.

> Can someone explain what I am doing wrong?  

I think the biggest thing you're doing wrong is you're not using the
extensive resources at your disposal.  Go and read (don't post!) the
comp.lang.perl.* hierarchy of newsgroups available (see
http://deja.com/home_ps.shtml if you need to); read the perl manpage,
then read any of the pages it points to that seem relevant; join a few
mailing lists; or (if you've got the cash) borrow a copy of
/Programming Perl/, or /Learning Perl/.

I've replied back to -general because I think this question is
completely on-topic and this reply interesting enough to warrant it.

jason
-- 
``Banks *are* bastards.'' -- John Laws

--
* This is list (humbug) general handled by majordomo at lists.humbug.org.au .
* Postings to this list are only accepted from subscribed addresses of
* lists 'general' or 'general-post'.



More information about the General mailing list