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;
In reply to Re: How not to use "our"?
by JavaFan
in thread How not to use "our"?
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |