Home > Archive > PERL Beginners > May 2007 > Global varables
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
| Ben Edwards 2007-05-23, 6:59 pm |
| Ime a bit about globals, I know they should be used with care but....
Currently I am using $::var witch seems to work.
'our var' also seems to work.
Cant find any documentation with a discussion of this that answerers
my question totally.
what is the conversion with globals?
Regards,
Ben
--
Ben Edwards - Bristol, UK
If you have a problem emailing me use
http://www.gurtlush.org.uk/profiles.php?uid=4
(email address this email is sent from may be defunct)
| |
| Paul Lalli 2007-05-23, 6:59 pm |
| On May 23, 1:50 pm, funkyt...@gmail.com (Ben Edwards) wrote:
> Ime a bit about globals, I know they should be used with care but....
>
> Currently I am using $::var witch seems to work.
That's brittle. $::var is shorthand for $main::var (that is, the
global variable named $var in the package main), regardless of what
package you're in. If you move your code to a new package, $::var
will suddenly stop working the way you expect.
> 'our var' also seems to work.
>
> Cant find any documentation with a discussion of this that answerers
> my question totally.
So far you've yet to ask a question. . .
> what is the conversion with globals?
If this is the question you're having trouble finding answers to, I'm
not surprised. It's gibberish. What does "the conversion with
globals" mean?
I have no idea what you're asking, but you may be interested in the
"Coping with Scoping" article, written years ago:
http://perl.plover.com/FAQs/Namespaces.html
Paul Lalli
| |
| Rob Dixon 2007-05-23, 6:59 pm |
| Ben Edwards wrote:
>
> Ime a bit about globals, I know they should be used with care
> but....
>
> Currently I am using $::var witch seems to work.
There's no reason to use $::var - it's the same as $main::var so use that
if it's what you mean.
> 'our var' also seems to work.
Work how?
> Cant find any documentation with a discussion of this that answerers
> my question totally.
And that question would be...?
> what is the conversion with globals?
I can't imagine what you mean by this. There is no conversion.
What are you trying to do Ben? It's actually quite unlikely that you need
'global' variables (by which you mean package variables) at all. What can't
you do if you declare everything using 'my'?
Rob
| |
| Rob Dixon 2007-05-23, 6:59 pm |
| Ben Edwards wrote:
> On 23/05/07, Rob Dixon <rob.dixon@350.com> wrote:
>
> Ime changing some very badly written - almost everything is done in
> callbacks. We are slowly re-writing it but as it is a production
> system we dont want to take a bit bang approach yet. This is partly
> cos there is stuff that we probably dont need but I need to get to
> know the perl better to verify this.
>
> The main thing that I think is going to be a global (for now) is the
> database connection.
>
> We have something like
>
> $dbh = DBI->connect....
>
> For now I want to make $dbh available throughout the whole script and
> am using $::dbh, but as you said maybe this should be $main::dbh.
>
> By conventions I mean good practice, if there is such thing for globals;)
>
> Is this any help?
Yes it is, thank you. But there's still no need for package variables unless
you're using multiple packages or multiple source files and need to share the
same data across more than one. Typically a database handle would be declared
lexically and assigned at the start of your program, and a simple
my $dbh = DBI->connect($dsn, $user, $pass);
will allow you to access the handle through the rest of the file.
You may need more than this, however, if your script is spread across multiple
source files.
HTH,
Rob
| |
| John W. Krahn 2007-05-23, 6:59 pm |
| Ben Edwards wrote:
> Ime a bit about globals, I know they should be used with care
> but....
Some of Perl's special variables like $_, $\ etc. are global, and yes they
should be used with care.
perldoc perlvar
> Currently I am using $::var witch seems to work.
$::var is a package variable and has package scope (:: is short for main::).
$ perl -le'
package one;
$::var = "test";
print "one: $var";
package two;
print "two: $var";
package main;
print "main: $var";
'
one:
two:
main: test
> 'our var' also seems to work.
That depends on what you mean by "work"?
> Cant find any documentation with a discussion of this that answerers
> my question totally.
>
> what is the conversion with globals?
This may help:
http://perl.plover.com/FAQs/Namespaces.html
John
| |
| Martin Barth 2007-05-23, 6:59 pm |
| Hi,
seems that there is one msg of you missing...
if you want to know something about good style: perldoc perlstyle.
there you can find some stuff about (package) globals.
if you have a big application maybe you want a data abstraction package
which should containt the database handle?
|
|
|
|
|