Veltro:

As you know, your program breaks because it interferes with the import function provided by Exporter. This leads to your question of how to properly overload the import function.

First I'd ask whether you actually need to overload import. If you just want to do some extra work in your module before anyone uses your code, you can do that at the end of your module, like this:

$ cat Foo.pm package Foo; require Exporter; our @ISA = qw( Exporter ); our @EXPORT_OK = qw( saySomething ); sub saySomething { print "Foo!\n"; } # Special package initialization stuff print "Special FOO.PM initialization complete!\n"; 1; $ cat T.pl #!env perl use lib '.'; use Foo qw( saySomething ); print "Begin\n"; saySomething(); print "End\n"; $ perl t.pl Special FOO.PM initialization complete! Begin Foo! End

The only time I can think of where you would need to overload import is if (a) import() doesn't do something that you need done, and (b) you're going to create other packages that rely on your package which need that extra behavior. If both of these are the case, I expect you'll need to look at the source code to Exporter and see what the import function does and how to not interfere with it. I've not done so, so I don't have much to offer you in that case. I did, however, look over the Exporter docs, though, and found that there's a section "Exporting Without Using Exporter's import Method" that may give you a starting point.

...roboticus

When your only tool is a hammer, all problems look like your thumb.


In reply to Re: Exporter. Correct way to override import? by roboticus
in thread Exporter. Correct way to override import? by Veltro

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.