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

> 7) It was suprise for me that when I used "$hash{length}" it interpreted (length $_) as "length";

I was shocked to read that, till ...

DB<24> $h{length}=3 => 3 DB<25> \%h => { length => 3 }

... I realized the behaviour is sane (autoquoting of barewords) and that you want it reversed to DWIM magic which is easily avoided with little explicit syntax.

just use parens if you really want to avoid an explicit $_

DB<33> $_="abc" => "abc" DB<34> $h{length()}=42 => 42 DB<35> \%h => { 3 => 42, length => 3 } DB<36> $_="abcd" => "abcd" DB<37> $h{(length)}=666 => 666 DB<38> \%h => { 3 => 42, 4 => 666, length => 3 }

In this contrived case ( length as a hashkey? ) I'd personally reject anything other than

DB<39> $h{length $_}= 'best' => "best" DB<40> \%h => { 3 => 42, 4 => "best", length => 3 }

in code review.

Question Are you possibly unconciously trolling???

Opinion I doubt that ... but keep on trying to entertain us.

Cheers Rolf

(addicted to the Perl Programming Language and ☆☆☆☆ :)

Replies are listed 'Best First'.
Re^2: Opinion: where Perl5 wasn't attractive for me
by rsFalse (Chaplain) on Nov 19, 2014 at 14:14 UTC
    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

      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.