Sep 29, 2015, 1:15 PM
Post #3 of 5
Re: [regex2012] Trying to understand our versus my
[In reply to]
there is no reason at all to use our in the script above. And, in general, there is usually no reason to use our in a standalone script. Even if you want (and really need) your variable to be "global" within your standalone script, you would still be advised in most cases to declare it with my near the top of your script and outside any block.
In theory, there are some very rare exceptions to this (for example if you want to localize a variable, it has to be a package global, but I have yet to see a practical case where this is really useful, or in the event you wanted to use symbolic references, but they are deprecated and you should most probably not do it), and, since you ask the question, your level of comprehension of these matters is such that you can simply avoid our variables altogether in stand-alone scripts.
You would possibly need our when you start to write modules, especially if you want some of the module variables to be accessible from the calling program or possibly from some other module, since lexical variables (declared with my) in a module cannot be accessed from a program in another file (the scope of lexicals is limited at most to the file in which they are declared). Even then, it is IMHO often better to provide access through a subroutine or a method, but it is sometimes easier to use a few package globals. Also, there are some magical constructs requiring the use of package globals.
In brief, our variables are relatively seldom used (except for some common boiler plate code at the top of modules), and the case outlined above of accessing to variables from another compilation unit (e.g. accessing a module variable from the calling program) is essentially the only one where you would really need our variables (unless you start to write highly magical stuff, but then you most probably don't need my advice).