Re: "use strict;" woes
by Ovid (Cardinal) on Oct 14, 2003 at 17:59 UTC
|
It would help if you share the error message with us. In this case, I see that you have the capitalization wrong on $serverDb (should be $serverDB). Change that and the code works fine.
| [reply] |
Re: "use strict;" woes
by davido (Cardinal) on Oct 14, 2003 at 18:38 UTC
|
Because strictures caught the error you made in misspelling $serverDB as $serverDb (a fact that might have gone unnoticed until it really caused a problem if you hadn't used strict), the title of your post seems to be misstated. Rather than "use strict;" woes you probably should have called it, "use strict;" saved me from hours of debugging.
grin
Dave
"If I had my life to do over again, I'd be a plumber." -- Albert Einstein
| [reply] |
Re: "use strict;" woes
by hmerrill (Friar) on Oct 14, 2003 at 21:28 UTC
|
Just a CGI/DBI thought - doing as you have here by hardcoding the database $serverName, $serverDB, $serverUser, and $serverPass right in your CGI script is dangerous. It's fine for testing or just getting your feet wet with a test CGI application that connects to a database, but one way to make it safer is to *NOT* hardcode the database server and user login info actually in the CGI script - instead, put those variables in their own module(Ex: MY_DB.pm) and put the module *outside* the webserver's document root. By doing that, you prevent someone getting access to that database info if they compromise your webserver.
In your CGI script, put a 'use lib' in there where the value is the directory where MY_DB.pm is located. For example, if you put MY_DB.pm in /path/to/safe_dir/MY_DB.pm, then do this in your CGI script:
use lib '/path/to/safe_dir';
use MY_DB;
our $serverName = $MY_DB::serverName;
our $serverDB = $MY_DB::serverDB;
our $db = "DBI:mysql:$serverDB:$serverName";
Creating modules is pretty easy, and there's good documentation on how - just do
perldoc perl
and look for 'perlmod' - there are several perldoc's describing modules, but I think the one you are interested in is 'perlmodlib' since that one is for how to write and use modules. So, to read the perlmodlib perldoc, just do
perldoc perlmodlib
at a command prompt.
HTH. | [reply] [d/l] [select] |
Re: "use strict;" woes
by hardburn (Abbot) on Oct 14, 2003 at 18:00 UTC
|
our $serverDB = "blahblahblah";
. . .
our $db = "DBI:mysql:$serverDb:$serverName";
You didn't capitalize the last 'b' in $serverDb.
BTW--Generally, globals are capitalized to make it clear that they are, in fact, globals. Words are seperated with '_' chars, so your $serverName would become $SERVER_NAME. This idiom would have saved you above.
---- I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
-- Schemer
Note: All code is untested, unless otherwise stated
| [reply] [d/l] [select] |
|
|
BTW--Generally, globals are capitalized to make it clear that they are, in fact, globals. Words are seperated with '_' chars, so your
$serverName would become $SERVER_NAME. This idiom would have saved you above.
I think you are generalizing your personal stylistic preference to a
community preference, which it most assuredly is not.
| [reply] |
|
|
| [reply] |
|
|
|
|
|
|
|
My experience is that if you look at a lot of CPAN code you will find that underscores are more popular than studly caps, package globals are usually Mixed_Case, and constants are usually ALL_CAPS. This is backed up by most of the writings on Perl style. You are of course free to do whatever you like in your code, but I don't think it's a stretch to call this a general preference.
| [reply] |
|
|
|
|
Re: "use strict;" woes
by Zaxo (Archbishop) on Oct 14, 2003 at 18:04 UTC
|
The only thing wrong with your initial attempt is that you've spelled it $serverDB in the declaration and $serverDb in the quoted string.
| [reply] |
Re: "use strict;" woes
by DrHyde (Prior) on Oct 14, 2003 at 18:59 UTC
|
Another bug which I don't think anyone has caught here is ...
print header, "test";
What is 'header'?
Update: oh, yeah, CGI.pm. Oops. In my defence, I don't use CGI.pm to generate HTML :-) | [reply] [d/l] |
|
|
use CGI;
print CGI::header();
# or
my $q = CGI->new;
print $q->header;
| [reply] [d/l] |
|
|
| [reply] [d/l] [select] |
|
|
Unless you are using mod_perl, i really see no excuse to
not use CGI.pm to generate an HTML header. It's
just too easy:
use CGI qw(header); # only loads the header method
print header();
Regardless of what you think about CGI.pm and it's other
HTML generation methods (and i always say "right tool for
the right job" here), i still recommend you use the
header method.
| [reply] [d/l] |