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

I need a simple way to know the needed path to a package. Let's say I have a full path file "/my/directory/Dogs/Mooma.pm". The package in this case is "Dogs::Mooma".
I want a simple way to know that @INC should have "/my/directory/" in it and not "/my/directory/Dogs/".
I can open the file, grep for "^package\s+(\S+)", replace :: with / and fix the path I have, but I rather use an existed code, something that is already optimized (I need to do it on thousands of files…).

TIA,
shushu

Replies are listed 'Best First'.
Re: Finding path to a package
by ysth (Canon) on Apr 19, 2004 at 07:25 UTC
    For thousands of files, I don't think you need to optimize; you might consider it for hundreds of thousands. I think the approach you outline is going to be best. You'll probably want to print exceptions to handle manually (or improve your tool) if you can't find "package" or it has a leading path component that doesn't match. I think you want [\w:] instead of \S. Watch out for apostrophes :)
Re: Finding path to a package
by matija (Priest) on Apr 19, 2004 at 07:29 UTC
    1. Find all the unique directory paths from your list.(via sorting or a hash table)
    2. Find all the unique roots (I guesss there might be more than one). (a/b/c, a/b => a/b)
    3. Having vastly diminished the number of directories to check, for each unique root,
      1. open one file in that root,
      2. find it's package declaration,
      3. and parse it to determine what the directory entry should be.
    4. Examine the results for all the unique roots to see if you can merge the results.