Slash Boxes
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

ferreira (5993)

  (email not shown publicly)

Just another Brazilian Perl hacker.

Journal of ferreira (5993)

Tuesday August 07, 2007
08:41 AM

Another trap: length of a list constant

[ #34027 ]
List constants are weird. The documentation warns you about this. They are lists and not arrays and call for convoluted expressions like the ones below for doing some common tasks.

(WEEKDAYS)[1..5] # to slice

@week_names_in_lojban{(WEEKDAYS)} # to index slices
#BEWARE! @week_names_in_lojban{+WEEKDAYS} gives you a suspicious warning

And so I wanted to take the length of a constant list, and naively tried:

use constant LIST => qw(A B C);

which worked right. But

use constant LIST => qw(A);

give me zero (because that's the result of "'A'+0") and

use constant LIST => ();

ends in zero as well (because it is "undef+0"). Well, you'll see that in a flash IF you have warnings on. (And yes, I stumbled with the strong suggestion to not use constants with more than one value in scalar context, because they return the number of values today, but that may change.) But the end of story is that it seems like you don't have a good expression to compute the length of a generic list constant. A seemingly sensible alternative is using a constant array ref.

use constant LIST_REF => [qw(A)];
@{+LIST_REF}+0 # ugh! but works

I have to admit that using Readonly is much prettier. It is really a win to preserve the type and the visual distinction among scalar/array/hash with the sigil. But hey, I wanted to use that in tests requiring Readonly for writing beautiful test code feels like overkill, but maybe that was what I should do.

use Readonly;
Readonly::Array @LIST = qw(A B C);
#Readonly::Array @LIST = qw(A);
#Readonly::Array @LIST = qw();
scalar @LIST # works alright in all three cases

That reminded me of Ingy's manifesto in Module::Install::Philosophy. Mainly, the phrase that goes similar to this:

I have a dream that one day my modules will not be judged by the number of their prerequisites.

But... I was going to rant about failing tests of a not-so-urgent prerequisite (but Readonly distribution is flawless according to CPAN testers). And what about performance? What about the OS-challenged and ISP-challenged people that can't install hybrid modules that need C compilation? What about [many other things]? Well, excuse me. Too much digression :/

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
More | Login | Reply
Loading... please wait.