How can I use constants?

B

Bill H

I have an array that I want to reference the different items by a
constant instead of the actual value (quick code snippet example):

x_PLS = 0;
y_PLS = 1;
xscale_PLS = 2;
yscale_PLS = 3;
lock_PLS = 4;
constrain_PLS = 5;
useBW_PLS = 6;
transparency_PLS = 7;
dragx_PLS = 8;
dragy_PLS = 9;
dragwidth_PLS = 10;
dragheight_PLS = 11;
category_PLS = 12;
imagenumber_PLS = 13;
width_PLS = 14;
height_PLS = 15;
caption_PLS = 16;

@photoLayer = ();

.... file the array from a file

$photoLayer[lock_PLS] = 1;

But perl seems to require that I make all my constants variables
($lock_PLS versus lock_PLS). Since these values are constant and will
not change, do I have to do this or am I missing a step?

Background reasoning for wanting to do this, my Flash App uses the
same constants and I want to be able to cut / paste the latest
constant values from the actionscript into the perl code and not have
to maintain 2 different versions of the same file (one with "$" before
the constant name, one without).

Bill H
 
B

Bill H

I have an array that I want to reference the different items by a
constant instead of the actual value (quick code snippet example):

x_PLS = 0;
y_PLS = 1;
xscale_PLS = 2;
yscale_PLS = 3;
lock_PLS = 4;
constrain_PLS = 5;
useBW_PLS = 6;
transparency_PLS = 7;
dragx_PLS = 8;
dragy_PLS = 9;
dragwidth_PLS = 10;
dragheight_PLS = 11;
category_PLS = 12;
imagenumber_PLS = 13;
width_PLS = 14;
height_PLS = 15;
caption_PLS = 16;

@photoLayer = ();

... file the array from a file

$photoLayer[lock_PLS] = 1;

But perl seems to require that I make all my constants variables
($lock_PLS versus lock_PLS). Since these values are constant and will
not change, do I have to do this or am I missing a step?

Background reasoning for wanting to do this, my Flash App uses the
same constants and I want to be able to cut / paste the latest
constant values from the actionscript into the perl code and not have
to maintain 2 different versions of the same file (one with "$" before
the constant name, one without).

Bill H

As a follow up to my own post per perldoc it appears I have to use the
following format to set a constant:

use constant lock_PLS => 1;

If that is the case then I guess my cut / paste method could work, I
would just have to do a few find / replaces afterwards.

Bill H
 
B

Bill H

I have an array that I want to reference the different items by a
constant instead of the actual value (quick code snippet example):
x_PLS = 0;
y_PLS = 1;
xscale_PLS = 2;
yscale_PLS = 3;
lock_PLS = 4;
constrain_PLS = 5;
useBW_PLS = 6;
transparency_PLS = 7;
dragx_PLS = 8;
dragy_PLS = 9;
dragwidth_PLS = 10;
dragheight_PLS = 11;
category_PLS = 12;
imagenumber_PLS = 13;
width_PLS = 14;
height_PLS = 15;
caption_PLS = 16;
@photoLayer = ();
... file the array from a file
$photoLayer[lock_PLS] = 1;
But perl seems to require that I make all my constants variables
($lock_PLS versus lock_PLS). Since these values are constant and will
not change, do I have to do this or am I missing a step?
Background reasoning for wanting to do this, my Flash App uses the
same constants and I want to be able to cut / paste the latest
constant values from the actionscript into the perl code and not have
to maintain 2 different versions of the same file (one with "$" before
the constant name, one without).

As a follow up to my own post per perldoc it appears I have to use the
following format to set a constant:

use constant lock_PLS => 1;

If that is the case then I guess my cut / paste method could work, I
would just have to do a few find / replaces afterwards.

Bill H- Hide quoted text -

- Show quoted text -

Following up my follow up. That don't work. Perl doesn't balk on it,
but it seems it is a null value, since it always brings up the 1st
element in the array (element 0) when I do:

print $photoLayer[lock_PLS];

Unless I am totally missing the boat, it will probably be easier to
just do a search on the "var " in the text I bring in from
actionscript and replace it with a "$" and make them variables.

Think I will go yard sailing.

Bill H
 
S

smallpond

I have an array that I want to reference the different items by a
constant instead of the actual value (quick code snippet example):
x_PLS = 0;
y_PLS = 1;
xscale_PLS = 2;
yscale_PLS = 3;
lock_PLS = 4;
constrain_PLS = 5;
useBW_PLS = 6;
transparency_PLS = 7;
dragx_PLS = 8;
dragy_PLS = 9;
dragwidth_PLS = 10;
dragheight_PLS = 11;
category_PLS = 12;
imagenumber_PLS = 13;
width_PLS = 14;
height_PLS = 15;
caption_PLS = 16;
@photoLayer = ();
... file the array from a file
$photoLayer[lock_PLS] = 1;
But perl seems to require that I make all my constants variables
($lock_PLS versus lock_PLS). Since these values are constant and will
not change, do I have to do this or am I missing a step?
Background reasoning for wanting to do this, my Flash App uses the
same constants and I want to be able to cut / paste the latest
constant values from the actionscript into the perl code and not have
to maintain 2 different versions of the same file (one with "$" before
the constant name, one without).
Bill H
As a follow up to my own post per perldoc it appears I have to use the
following format to set a constant:
use constant lock_PLS => 1;
If that is the case then I guess my cut / paste method could work, I
would just have to do a few find / replaces afterwards.
Bill H- Hide quoted text -
- Show quoted text -

Following up my follow up. That don't work. Perl doesn't balk on it,
but it seems it is a null value, since it always brings up the 1st
element in the array (element 0) when I do:

print $photoLayer[lock_PLS];

Unless I am totally missing the boat, it will probably be easier to
just do a search on the "var " in the text I bring in from
actionscript and replace it with a "$" and make them variables.

Think I will go yard sailing.

Bill H


perl -e '@f=(0, 1, 2, 3); use constant two => 2; print $f[two]'
2

I think your boat must have sailed. Also, you shouldn't be
cutting and pasting, there's a pretty good text processing
language for doing that kind of thing. I'll remember its name
in a second. Maybe you could store the values in a hash
instead and use the names as keys?

--S
 
P

Peter J. Holzer

At said:
use constant lock_PLS => 1;

Following up my follow up. That don't work. Perl doesn't balk on it,
but it seems it is a null value, since it always brings up the 1st
element in the array (element 0) when I do:

print $photoLayer[lock_PLS];

See the docs ("perldoc constant"):

You can get into trouble if you use constants in a context which auto-
matically quotes barewords (as is true for any subroutine call). For
example, you can't say $hash{CONSTANT} because "CONSTANT" will be
interpreted as a string. Use $hash{CONSTANT()} or $hash{+CONSTANT} to
prevent the bareword quoting mechanism from kicking in. Similarly,
since the "=>" operator quotes a bareword immediately to its left, you
have to say "CONSTANT() => 'value'" (or simply use a comma in place of
the big arrow) instead of "CONSTANT => 'value'".

How is this relevant? He is using lock_PLS neither as a hash key nor on
the left side of a fat comma (except int "use constant lock_PLS => 1"
and there he wants lock_PLS to be autoquoted). The docs say nothing
about using a constant as an array index.

hp
 

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

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top