From:   "Stanisław T. Findeisen"
        <sf181...@students.mimuw.edu.pl>
> Chas. Owens wrote:
> > SOME_CONSTANT is being interpreted as the string "SOME_CONSTANT".
>
> Why is it so? This is crazy.

Because most often you want it that way. Most often you do want the
$hash{BLAHBLAH} to mean $hash{'BLAHBLAH'} and not have to put quotes
around the hash keys. Same thing in

%hash = (
        somekey => 87754,
        otherkey => 71816,
);

> > This is one of the drawbacks to the constant pragma.  Change the code
> > hash keys to one of these and it will work the way you want it to:
> >
> > $hash{+SOME_CONSTANT} #unary plus
>
> What is this unary plus?

It's a "noop" = "no operation", that prevents the parser from using
the rule that says that if the contents of the {} look like word,
they should be treated as if the word was quoted.

> Why aren't there constants in the language itself?? This is crazy.

Define "in the language itself".

Constants in the form of inlined function were in the language for
ages, the "constant" pragma is a relatively new invention that does
little more than the original

sub SOME_CONSTANT () { 'value 1' }

such functions are inlined into the place they are called from during
the optimization phase of compilation. This means that the optimizer
may continue with the optimization, evaluate expressions

        sub PI () {3.1415927}
        $O = 2*PI*$r;         ==>   $O = 6.2831854*$r;

remove unreachable code:

   use constant DEBUG => 0;
   print Dumper($dataStructure) if DEBUG; ==>  <nothing at all>

etc.

> Also: is that true that Perl's specification is its implementation by
> Larry Wall? This is even more crazy. :-/
> A language and its implementation are (should be) 2 different things!

The implementation is open sourced and ported to prettymuch anywhere.
It had even been forked temporarily at times. And the specification
is not so much the implementation, but rather its tests. And there's
plenty of them. Isn't it great to have an executable specification?
One you can test easily? Instead of immersing yourself into a bunch
of papers and attempting to convince yourself that what you
implemented matches the specification, you run the test suite and
KNOW.

Besides ... the implementation was not written by Larry. Maybe back
then when it was Perl 1, but the current Perl is written and
maintained by a lot more people.

> Gunnar Hjalmarsson wrote:
> > So, what behavior exactly is it that you consider weird? What output had 
> > you expected?
>
> I'd expect equal hash keys to map to equal values.
> I'm a conservatist. :-)

Not really. You expected $hash{BLAH} and $hash{'BLAH'} to mean
different things. They don't. They are parsed exactly the same.

Jenda
===== je...@krynicky.cz === http://Jenda.Krynicky.cz =====
When it comes to wine, women and song, wizards are allowed
to get drunk and croon as much as they like.
        -- Terry Pratchett in Sourcery


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to