Export - Import



In order to eliminate the need to use the full name of
a subroutine e.g. Calc::add() we can export it from the module
and the user can import it into the namespace of her own code.

(we call it A::Calc as we already had a Calc.pm in the previous slide)


examples/modules/calca.pl
#!/usr/bin/perl
use strict;
use warnings;

use lib 'examples/modules';

use A::Calc;

print add(2, 3), "\n";

examples/modules/A/Calc.pm
package A::Calc;
use strict;
use warnings;

use Exporter qw(import);

our @EXPORT = qw(add multiply);

my $base = 10;

sub add {
    validate_parameters(@_);

    my $total = 0;
    $total += $_ for (@_);
    return $total;
}

sub multiply {
}

sub validate_parameters {
    die 'Not all of them are numbers'
        if  grep {/\D/} @_;
    return 1;
}

1;

Exporter is a standard module that provides the 'import' function called by
'use Module' automatically.

This imports automatically every function (and variable) mentioned in the
@EXPORT array. This is nice as the user (the script writer) does not have
to think much.

On the other hand it might be bad as the user gets many functions she does not
need, just because the module author thinks these functions are useful.