Kelly Jones wrote:
Consider:
perl -le '$hash{"foo-bar"} = 1; print $hash{foo-bar}'
[no result]
Using warnings and/or strict may have helped:
$ perl -Mwarnings -le 'my %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 = ("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.
perl -le '$hash{"foobar"} = 1; print $hash{foobar}'
1
$ perl -Mwarnings -le 'my %hash = ("foobar", 1); print $hash{foobar}'
1
$ perl -Mstrict -le 'my %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.
Actually it sets $hash{"foo-bar"} to 1. There is a difference between
the string "foo-bar" and the expression foo-bar.
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?
It is 'foo' - 'bar', unless either foo or bar can be resolved to a
subroutine.
Why is it 0?
In numerical context strings are converted to numbers and if there are
no leading numerical digits in the string they are converted to the
number 0.
$ perl -le'print "1234" - "123"'
1111
$ perl -le'print "1234junk" - "123garbage"'
1111
$ perl -le'print "junk1234" - "garbage123"'
0
The behavior above seems inconsistent to me. Is it considered a bug?
No, that is the defined behaviour.
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/