What's the history of the + in front of a hash

2019-02-23 01:21发布

I would like to read a little more about the + that is usually put in front of a hashref that helps to disambiguate from a code block.

  • When was it first introduced?

  • Who first introduced it (recommended it)?

  • How did people go around the issue before this was introduced?

  • Any trivia or notes that comes to mind while using this syntax?

标签: perl syntax hash
1条回答
爷的心禁止访问
2楼-- · 2019-02-23 01:50

The specific case of disambiguating a codeblock from the anonymous hashref constructor can't be older than the anonymous hashref constructor, which was added with Perl 5.0 back in 1993-4. Before then the problem didn't exist.

But the "unary plus" has been around for longer -- since Perl 4 at least (it wasn't there in Perl 1 but it could have been added any time in the intervening few years, as far as my knowledge goes). It's always done the same thing, forcing its RHS to be evaluated as a term rather than anything else that might make sense in context, and distinguishing, for example:

print (1 + 2), 42; # Does nothing useful with 42!

from

print +(1 + 2), 42; # Prints 3 and 42.
查看更多
登录 后发表回答