Red Neckerson has asked for the wisdom of the Perl Monks concerning the following question:

I am making my first stab at writing a module. In the package is a subroutine which connects up with my MySQL server, called connect. It is the standard dbi subroutine connecting up to my MySQL. I am wanting to put alot more subroutines into that module, other than just dbi connect, but because I connect and disconnect to the server in my scripts all the time, I would like just to have a module I could just call use mypackage:connect(), so I won't have to do a new subroutine in every perl file I use with mySQL.

I am using Perl DBI to MySQL. I have put my $dbh variables, such as $dbname, $dbhost, etc, just as global variables in the package, sticking out, above all the functions. I also have a disconnect function in the package file. I save the package as a pm file, properly name it, stick a .pm on it, put the 1; at the end of it. I then use the package in another perl file, use mypackage. Sure enough, I can connect to the mySQL from that script, with mypackage::connect(), and using dbi pull out rows from a table I name. Then I can disconnect from that script with mypackage::disconnect().

So far so good. However, when I try to process input from CGI.PM and insert it into the table, after a mypackage::connect() (insert into table (values) etc...., I get an error, 405. I don't know much about diagnostics or tracing errors to figure out what's wrong, but the syntax checks out okay, using -w. The same mySQL insert works fine if I just use a sub connect to the mySQL withtin that script. It just errors when I try to use connection from the module, the mypackage::connect().

Anyone have any ideas why I could access the mySQL database with a connect function inherited from a package, but not insert into the mySQL after it.

Edited 2001-05-16 by Ovid

Replies are listed 'Best First'.
Re: The Use of Use
by Masem (Monsignor) on May 16, 2001 at 19:02 UTC
    Remember that nearly all of the DBI functions can return undef, and provides a nice die message to help you trace errors at the SQL level. EG..
    my $sth = $dbi->prepare( "SELECT * FROM table" ) or die DBI->errstr; $sth->execute( ) or die DBI->errstr;
    Catching that error might help you locate your problem a bit more. I doubt highly it's with the use of modules and the like.
    Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
(Ovid) Re: 405 error ( was: The Use of Use)
by Ovid (Cardinal) on May 16, 2001 at 19:41 UTC

    Your problem probably is not with DBI or your module. You may have issues there, but it's the 405 Error - Method Not Allowed that we're worried about here. Here's the definition of a 405:

    The method specified in the Request Line is not allowed for the resource identified by the request. Please ensure that you have the proper MIME type set up for the resource you are requesting.

    I suspect that this is not a code issue. We'll need more info:

    • What Web server are you running?
    • What operating system?
    • Have you verified that the extension for that filename is recognized by your Web server as executable?
    • Or, does your Web server consider anything in the directory the script is in as executable?
    • Posting some of the code in question can't hurt. But, as I said, I doubt it's the issue.

    My guess is your Web server, for whatever reason, is not recognizing the script as executable.

    Cheers,
    Ovid

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

      Not 405. Sorry. Red------
        If you're getting this error from the web server, then the URL you're using to try to access your script is wrong, your script is in the wrong place, or is otherwise misconfigured. Check the error log of your server for more clues.

        404 is a 'not found' error. This means that the error isn't in your script at all (although your script may indeed have errors). Your script isn't even being run.

        stephen

Re: The Use of Use
by converter (Priest) on May 16, 2001 at 19:33 UTC
    In the future, please consider the use of paragraphs to make your posts more readable.

    Organizing your thoughts into paragraphs allows the reader's eye to scan the text with greater ease, and guides him to your conclusion incrementally while allowing him to digest the "chunks" as he reads.

    As it stands now, I imagine many monks will skip reading your question in favor of others that are more readable.

    Update:

    This comment was posted before Ovid's edit. Yes, I knew the node would probably be edited to make it more readable, but I thought it was worth posting as a reminder.