counting perl src when have nested requires?

Discussion in 'Perl Misc' started by Sherm Pendley, Jan 23, 2005.

  1. 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
     
    Sherm Pendley, Jan 23, 2005
    #1
    1. Advertising

  2. Sherm Pendley

    Ted Johnson Guest

    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
     
    Ted Johnson, Jan 23, 2005
    #2
    1. Advertising

  3. Sherm Pendley

    Anno Siegel Guest

    Sherm Pendley <> 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
     
    Anno Siegel, Jan 23, 2005
    #3
  4. Anno Siegel wrote:

    > Sherm Pendley <> 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?
     
    Brian McCauley, Jan 24, 2005
    #4
  5. 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".
     
    Brian McCauley, Jan 24, 2005
    #5
  6. Sherm Pendley

    Anno Siegel Guest

    Brian McCauley <> wrote in comp.lang.perl.misc:
    > Anno Siegel wrote:
    >
    > > Sherm Pendley <> 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
     
    Anno Siegel, Jan 24, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steve Richter

    <txt src= ...> equivalent of <img src= ...>

    Steve Richter, Feb 8, 2006, in forum: ASP .Net
    Replies:
    3
    Views:
    2,139
    Laurent Bugnion
    Feb 9, 2006
  2. Greg Johnson
    Replies:
    4
    Views:
    3,112
  3. Replies:
    1
    Views:
    1,639
    Alex Hunsley
    Mar 23, 2007
  4. pheadxdll
    Replies:
    16
    Views:
    3,780
    Neredbojias
    Jun 6, 2007
  5. Soren Vejrum
    Replies:
    4
    Views:
    671
    Lasse Reichstein Nielsen
    Jul 5, 2003
Loading...

Share This Page