Re: How to set up a global variable in a sub-routine?



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.

.



Relevant Pages

  • Re: How to set up a global variable in a sub-routine?
    ... 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 ... So, if you construct a loop with a loop variable, you ... Global variables are generally to be avoided unless ...
    (perl.beginners)
  • Re: Report in FM 6 or 7
    ... achieve this report format will have to be done via scripting. ... but they will simplify the script and Sort Order. ... End Loop ... The normal 'Dancer' field is used so that if there are more than one ...
    (comp.databases.filemaker)
  • Re: recreate database script not work
    ... I got a script which is supposed to regenerate database systax. ... REM gen_dbse_9.sql ... end loop; ...
    (comp.databases.oracle.server)
  • Re: The Doctor And Escherichia Coli
    ... farm will strain off the unusual referendum. ... If you will avoid ... Abdullah's script without brakes, ...
    (rec.arts.poems)
  • Re: Loop Problem (At Least I think)
    ... If there are no images matching the product ... "" Then" statement (the one below the loop, outside of it), is this just ... >> script to finish. ... > 2) Read the database table and acess the dictionary object for the ...
    (microsoft.public.scripting.vbscript)