in reply to To USE or not to USE

A related question. When I do this (as I often do):

use CGI qw/header/; print header;

Am I paying too high a price? Does importing selective functions save any cpu time, or does it just stop the namespace from getting filled?

I suppose that I could just avoid the penalty of CGI and use:

print "Content-type: text/html\n\n";

Would this be a significant advantage for my scripts?

On a related note, if someone could point me to a node on timing the execution of scripts, that would be great, as I a can't seem to find the information I need to try this myself.

</ajdelore>

Replies are listed 'Best First'.
Re: Re: To USE or not to USE
by perrin (Chancellor) on Jul 22, 2003 at 16:31 UTC
    Importing selected functions reduces namespace pollution and memory usage, since aliases do take up space. Of course you could just use the OO interface instead.

    Note that your particular example is actually a counter-example, because CGI prints "Content-Type: text/html; charset=ISO-8859-1" in order to prevent cross-site scripting attacks. This demonstrates how the "just code this part yourself because it's too simple to load a module" attitude can lead to half-assed and broken code if you don't fully understand the problem you're trying to solve.

      I do make a habit of using CGI and other CPAN modules just for that very reason. I have no plans to change this and go re-writing CPAN everytime I write code.

      I was merely asking about the performance issues from a perspective of academic curiousity. As others have pointed out, optimization is only one factor to consider along with readability, safety, development time, etc. when writing code.

      Was the last part of your post directed at me, personally? Or are you making a generalization?

      </ajdelore>

        I was just using your CGI example to make a point about the dangers of the "CPAN modules are too big for me" attitude. I don't know anything about you personally, or your coding habits.

      Another advantage of being explicit in listing what functions you import is that your code is more self-documenting. You can see where each subroutine in your code comes from without having to know the default export lists of the modules you use.

Re: Re: To USE or not to USE
by castaway (Parson) on Jul 23, 2003 at 08:08 UTC
    DProf is good for timing scripts, or just finding where they take their time. Try reading perldebug, it's mentioned there. Basically it's "perl -d:DProf <scriptname>" and then feed the resulting output to dprofpp. Be sure to also read the manpage to dprofpp, for example to get actual time used and not just system time, use dprofpp -r.

    C.