Re: How to set up a global variable in a sub-routine?
- From: ArKane <whatever@xxxxxxxxxxxx>
- Date: Fri, 20 Jan 2006 13:16:13 GMT
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@xxxxxxxxxxxxxxx wrote:
>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.
.
- Follow-Ups:
- Re: How to set up a global variable in a sub-routine?
- From: Paul Lalli
- Re: How to set up a global variable in a sub-routine?
- References:
- How to set up a global variable in a sub-routine?
- From: ArKane
- Re: How to set up a global variable in a sub-routine?
- From: usenet
- How to set up a global variable in a sub-routine?
- Prev by Date: Re: keylogger in perl
- Next by Date: Re: How to set up a global variable in a sub-routine?
- Previous by thread: Re: How to set up a global variable in a sub-routine?
- Next by thread: Re: How to set up a global variable in a sub-routine?
- Index(es):
Relevant Pages
|
|