Constants in Perl

Go supports constants of character, string, boolean, and numeric values.

use strict;
use warnings;
use Math::Trig;

# 'constant' pragma declares a constant value.
use constant s => 'constant';

sub main {
    print s . "\n";

    # A 'constant' can be used anywhere a 'variable' can be.
    use constant n => 500_000_000;

    # Constant expressions perform arithmetic with arbitrary precision
    use constant d => 3e20 / n;
    print d . "\n";

    # Numeric constants have no type until given one, such as by an explicit conversion
    print int(d) . "\n";

    # A number can be given a type by using it in a context that requires one,
    # such as a variable assignment or function call.
    # For example, here 'sin' expects a 'float'.
    print sin(n) . "\n";
}

main();

To run the program, save the code in a file called constants.pl and use perl to execute it.

$ perl constants.pl
constant
6e+11
600000000000
-0.284704073237544

Now that you can run and build basic Perl programs, let’s learn more about the language.