Chas. Owens wrote:
On Sat, Nov 15, 2008 at 13:52, Kelly Jones <[EMAIL PROTECTED]> wrote:
Consider:

perl -le '$hash{"foo-bar"} = 1; print $hash{foo-bar}'
[no result]

perl -le '$hash{"foobar"} = 1; print $hash{foobar}'
1

I sort of understand this: in the first script, Perl treats foo-bar as
a subtraction, and sets $hash{0} to 1. In the second one it assumes
you just left off some quotes.

My question: since Perl doesn't have constants, what exactly IS
foo-bar? Why is it 0?

The behavior above seems inconsistent to me. Is it considered a bug?

First, Perl does have constants.  See the constant* pragma and the
Readonly** module for more information.  But those are not constants;
they are barewords.  Barewords can be one of three things: an old
style filehandle, a subroutine call, or a string.  The use of the
strict pragma turns off the last of these options (which is one of the
many reasons it should nearly always be used).  The reason the answer
is zero is simple: a string minus a string is zero because a string in
numeric context yields zero and a zero minus a zero is zero.  Things
would have become clearer if you had turned on either  the warnings***
or strict**** pargmas:
                *******
s/pargmas/pragmas/;


:-)

John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order.                            -- Larry Wall

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to