Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Imported subroutines

by rpike (Scribe)
on Sep 16, 2013 at 15:44 UTC ( #1054305=perlquestion: print w/replies, xml ) Need Help??
rpike has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to find ways to speed up my perl web app. I was wondering how exactly @EXPORT and @EXPORT_OK worked when it comes to importing subroutines? I want 3 out of 10 subroutines out of a module - if adding all functions to @EXPORT or to @EXPORT_OK will that slow down processing? Compiling? Thanks in advance for any helpful info regarding this.

Replies are listed 'Best First'.
Re: Imported subroutines
by daxim (Chaplain) on Sep 16, 2013 at 15:55 UTC
    will that slow down processing?
    Yes, it will be unnoticably slower (when import is called, that would be at compile time most of the time) than not exporting.

    However, that's not where the slow part of your program is. Instead use a profiler such as Devel::NYTProf to find out where the real problems are, and optimise those.

Re: Imported subroutines
by davido (Cardinal) on Sep 16, 2013 at 16:13 UTC

    You can look for yourself at the code for Exporter, and see that it's unlikely that adding a few items to the EXPORT list will impact your code's performance meaningfully. If you're facing performance issues, you'll want to look elsewhere. And as daxim said, you should profile rather than take wild guesses.

    Where there may be some benefit is when using modules that autoload or eval subroutines into existence at import time. But even with those sorts of modules, there probably just isn't enough going on to worry about unless your profiling demonstrates a need to investigate this corner of your code base further.

    There are still advantages to exporting/importing only what you need. The most oft-cited one is avoiding namespace pollution in the calling code. Another advantage from the perspective of the calling code is that by specifying exactly which subroutines to import on the use line, the calling code becomes more self-documenting.


Re: Imported subroutines
by Laurent_R (Canon) on Sep 16, 2013 at 21:50 UTC

    I may be wrong, but my understanding is that when you use or require a module, Perl is compiling the whole module anyway, including the subroutines which are not exported, so that the only difference between exporting or not exporting a function (putting its name in @EXPORT or @EXPORT_OK) would really be updating the program namespace, which probably is not measurable. The fact that you don't export a subroutine does not mean that you don't use it, it only means that it is not intended to be used directly by the main program, but it may very well be used internally by other functions of the same module. Is this view wrong? Am I missing something?

      No, I think that you are exactly correct.   The module will be compiled, regardless, if not done already.   Then, any name in @EXPORT will be added to the global namespace.   If you provide a list of routines in your use statement, they must be in @EXPORT_OK.

      Nothing will lead to any sort of performance problem.   Everything goes through a one-time “compile (on the fly)” process and, once done, never happens again (AFAIK) for the duration of the run.   If there is a performance issue in this app, “red herring ... look elsewhere.”

      I almost never export anything from modules I write. I almost always:

      use Wibble qw();

      when I use modules. I means I have to:


      when I need to call Wobble(), but at least I know where Wobble() lives that way even minutes, or months, after I wrote the code.

      Using OO largely avoids the name space pollution problem that these measures seek to avoid without cluttering the code with explicit name space references.

      True laziness is hard work

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1054305]
Front-paged by Arunbear
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (8)
As of 2018-10-19 11:59 GMT
Find Nodes?
    Voting Booth?
    When I need money for a bigger acquisition, I usually ...

    Results (107 votes). Check out past polls.