cajun has asked for the wisdom of the Perl Monks concerning the following question:

While browsing through Cool Uses for Perl tonight, I ran across something I hadn't thought of before. In backup.pl the author was skipping over comments in the code. He was told other methods to skip over the comments by fellow Monks. Now I can see where it does take a finite amount of time to process comments in the code, but unless there are tons of comments aren't we talking a VERY short period of time ?

If for instance we had a subroutine that got called for every line of a 300MB file and that subroutine had lots of comments, then yes, I can see jumping around them, or moving the comments elsewhere within the code.

Curious what my fellow Monks have to offer.

Replies are listed 'Best First'.
Re: Weeding out comments ?
by MeowChow (Vicar) on Feb 13, 2001 at 13:44 UTC
    In the node you mention, the author was skipping over comments in the configuration file, not the code.

    The Perl interpreter doesn't run through the text of your program directly, but compiles it into an intermediary byte-code representation, which is then executed. Comments are not present in any form in this representation, and thus have zero effect on the speed at which your code is run.

    And unless you write epic novels to describe your subroutines, they have utterly negligable impact on your compilation speed as well :-)

       MeowChow                                   
                   s aamecha.s a..a\u$&owag.print
Re: Weeding out comments ?
by mirod (Canon) on Feb 13, 2001 at 13:47 UTC

    No matter how many times you call a subroutine, it will only be parsed by the Perl interpreter once (unless it is eval'ed for each call). So removing the comments won't help.

    The way the Perl interpreter works is that it goes through your program once, parsing and executing BEGIN blocks and building a parsed version of your code. This parsed version is then executed. This parsed version is a data structure that represents a "closer-to-the-metal" (and optimized) version of the code, and does not include comments. You can see it using B::Deparse.

    In short, if your concern is about speed, you don't have to remove comments yourself, the interpreter does it for you, once per script.