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).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:Other suggestions, anyone?
I did try it with CGI.pm this way:open SRC, "$source"; open IDX, ">$source.offset"; binmode IDX; while(<SRC>) { print IDX pack 'N', tell SRC; }
Now, when you want the start of line 2990 (first line at is at offset 0, and its start offset is not stored):use CGI; $source = $INC{'CGI.pm'};
which, for me, printsmy $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>;
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 |