The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Tie::Math - Hashes which represent mathematical sequences.

SYNOPSIS

  use Tie::Math;
  tie %fibo, 'Tie::Math', sub { f(n) = f(n-1) + f(n-2) },
                          sub { f(0) = 1;  f(1) = 1 };

  # Calculate and print the fifth fibonacci number
  print $fibo{5};

DESCRIPTION

Defines arrays which represent mathematical sequences, such as the fibonacci sequence.

tie
  tie %func, 'Tie::Math', \&function;
  tie %func, 'Tie::Math', \&function, \&initialization;

&function contains the definition of the mathematical function. Use the f() subroutine and $n index variable provided. So to do a simple exponential function represented by "f(n) = n**2":

    tie %exp, 'Tie::Math', sub { f($n) = $n**2 };

&initialization contains any special cases of the function you need to define. In the fibonacci example in the SYNOPSIS you have to define f(0) = 1 and f(1) = 1;

    tie %fibo, 'Tie::Math', sub { f($n) = f($n-1) + f($n-2) },
                            sub { f(0) = 1;  f(1) = 1; };

The &initializaion routine is optional.

Each calculation is "memoized" so that for each element of the array the calculation is only done once.

1 POD Error

The following errors were encountered while parsing the POD:

Around line 48:

=over without closing =back