in reply to Filepointer of source file inside source filter

One yucky way to do this, would be to read the original source file for the N lines until where the use load is located (because I can know the line number from the call stak, but I would need the byte offst).

Other suggestions, anyone?

Eh... create an index file, next to the original source file, mapping the line number to the index? It's simple enough — error checking has been dropped, as this is just an example, and error checking distracts from the code that actually matters:
open SRC, "$source"; open IDX, ">$source.offset"; binmode IDX; while(<SRC>) { print IDX pack 'N', tell SRC; }
I did try it with CGI.pm this way:
use CGI; $source = $INC{'CGI.pm'};
Now, when you want the start of line 2990 (first line at is at offset 0, and its start offset is not stored):
my $line = 2990; open IDX, "$source.offset"; binmode IDX; seek IDX, 4 * ($line - 2), 0; read IDX, my($packed), 4; $offset = unpack 'N', $packed; open SRC, "$source"; seek SRC, $offset, 0; print "Line $line is:\n", scalar <SRC>;
which, for me, prints
Line 2990 is:
# Globals and stubs for other packages that we use.
This is indeed the line #2990 for my CGI.pm ($CGI::VERSION='2.752').

Note that this is Windows, and I did not bother to use binmode() on the source file. You must be consistent: either use binmode() both on creating the index file and seeking in the source file, or for neither.

My idea is that you have a make-like mechanism, much like Inline, and compare the modification dates on the index file and the module source file, and recreate the index file if it doesn't exist, or is out of date.

Replies are listed 'Best First'.
Re: Re: Filepointer of source file inside source filter
by liz (Monsignor) on Oct 05, 2003 at 13:05 UTC
    Hmmm... I guess I should have mentioned I want to do all of this at compile time without the need of any external file store, because otherwise I might as well use AutoLoader ;-).

    I guess it will come down to reading the source file until the invoking line is encountered and recording that file pointer. That shouldn't be too bad as the load.pm source filter should be the first for that module, and consequently is located somewhere near the beginning of the source.

    Wish someone would have a better idea, though (apart from hacking it into Perl itself, which might be an option).

    Liz