CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Declaring constants

 



Jean
User


Mar 11, 2001, 3:58 AM

Post #1 of 7 (582 views)
Declaring constants Can't Post

I'd like to declare a constant value, i.e. value that can not be changed anywhere in the program.

The only way I've found is using globs, like
*SUCCESS = \1;
if ( $SUCCESS ) { ... }

The problem is this doesn't work with use strict; and my doesn't accept globs ...

Please suggest a workaround to the globs dilemma or any other way to create a const.
Perl has to have something as simple as that, but for some reason I couldn't find that in the books :-(


Jean Spector
QA Engineer @ Extent Technologies, Ltd.
mage@lycosmail.com


rGeoffrey
User / Moderator

Mar 11, 2001, 8:51 AM

Post #2 of 7 (579 views)
Re: Declaring constants [In reply to] Can't Post

One option is to use Locked.pm.

Another is to do something like this...


Code
my $it =&make_constant(3); 

print $it->(), "\n";

sub make_constant
{
my ($value) = @_;
return sub { return ($value); } ;
}

If you do something with $it->() the value will always be the one you set it to with &make_constant(). And it can not be changed later with what I have included here. Unfortunately you have to use the ->() after the variable name to make it work so it is a little ugly. But it will work with strict.

PS. I will not say why this works because that would spoil a future quiz question.

Edited to add link to the quiz question.
--
Sun Sep 9, 2001 - 1:46:40 GMT, a very special second in the epoch. How will you celebrate?

(This post was edited by rGeoffrey on Mar 11, 2001, 9:28 AM)


Jean
User


Mar 11, 2001, 9:05 AM

Post #3 of 7 (577 views)
Re: Declaring constants [In reply to] Can't Post

10x, but doesn't Perl have something SIMPLER, you know, like const in C ?
BTW, you don't have to explain why it works, when you see the code it's actually very simple ;-)



Jean Spector
QA Engineer @ Extent Technologies, Ltd.
mage@lycosmail.com


Kanji
User / Moderator

Mar 11, 2001, 10:13 AM

Post #4 of 7 (576 views)
Re: Declaring constants [In reply to] Can't Post

Why not use the constant module?


Code
    use constant SUCCESS => 1; 
if ( SUCCESS ) { ... }



rGeoffrey
User / Moderator

Mar 11, 2001, 10:55 AM

Post #5 of 7 (576 views)
Re: Declaring constants [In reply to] Can't Post

Sure, find the easy answer.

I have not actually used a constant in my perl programs going on the assumption that a good programer will not change a value that should not change, and being too lazy to take the extra effort to make one.

I was just reminded about my two suggestions yesterday as I was looking for parts to use processing my log files and the only other method I could remember was the one Jean had already had problems with.

Yet another reminder that with perl TIMTOWTDI.

--
Sun Sep 9, 2001 - 1:46:40 GMT, a very special second in the epoch. How will you celebrate?


japhy
Enthusiast

Mar 11, 2001, 3:59 PM

Post #6 of 7 (571 views)
Re: Declaring constants [In reply to] Can't Post

The preferred means is:


Code
use constant FOO => 10;

which also allows you to make constant lists:


Code
use constant KEYS => qw( first last age sex );

But this becomes troublesome when doing:


Code
use constant FIELD => "name"; 

%hash = (
FIELD => 10, # wrong
+FIELD => 10, # right
FIELD() => 10, # right
);

print $hash{FIELD}; # wrong
print $hash{+FIELD}; # right
print $hash{FIELD()}; # right

because of the way barewords are seen in braces, and to the left of =>.

You can create "constant" scalars and keep strict happy:


Code
use strict; 
use vars qw( $FIELD );

*FIELD = \"name";
# now $FIELD is "constant"

This only works for scalars, though, since you can't take a reference to a list, only a list of references to elements in a list. And it's not really constant, since you can modify it again later in the same manner.

You could make your own tie class, but the data could be modified by doing:


Code
my $obj = tied($constant_var); 
$obj->{VALUE} = "new stuff";

So the question is: why do you need a constant? And what type of constant do you need that use constant does not provide?

Jeff "japhy" Pinyan -- accomplished hacker, teacher, lecturer, and author


Jean
User


Mar 14, 2001, 2:21 AM

Post #7 of 7 (557 views)
Re: Declaring constants [In reply to] Can't Post

10x, that basically what I was looking for ;-)

Jean Spector
QA Engineer @ Extent Technologies, Ltd.
mage@lycosmail.com

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives