Ok here is the world's simplest Perl module domonstrating all the salient features of exporter.
# I am a very simple module package MyModule; use warnings; use Exporter; our $VERSION = 1.00; our @ISA = qw(Exporter); our @EXPORT = qw(&func1); our @EXPORT_OK = qw(&func2); our %EXPORT_TAGS = (DEFAULT => [qw(&func1)], ALL => [qw(&func1 &func2) +]); sub func1 {return reverse @_} sub func2 {return @_} 1; # here is a very simple script to use this module #!/use/bin/perl -w use strict; my @list = qw (J u s t ~ A n o t h e r ~ P e r l ~ H a c k e r !); # case 1 # use MyModule; # print func1(@list),"\n"; # print func2(@list),"\n"; # case 2 # use MyModule; # print func1(@list),"\n"; # print MyModule::func2(@list),"\n"; # case 3 # use MyModule qw(:DEFAULT); # print func1(@list),"\n"; # print func2(@list),"\n"; # case 4 # use MyModule qw(:ALL); # print func1(@list),"\n"; # print func2(@list),"\n";
We build our module as shown. It exports &func1 by default. It exports &funct2 only is specifically requiested to. It defines two sets of tags. The ':DEFAULT' tag exports only &func1; the ':ALL' tag exports all the functions - all two of them!. We use it as shown. Uncomment the examples to see what happens. In case 1 we get an error as &funct2 has not been exported thus does not exist. In case 2 we are OK as although &func2 was not exported we reference it with its full package name. Case 3 is the same as case 1 as the :DEFAULT tag only exports &func1. Case 4 uses the :ALL tag to export all the functions we defined in the EXPORT_TAGS => ALL so &func1 and &func2 are exported and thus this works.
Note the use statement refers to a *file* that ends with .pm. When we say &MyModule::func2 we are refering to the *package name* not the file name that we used in the use. This allows you to have many package names in one used file. In practice the names are usually the same.
Hope this explains how it works.
cheers
tachyon
s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print
In reply to Re: dripping wet rank perl newbie ?:
by tachyon
in thread How to make a module which exports a function, takes a list and returns a list?
by luma
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |