Search code examples
perlsyntaxhash

What's the history of the + (unary plus) in front of a hashref that disambiguates from a code block?


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?


Solution

  • 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.