Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does a plus sign mean in a hash?

Tags:

constants

perl

I have the following piece of Perl code, but I can't understand what its doing.

use constant ANIMAL => 'rabbit'; 
if ($self->{+ANIMAL}) {
    # Do something here
}

What does the + sign before the constant ANIMAL mean?

like image 363
Nosrettap Avatar asked Jan 16 '14 21:01

Nosrettap


1 Answers

From perldoc constant:

You can get into trouble if you use constants in a context which automatically quotes barewords (as is true for any subroutine call). For example, you can't say $hash{CONSTANT} because CONSTANT will be interpreted as a string. Use $hash{CONSTANT()} or $hash{+CONSTANT} to prevent the bareword quoting mechanism from kicking in. Similarly, since the => operator quotes a bareword immediately to its left, you have to say CONSTANT() => 'value' (or simply use a comma in place of the big arrow) instead of CONSTANT => 'value'.

like image 120
Denis Ibaev Avatar answered Sep 19 '22 18:09

Denis Ibaev