Home > Archive > PERL Beginners > January 2006 > How to set up a global variable in a sub-routine?
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]
| Author |
How to set up a global variable in a sub-routine?
|
|
| ArKane 2006-01-19, 9:55 pm |
| Hello all,
I'm pretty new to Perl .. up to chapter 13 in the llama book (4th ed).
At the moment, I've got abotu 5-6 scripts that use the same handful of
sub-routines, and it's a bit of pain in the arse to declare the same
variables in each individual script. Would be better if I had an
initializer sub-routine so I could simply call that sub to do all the
work.
So, I'm wondering if it's possible for me to write a script with a
'Main_Init' subroutine who's purpose is to set up a bunch of global
variables for the rest of the program with 'use strict' turned on?
Something like this:
use strict;
print "Entering subroutine ...\n";
&GetNumber();
print "The number is $number\n";
sub GetNumber() {
my $number = 5;
return $number;
}
Obviously, the above doesn't work, but I'm trying to find a way.
| |
| usenet@DavidFilmer.com 2006-01-19, 9:55 pm |
| ArKane wrote:
> So, I'm wondering if it's possible for me to write a script with a
> 'Main_Init' subroutine who's purpose is to set up a bunch of globals
Sure, you can do that, but I'm not sure your reading has brought you up
to the point where you understand some of the more subtle points of WHY
we scope variables. "Scoping" is not quite the same as "declaring"
variables in other languages (where they are generally all declared at
the beginning of the program).
Variables should be narrowly-scoped to only the bit of code that they
are used in. So, if you construct a loop with a loop variable, you
should scope the loop variable only to that loop, such as
foreach my $counter (1..100) { ...
The variable is scoped to that particular block of code (the foreach
loop) and is unknown to the rest of the code. That's what "scoping"
means, and why it's different from a simple declaration as you may find
in other languages.
As a general rule of thumb, you scope a variable only when and where
you need it. Global variables are generally to be avoided unless
there's a GOOD reason (such as a common variable used by many
subroutines, such as a database handle, maybe). Even then, many
programmers avoid global variables completely.
If you simply pre-scope all your variables as globals when you run the
script, you might as well not "use strict" at all, because in that
case, strict is only helping you avoid variable typos, which is only a
fraction of the benefits it provides.
| |
| ArKane 2006-01-20, 6:59 pm |
| Actually, I know what scoping means but in this casse, I'd rather have
the option to use strict in order to avoid typos :) In this particular
case, the 'global variables' are more like constants ... they don't
really change. For example, one of the variables is the location of a
cookie file I am using for web scraping a certain website. Right now,
I've got several scripts (in seperate locations) that access this
cookie file, and I don't really want to combine them if I don't have
to. So, instead of telling perl where the cookie file is in every
single script, I need a way to tell it only once where the file is and
have all the scripts access that variable. I suppose you could
probably do this with objects and/or using a module, but I'm not quite
that far along yet :)
On 19 Jan 2006 19:35:07 -0800, usenet@DavidFilmer.com wrote:
>ArKane wrote:
>
>
>Sure, you can do that, but I'm not sure your reading has brought you up
>to the point where you understand some of the more subtle points of WHY
>we scope variables. "Scoping" is not quite the same as "declaring"
>variables in other languages (where they are generally all declared at
>the beginning of the program).
>
>Variables should be narrowly-scoped to only the bit of code that they
>are used in. So, if you construct a loop with a loop variable, you
>should scope the loop variable only to that loop, such as
>
> foreach my $counter (1..100) { ...
>
>The variable is scoped to that particular block of code (the foreach
>loop) and is unknown to the rest of the code. That's what "scoping"
>means, and why it's different from a simple declaration as you may find
>in other languages.
>
>As a general rule of thumb, you scope a variable only when and where
>you need it. Global variables are generally to be avoided unless
>there's a GOOD reason (such as a common variable used by many
>subroutines, such as a database handle, maybe). Even then, many
>programmers avoid global variables completely.
>
>If you simply pre-scope all your variables as globals when you run the
>script, you might as well not "use strict" at all, because in that
>case, strict is only helping you avoid variable typos, which is only a
>fraction of the benefits it provides.
| |
| Paul Lalli 2006-01-20, 6:59 pm |
| ArKane wrote:
> Actually, I know what scoping means but in this casse, I'd rather have
> the option to use strict in order to avoid typos :) In this particular
> case, the 'global variables' are more like constants ... they don't
> really change. For example, one of the variables is the location of a
> cookie file I am using for web scraping a certain website. Right now,
> I've got several scripts (in seperate locations) that access this
> cookie file, and I don't really want to combine them if I don't have
> to. So, instead of telling perl where the cookie file is in every
> single script, I need a way to tell it only once where the file is and
> have all the scripts access that variable. I suppose you could
> probably do this with objects and/or using a module, but I'm not quite
> that far along yet :)
Get that far along. :-) It's really not that hard....
package MyConfig;
use strict;
use warnings;
sub get_config {
return {
cookie_file => 'path/to/cookie/file.txt',
username => 'john_smith',
# etc ...
};
}
1;
--------------------------
#!/usr/bin/perl
use strict;
use warnings;
use MyConfig;
my $config = MyConfig::get_config();
print "Cookie location: $config->{cookie_file}\n";
__END_
|
|
|
|
|