PDA

View Full Version : Defining global variables


Carl_FK
10-22-2007, 11:39 AM
Hi All,

I'm rather a newb at Perl so you'll have to bear with me here!
What I'm trying to do should be very simple for the average Perl programmer...

In the BEGIN:{ } section of my script, I have:

$dbh = Mysql->connect("localhost", "xxxx", "xxxx", "xxxx");
$dbh->selectdb("xxxxx");

I'd like to make $dbh a global variable so I can access it from within my sub { } functions so I can do the following:

$execute = $dbh->query("SELECT * FROM table");

I've tried putting our $dbh; at the top of my script, along with putting it within each sub { } and various other places. I don't really know what I'm doing so some help would be greatly appreciated!

Note: yes I have Google searched and read manuals, for the life of me I seem to be finding it rather hard to make the transition from other languages :p

Thanks in advance.

shyam
10-22-2007, 12:21 PM
our $dbh;
# and later refer to it as
main::$dbh

Carl_FK
10-22-2007, 12:49 PM
Thanks for the reply shyam,
I've just tried that in my script:


our $dbh;

BEGIN:
{
our $dbh;
$dbh = Mysql->connect("localhost", "xxxx", "xxxx", "xxxx");
$dbh->selectdb("xxxxx");
}

sub my_test
{
print " + testing mysql query... ";
$execute = main::$dbh->query("SELECT * FROM table");
print $execute . " - done.\n";
}


But I get this error:
Bad name after main::

Any ideas?

FishMonger
10-22-2007, 07:37 PM
Thanks for the reply shyam,
I've just tried that in my script:


our $dbh;

BEGIN:
{
our $dbh;
$dbh = Mysql->connect("localhost", "xxxx", "xxxx", "xxxx");
$dbh->selectdb("xxxxx");
}

sub my_test
{
print " + testing mysql query... ";
$execute = main::$dbh->query("SELECT * FROM table");
print $execute . " - done.\n";
}


But I get this error:
Bad name after main::

Any ideas?

Which are you trying to write, Perl or PHP script? I ask because you're intermixing the syntax (most of what you have is php syntax).

BEGIN is a reserved keyword used to start a block of runtime code that you want executed at compile time. Your use of the colon between the keyword and the opening brace is not allowed.

The proper method of declaring a lexical variable that has file scope is to use the my keyword instead of our and declare the var near the beginning of the script outside of any other block.

e.g.#!/usr/bin/perl

use warnings;
use strict;
use DBI;

my $dbh = DBI->connect($data_source, $username, $auth, \%attr);

sub my_test {
my $sth = $dbh->prepare("SELECT * FROM table");
$sth->execute;

while(my @row = $sth->fetchrow-array) {
# process each row as needed
}
}

KevinADC
10-22-2007, 11:27 PM
The Mysql module is considered obsolete.

FishMonger
10-22-2007, 11:54 PM
The Mysql module is considered obsolete.

lol, I forgot all about that module. It appears that PHP modeled their mysql calls after that module.

Carl_FK
10-23-2007, 01:47 PM
Thanks for all your help guys, most appreciated.

bettytech
11-05-2007, 12:17 PM
Making Variables Global
When you first wrote $x in your code, you created a (package) global variable. It is visible everywhere in your program, although if used in a package other than the package in which it was declared (main:: by default), then it must be referred to with its fully qualified name, unless you have imported this variable with import(). This will work only if you do not use strict pragma; but it's important to use this pragma if you want to run your scripts under mod_perl.

Making Variables Global With strict Pragma On

First you use :


use strict;

Then you use:

use vars qw($scalar %hash @array);

This declares the named variables as package globals in the current package. They may be referred to within the same file and package with their unqualified names; and in different files/packages with their fully qualified names.
With perl5.6 you can use the our operator instead:


our ($scalar, %hash, @array);