using constant array

A

A. Farber

Hi,

I have defined a list of constants in my script:

use constant DEFPLATS => qw(ARMI ARM4 THUMB WINS WINSCW);

But the use of it feels a bit clumsy though -
I have to put it in brackets for the hash slices:

@prj_platforms{(DEFPLATS)} = (1) x length DEFPLATS;

and it doesn't interpolate in strings.
Do you have any advices on that? TIA

Regards
Alex
 
B

Ben Morrow

Quoth (e-mail address removed) (A. Farber):
I have defined a list of constants in my script:

use constant DEFPLATS => qw(ARMI ARM4 THUMB WINS WINSCW);

But the use of it feels a bit clumsy though -
I have to put it in brackets for the hash slices:

@prj_platforms{(DEFPLATS)} = (1) x length DEFPLATS;

You don't have to, you just have to make it not a bareword:

@hash{DEFPLATS()}
@hash{+DEFPLATS}
and it doesn't interpolate in strings.

You can interpolate a sub with

my $str = "stuff @{[ DEFPLATS ]} stuff"

However, I think both your problems are better solved by simply using an
array:

my @DEFPLATS = qw/.../;

or, if you need it to be package- rather than lexically-scoped, like the
constant was,

our @DEFPLATS = qw/.../;

If you need it at compile time you can do the slightly clumsy

my @DEFPLATS;
BEGIN { @DEFPLATS = qw/.../ }

or, if you are package-scoping it,

use vars::i '@DEFPLATS' => qw/.../;

Get vars::i from CPAN.

Ben
 
T

Tad McClellan

A. Farber said:
I have defined a list of constants in my script:

use constant DEFPLATS => qw(ARMI ARM4 THUMB WINS WINSCW);

and it doesn't interpolate in strings.


Constants defined using this module cannot be interpolated
into strings like variables.

Do you have any advices on that?


What "that"? It does exactly what it says it will do.

If that isn't want you want, then do something else. <g>


perldoc constant
 
T

Tad McClellan

A. Farber said:
use constant DEFPLATS => qw(ARMI ARM4 THUMB WINS WINSCW);

I have to put it in brackets for the hash slices:

@prj_platforms{(DEFPLATS)} = (1) x length DEFPLATS;


That supplies a list context, which will keep "use warnings" quiet.

You can put the parenthesis elsewhere if you like it better (I do):

@prj_platforms{ DEFPLATS() } = (1) x length DEFPLATS;

(whitespace is your friend if used wisely.)



Also, length() no worky on lists (nor arrays):

perldoc -f length
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,156
Messages
2,570,878
Members
47,413
Latest member
KeiraLight

Latest Threads

Top