in reply to How not to use "our"?
How can I improve my code to achieve a similar behaviour i.e. to be able access a common variable but without resorting to globals?You don't. A variable either is lexical, or it can be accessed from elsewhere, and hence it's a global.
I've read in a number of places that we rarely have to declare variables with "our" and it's usually a bad idea.If you read statements like that without context, discard them. "A little knowledge is worse than no knowledge". If it did come with context, then read and remember the context as well.
Your example is an excellent case for 'our'.
What is, IMO, bad is your use of string literals. I would write it as:
Then, if you mistype 'tinytext' in your main code, you get a compile time error, instead of a run time uninitialized error. Using string literals as hash keys in the way you're doing is like coding without lexical variables, and warnings turned off.use strict; use Exporter qw(import); our @EXPORT = qw($tinytext $text $mediumtext $longtext); our $tinytext = 100; our $text = 200; our $mediumtext = 500; our $longtext = 2000;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: How not to use "our"?
by PeterPeiGuo (Hermit) on Nov 29, 2010 at 14:58 UTC | |
by JavaFan (Canon) on Nov 29, 2010 at 15:44 UTC | |
|
Re^2: How not to use "our"?
by Anonymous Monk on Nov 30, 2010 at 10:06 UTC | |
by BrowserUk (Patriarch) on Nov 30, 2010 at 10:23 UTC | |
by Anonymous Monk on Nov 30, 2010 at 10:46 UTC | |
by BrowserUk (Patriarch) on Nov 30, 2010 at 11:02 UTC | |
by Anonymous Monk on Nov 30, 2010 at 14:32 UTC | |
| |
by JavaFan (Canon) on Nov 30, 2010 at 15:03 UTC | |
by Anonymous Monk on Nov 30, 2010 at 16:01 UTC | |
by JavaFan (Canon) on Nov 30, 2010 at 16:39 UTC |