in reply to Re: Opinion: where Perl5 wasn't attractive for me
in thread Opinion: where Perl5 wasn't attractive for me

I don't know if it is trolling? (Yesterday I read some articles about popularity of Perl and tried to remember which things in Perl took away some of my time and mood; And I think that they could be annoying for other beginners.)

Why did I used $hash{length}? It's because I used $array[length] many times. And usually I suppose (cargo-cult?) that similar construct should behave similar. And I have used $hash{1+1} before, and haven't gained "('1+1' => smth)". So I dislike that bareword exception that you mean.
$_="abc"; $h{1+3}=41; $h{a+3}=42; $h{- length}=43; $h[1+3]=41; $h[a+3]=42; $h[- length]=43; $h[length]=44; $,='|'; print %h,"\n",@h __END__ -length|43|4|41|3|42| |||43|44|41

Replies are listed 'Best First'.
Re^3: Opinion: where Perl5 wasn't attractive for me
by SuicideJunkie (Vicar) on Nov 19, 2014 at 14:59 UTC

    I can't imagine many cases at all where you might want to use length $_ as an index or key, but almost every case where you would want to be able to type a key without having quotes all over the place.

    The simplest way to do what you want is with a leading unary + to make it not be a bareword.

    use strict; use warnings; use Data::Dumper; my %foo; $foo{+length} = 42; $foo{length} = 'interminable'; print Dumper \%foo;
    Gives:
    Use of uninitialized value $_ in hash element at test.pl line 5. $VAR1 = { '' => 42, 'length' => 'interminable' };
      Ok. Thats true. And I used $hash{(length)}++ to save occurences of the substring lengths.
      And this works too: $foo{- -length} = 42;, but '+' of course is better.