Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > counting perl src when have nested requires?

Reply
Thread Tools

counting perl src when have nested requires?

 
 
Sherm Pendley
Guest
Posts: n/a
 
      01-23-2005
Ted Johnson wrote:

> I've inherited a perl program and libs which have extremely
> nested series of "require" statements.

....
> Is there any way to --->count the lines of perl src<---
> by inserting some sort of magic statement right after
> the "main()" line in the master file, "foo.pl"?


You can get a list of all the files that have been included with use(),
require(), or do() from the special hash %INC. The keys are the values as
given in your script(s), and the values are the actual paths at which the
files were found.

So, in the simplest case, it could be as simple as this:

system("wc -l " . join(' ', values(%INC)));

Like I said, that's the simple case - obviously, if there are spaces or
other funky stuff in the paths to these files, you'll need to deal with
that by escaping them properly before executing the "wc -l" shell command.
I'll leave that as an exercise for the reader.

sherm--

--
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org
 
Reply With Quote
 
 
 
 
Ted Johnson
Guest
Posts: n/a
 
      01-23-2005
I've inherited a perl program and libs which have extremely
nested series of "require" statements.

For example, in the main file, "foo.pl" we have:
#!/usr/bin/perl
require "lib1.pl";
require "lib2.pl";
require "lib3.pl";
...

# main() routine starts here...


But each of the libs in turn "require" other libs, eg,
"lib1.pl" has:
require "sublib11.pl";
require "sublib22.pl";
require "sublib33.pl";
...

And each sublib in turn "require"s yet more sublibs, eg,
"sublib11.pl" has:
require "sublib111.pl";
require "sublib429.pl";
require "sublib937.pl";

Is there any way to --->count the lines of perl src<---
by inserting some sort of magic statement right after
the "main()" line in the master file, "foo.pl"?

Thanks in advance!
-Ted
 
Reply With Quote
 
 
 
 
Anno Siegel
Guest
Posts: n/a
 
      01-23-2005
Sherm Pendley <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> Ted Johnson wrote:
>
> > I've inherited a perl program and libs which have extremely
> > nested series of "require" statements.

> ...
> > Is there any way to --->count the lines of perl src<---
> > by inserting some sort of magic statement right after
> > the "main()" line in the master file, "foo.pl"?


What "main()" line? Perl isn't C.

> You can get a list of all the files that have been included with use(),
> require(), or do() from the special hash %INC. The keys are the values as
> given in your script(s), and the values are the actual paths at which the
> files were found.
>
> So, in the simplest case, it could be as simple as this:
>
> system("wc -l " . join(' ', values(%INC)));


A solid approach, but it will report all modules loaded anywhere in the
program (up to the point where %INC is evaluated). The OP seems to be
interested in the modules loaded directly or indirectly by one specific
"use" statement.

To make it selective that way (and to add a little magic) I propose
this:

END {
my @report_these; # collect modules to report
sub report_loading {
push @report_these, $_[ 1];
return; # undef, so search continues
}
# Line counting left as an exercise
print "$_ => $INC{ $_}\n" for @report_these;
}

use Some::Stuff;

use lib \ &report_loading;
use Nested::Module;
no lib \ &report_loading;

use Other::Stuff;

The END block will only report what has been loaded between the "use lib ..."
and "no lib ..." statements. More pairs could be added.

This makes use of the fact that you can put a code object on @INC that will
be called when a module is loaded. The feature is described with "require".

Anno
 
Reply With Quote
 
Brian McCauley
Guest
Posts: n/a
 
      01-24-2005
Anno Siegel wrote:

> Sherm Pendley <(E-Mail Removed)> wrote in comp.lang.perl.misc:
>
>>
>>So, in the simplest case, it could be as simple as this:
>>
>> system("wc -l " . join(' ', values(%INC)));

>
>
> A solid approach, but it will report all modules loaded anywhere in the
> program (up to the point where %INC is evaluated). The OP seems to be
> interested in the modules loaded directly or indirectly by one specific
> "use" statement.
>
> To make it selective that way (and to add a little magic) I propose
> this:
>
> END {
> my @report_these; # collect modules to report
> sub report_loading {
> push @report_these, $_[ 1];
> return; # undef, so search continues
> }
> # Line counting left as an exercise
> print "$_ => $INC{ $_}\n" for @report_these;
> }
>
> use Some::Stuff;
>
> use lib \ &report_loading;
> use Nested::Module;
> no lib \ &report_loading;


Er, why not simply compare %INC before and after?

 
Reply With Quote
 
Brian McCauley
Guest
Posts: n/a
 
      01-24-2005
Sherm Pendley wrote:

> Ted Johnson wrote:
>
>>Is there any way to --->count the lines of perl src<---

>
> system("wc -l " . join(' ', values(%INC)));


That, of course counts the total number of lines, not just Perl source
which could be significantly less where the file contains __DATA__ or
__END__. Also it's debatable if lines of POD before the __END__ should
be considered "lines of perl source".

 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      01-24-2005
Brian McCauley <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> Anno Siegel wrote:
>
> > Sherm Pendley <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> >
> >>
> >>So, in the simplest case, it could be as simple as this:
> >>
> >> system("wc -l " . join(' ', values(%INC)));

> >
> >
> > A solid approach, but it will report all modules loaded anywhere in the
> > program (up to the point where %INC is evaluated). The OP seems to be
> > interested in the modules loaded directly or indirectly by one specific
> > "use" statement.
> >
> > To make it selective that way (and to add a little magic) I propose
> > this:
> >
> > END {
> > my @report_these; # collect modules to report
> > sub report_loading {
> > push @report_these, $_[ 1];
> > return; # undef, so search continues
> > }
> > # Line counting left as an exercise
> > print "$_ => $INC{ $_}\n" for @report_these;
> > }
> >
> > use Some::Stuff;
> >
> > use lib \ &report_loading;
> > use Nested::Module;
> > no lib \ &report_loading;

>
> Er, why not simply compare %INC before and after?


Good idea, now you mention it

It's conceptually simpler than making use of the obscure behavior of
coderefs on @INC. It may take a little more code to implement.

Anno
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What does the SRC in <IMG SRC> stand for? pheadxdll HTML 16 06-06-2007 02:45 AM
Convert Java Src to J2ME Src? macbrando@gmail.com Java 1 03-23-2007 01:29 PM
<txt src= ...> equivalent of <img src= ...> Steve Richter ASP .Net 3 02-09-2006 08:44 PM
src.jar (src.zip) missing on Mac OSX Java 1.4.1 installation? Greg Johnson Java 4 09-18-2003 07:21 AM
Problem: Setting MSIE iframe innerHTML change relative href/src to absolute href/src Soren Vejrum Javascript 4 07-05-2003 01:47 PM



Advertisments