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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.