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

Is there a way to know how many lines of Perl code perl has compiled up to a point, e.g.:

#!/usr/bin/perl
require Foo;
printf "Perl has compiled a total of %d lines of code after loading Foo\n", __MAGIC__;
require Bar;
printf "Perl has compiled a total of %d lines of code after loading Bar\n", __MAGIC__;

I'm sort of curious as to how "big" (or "heavy") a particular module is. Of course there's the issue of autoloading, XS, eval, etc, but maybe it's fun to know nevertheless.

Replies are listed 'Best First'.
Re: Counting line of code
by JavaFan (Canon) on Jul 21, 2009 at 12:20 UTC
    There's __LINE__ which gives you the line number of the current file (although that can be tricked with the #line directive), but I don't think Perl keeps a total count.

    But I guess you could something in the spirit of:

    my $lines = __LINE__; $lines += (`wc -l $_` =~ /([0-9]+)/)[0] for values %INC;
Re: Counting line of code
by cdarke (Prior) on Jul 21, 2009 at 12:55 UTC
    I query your basic premise. The number of lines of code is a poor indication of how "heavy" a module or program is. It might just be well written.
Re: Counting line of code
by Anonymous Monk on Jul 21, 2009 at 19:20 UTC
    newlines on a language that simple don't care about newlines means nothing, you could have 1 or 10 statements in a line and it would be the same for perl.

    Counting the ";" would be better but if you want a real approach simple benchmark the loading of the module.