"use constant X=>(1,2);" or "use constant X=>[1,2];"?

T

Tad J McClellan

Victor Porton said:
What's better?

use constant X=>(1,2);

or

use constant X=>[1,2];


They do not do the same thing.

So then the question becomes which of those 2 things is the one
that you want to do?

Use whichever that one is.
 
T

TonyV

What's better?

use constant X=>(1,2);

or

use constant X=>[1,2];

Honestly? The best practices I've read say that the best of all is:
my @X = (1, 2);

In the first form X=>(1,2), you'll have to make sure X is in
parentheses to get at the values:

my $foo_1 = (X)[0];
my $foo_2 = (X)[1];

In the second form, you'll have to use the arrow operator to get at
the values:

my $foo_1 = X->[0];
my $foo_2 = X->[1];

If you just declare it as a normal variable, people won't wonder what
your code is trying to do, as it will appear as the standard:
my $foo_1 = $X[0];
my $foo_2 = $X[1];

Maybe not the most ideal answer, but I think it's probably the most
clear one (and thus the "best" one) to use.
 
J

J. Gleixner

TonyV said:
What's better?

use constant X=>(1,2);

or

use constant X=>[1,2];

Honestly? The best practices I've read say that the best of all is:
my @X = (1, 2);

TonyV. Honestly, first you should read

perldoc constant

so you understand what it provides, before you comment
on what's the 'best'.
 
T

TonyV

TonyV. Honestly, first you should read

perldoc constant

so you understand what it provides, before you comment
on what's the 'best'.

Actually, what I read a long time ago was O'Reilly's "Perl Best
Practices," section 4.5: Use named constants, but don't use
"constant". (I.e. the constant pragma.) It follows with several good
reasons why using the constant pragma is generally not as good an idea
as other things, such as constants not being able to be interpolated,
constants being difficult to use where bareword strings are accepted,
and constants being package-scoped instead of lexically-scoped, which
can cause weirdities if you don't know what you're doing.

The book actually recommends using the Readonly module instead, but
generally, the easiest thing by far to do, in my opinion, is to just
not use constants.

Obviously, you're free to disagree and use constants to your heart's
content, but I'm not just making this up out of nowhere, and I have
read the documentation. I just happen to agree with O'Reilly's best
practices book on this one and think that constants in Perl are more
trouble than they're worth, at least through version 5.8, which is
what I use.
 
M

Michele Dondi

Actually, what I read a long time ago was O'Reilly's "Perl Best
Practices," section 4.5: Use named constants, but don't use
"constant". (I.e. the constant pragma.) It follows with several good

Actually, many experienced and knowledgeable hackers beg to differ
with some PBP item or another: after all it's just a list of
suggestions. While some of the points they make are perfectly fine,
constant.pm *does* have its use.


Michele
 
T

TonyV

Actually, many experienced and knowledgeable hackers beg to differ
with some PBP item or another: after all it's just a list of
suggestions. While some of the points they make are perfectly fine,
constant.pm *does* have its use.

I never said that PBP is The Way It Must Be Done(tm). All I said was
that there are compelling reasons to NOT use the constant pragma;
reasons that, in my opinion, outweigh the convenience and semantic
purity of using it unless there's just some particular reason that you
HAVE to have it.

And since it was implied that I haven't read the perldoc about
constants, I wanted to make sure he understood that this isn't just
one loony developer in a sea of more experienced people, it's actually
something that at least some gurus deem worthy enough to be a "Best
Practice."
 

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
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top