Why do you need $ when accessing array and hash elements in Perl?

后端 未结 9 1912
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-11 16:22

Since arrays and hashes can only contain scalars in Perl, why do you have to use the $ to tell the interpreter that the value is a scalar when accessing array or hash elemen

9条回答
  •  半阙折子戏
    2020-12-11 16:54

    I can think of one way that

    $x = myarray[1];
    

    is ambiguous - what if you wanted a array called m?

    $x = m[1];
    

    How can you tell that apart from a regex match?

    In other words, the syntax is there to help the Perl interpreter, well, interpret!

提交回复
热议问题