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

bear with me here, I'm new to SQL and DBI, and I've never really used "strict" until now. I want to put my database variables (servername, serverdb, serverpass, etc) as global variables at the top of my main script. so i have a setup like this:

#!/usr/bin/perl -Tw use strict; use CGI ":all"; our $serverName = "blahblahblah"; our $serverDB = "blahblahblah"; our $serverUser = "blahblahblah"; our $serverPass = "blahblahblah"; our $db = "DBI:mysql:$serverDb:$serverName"; print header, "test";

but it of course gives an error, only where i try and put a variable in the string. So I try and concatinate it as such:

our $db = "DBI:mysql:".$serverDb.":".$serverName;
but that too returns an error. In fact, the only way i could get all of this to work is if I did the following:
my $x = "DBI:mysql:"; my $y = ":"; our $db = $x.$serverDb.$y.$serverName;
I dont want to slap every petty string into a variable if I want to do something like this, but what else can I do? EDIT: My god, I'm a moron. Thanks, guys *sigh*

Replies are listed 'Best First'.
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.

    Cheers,
    Ovid

    New address of my CGI Course.

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

      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.

        If I'm guilty of it, then so is Larry Wall (yes, perlstyle specifically addresses this point).

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

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

    After Compline,
    Zaxo

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 :-)

      header is not a bug; it's one of the functions exported by CGI when using ":all" or ":standard". If you don't export it, you either call it with a fully qualified name or as a method.

      use CGI; print CGI::header(); # or my $q = CGI->new; print $q->header;

      Cheers,
      Ovid

      New address of my CGI Course.

      That one is OK--header() is imported with use CGI qw(:all);.

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

      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.

      jeffa

      L-LL-L--L-LL-L--L-LL-L--
      -R--R-RR-R--R-RR-R--R-RR
      B--B--B--B--B--B--B--B--
      H---H---H---H---H---H---
      (the triplet paradiddle with high-hat)