Why do Perl variables need to start with $, %,@ (sigils)?

后端 未结 6 1886
陌清茗
陌清茗 2020-12-05 00:47

Why do Perl variables need to start with different characters (sigils)?

  • Scalar variables start with $

  • Hashes start with %

6条回答
  •  一向
    一向 (楼主)
    2020-12-05 01:05

    Several reasons are explained by Larry Wall et al in "Programming Perl":

    Within any given namespace [...] every variable type has its own subnamespace, determined by the funny character. You can, without fear of conflict, use the same name for a scalar variable, an array, or a hash (or, for that matter, a filehandle, a subroutine matter, a label or your pet llama.)

    [...]

    Like most computer languages, Perl has a list of reserved words that it recognizes as special keywords. However, because variable names always start with a funny character, reserved words don't actually conflict with variable names.

提交回复
热议问题