Kolibrie
Kolibrie

Reputation: 149

Properly Use this Perl Module

I've looked at several examples of using a Perl module and I still can't get it right. I'm following this tutorial: http://www.perlmonks.org/?node_id=102347. For the following .pl and .pm files, I call

$ perl Solver.pl

and have the below error.

Undefined subroutine &main::mergeSort called at Solver.pl line 13.

Solver.pl

#!/usr/bin/perl
#Program usage: perl  PROGRAM
#example: 
#perl solver.pl

use strict;
use warnings;
use MergeSort qw(:DEFAULT);


### MAIN ###

mergeSort(\@list); #ERROR;

### END MAIN ###

MergeSort.pm

  package MergeSort;

    use strict;
    use Exporter;
    use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);

    $VERSION     = 1.00;
    @ISA         = qw(Exporter);
    @EXPORT      = ();
    @EXPORT_OK   = qw(mergeSort);
    %EXPORT_TAGS = ( DEFAULT => [qw(&mergeSort)],
                     Both    => [qw(&mergeSort &merge)]);


    sub mergeSort{
        ...(defined here
    }#end mergeSort()

    sub merge{
        ...(defined here)
    }#end merge()

    1;

Upvotes: 1

Views: 67

Answers (1)

ikegami
ikegami

Reputation: 385496

:DEFAULT has a builtin definition which takes precedence over yours. It exports all the symbols that are exported by default, which is to say all the symbols in @EXPORT. You should have used:

our @EXPORT      = qw( mergeSort );
our @EXPORT_OK   = @EXPORT;
our %EXPORT_TAGS = ( ALL => \@EXPORT_OK );

use MergeSort;   # Same as: use MergeSort qw( :DEFAULT );

But I think explicitly listing one's imports is a good idea, so I'd use

our @EXPORT      = qw( );
our @EXPORT_OK   = qw( mergeSort );
our %EXPORT_TAGS = ( ALL => \@EXPORT_OK );

use MergeSort qw( mergeSort );

Upvotes: 7

Related Questions