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?
snip

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:

perl -Mwarnings -le '$hash{"foo-bar"} = 1; print $hash{foo-bar}'
Unquoted string "foo" may clash with future reserved word at -e line 1.
Argument "bar" isn't numeric in subtraction (-) at -e line 1.
Argument "foo" isn't numeric in subtraction (-) at -e line 1.
Use of uninitialized value in print at -e line 1.

perl -Mstrict -le 'my %hash; $hash{"foo-bar"} = 1; print $hash{foo-bar}'
Bareword "foo" not allowed while "strict subs" in use at -e line 1.
Bareword "bar" not allowed while "strict subs" in use at -e line 1.
Execution of -e aborted due to compilation errors.

* http://perldoc.perl.org/constant.html
** http://search.cpan.org/dist/Readonly/Readonly.pm
*** http://perldoc.perl.org/warnings.html
**** http://perldoc.perl.org/strict.html

-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

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


Reply via email to