On Jan 27, Jenda Krynicky said: >> That kills the constant-ness of them. They have to be seen as >> barewords for the compile-time replacement of the bareword with its >> constant value to take effect. > >Whoops. I did not know that. I even did not believe you so I >benchmarked it and you are right. > >That's silly :-(
It's part of the prototype debacle. (If you already know this, skip; otherwise, it's an interesting read!) Prototypes are a compile-time feature. Consider this code: $avg = average(@numbers); sub average (\@) { my $list = shift; my $avg = 0; $avg += for @$list; return $avg / @$list; } When Perl creates the opcodes for calling average(@numbers), it checks to see whether there is a prototype for the function. However, these opcodes are generated when Perl sees the function call, which is BEFORE Perl sees the function's definition! Since the function has not be defined yet, Perl does not know of any prototype for it, so nothing "magic" happens. If you have warnings on, then when Perl sees the definition of average(), and realizes you've called it already, it'll bark at you: "foo() called too early for prototype" or somesuch. If you define the function beforehand, or at least declare it: sub average (\@); then when you call it, Perl will know to work its prototype magic on the argument list. Prototypes can be avoided by prepending a & to the function call -- that's just the way it goes. That way, you can say: $avg = &average([<FILE>]); if you really wanted to. I don't condone this, personally. Now, what are 'constants' in Perl? Well, the 'use constant' pragma basically creates a function with an empty prototype. What does that mean for Perl? Well, Perl treats such functions specially -- if it sees a function with an empty prototype (that is not the same as NO prototype), then it examines the function body to see if it returns a constant (known) value. If it does, then it replaces all instances of that function call with the literal return value. So: use constant FOO => 100; becomes BEGIN { require constant; constant->import(FOO => 100); } which ends up creating sub FOO () { 100 } at compile-time. Then, everywhere Perl sees use of the FOO bareword (where it is not followed by a =>, or inside {}'s like a hash key) it will expand it to its value. &FOO is NOT a bareword. Moreso, &FOO supercedes the empty prototype. Thus, accessing constants via &CONST is slower than, and not the same process as, accessing it via CONST (or +CONST or CONST()). -- Jeff "japhy" Pinyan [EMAIL PROTECTED] http://www.pobox.com/~japhy/ RPI Acacia brother #734 http://www.perlmonks.org/ http://www.cpan.org/ ** Look for "Regular Expressions in Perl" published by Manning, in 2002 ** <stu> what does y/// stand for? <tenderpuss> why, yansliterate of course. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]