You can then use these constants throughout your code, and their values cannot be changed.use constant PI => 3.14159; # name in uppercase without sigil $ use constant GREETING => "Hello, world!";# name in uppercase without sigil $ print "The value of PI is: " . PI . "\n"; print GREETING . "\n";
Constants can be redeclared. Use the following pragma to prevent mistakes: you get a warning if you redeclare a constant:use constant PHI => 1.62; PHI++; # Error message: Can't modify constant item in scalar assignment at ...
Notice that sometimes constants are declared with an asterisk and a reference to a value:use warnings;
*PHI = \1.62; # The same as: use constant PHI => 1.62;