--- Carl Franks <[EMAIL PROTECTED]> wrote:
> I have a seperate config file in which I define all of my variables.
> (because I'm making a program using several scripts with many common vars)
> 
> I was under the impression that by initiating the variable (even in a
> seperate file) 'use strict' would be happy.
> However, when I run the script, I get lots of warnings about undeclared
> variables.
> Do I have to declare the variables before using them, even though they are
> already defined, or is there something else I should know?
> (code below)
> 
> Thanks,
> Carl

Carl,

Usually (but not always), you want to predeclare these variables with the "my" 
keyword.  Also,
"param" is a CGI method, not a DBI method.

Your script below doesn't declare $var1, even if you do have it in a file you require. 
 One simple
way of accomplishing what you want is to put your configuration data in a file with a 
hash
structure:

    {
        var1 => "This is the first var",
        var2 => "This is the second var"
    }

Then, in your actual program, you 'do' the file:

    #!/usr/bin/perl -wT
    use strict;
    use CGI;
    my $q = new CGI;

    my $config = do ('config');

    if ( $config->{'var1'} == $q->param('user') ){
        &whatever
    }

The 'config' file has a hashref and using 'do' in this manner will evaluate the 
contents of the
file and assign them to the scalar $config.  See 'perldoc -f do' for more information.

I think that method is a bit cleaner because you're not using global variables (which 
are usually
a bad idea).  However, if you want to keep using your method, you need to simply 
declare those
variables in the required file with "use vars":

    #!/usr/bin/perl -wT
    use strict;
    use vars qw/ $var1 $var2 /;
    require "config";

    use CGI;
    my $q = new CGI; # <- note the use of 'my'

    if ( $var1 == $q->param('user') ){
        &whatever
    }

For a better understanding of "use vars", read 
http://www.perlmonks.org/index.pl?node_id=105446. 
This node explains the difference between "our" and "my", but I also cover "use vars" 
and it
should give you a good idea of what is going on.

Cheers,
Curtis "Ovid" Poe

=====
Senior Programmer
Onsite! Technology (http://www.onsitetech.com/)
"Ovid" on http://www.perlmonks.org/

__________________________________________________
Do You Yahoo!?
Check out Yahoo! Shopping and Yahoo! Auctions for all of
your unique holiday gifts! Buy at http://shopping.yahoo.com
or bid at http://auctions.yahoo.com

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to