Open van-de-bugger opened 8 years ago
Err... Probably I do not understand something. I supposed that creating a PPI::Document
from file name, e. g.:
my $d = PPI::Document->new( 'assa.pl' );
Creates a PPI document which known its own name, so location of an element can be retrieved with element's methods logical_line_number
, logical_filename
, and others. It seems my assumption was wrong:
$ cat assa.pl
#!/usr/bin/perl
use strict;
use warnings;
use PPI;
my $d = PPI::Document->new( './assa.pl' ); # Second comment.
$d->index_locations; # Third comment.
my $c = $d->find( 'Token::Comment' ) || [];
for my $e ( @$c ) {
printf( "%s at <%s> %d.\n", $e->content, $e->logical_filename, $e->logical_line_number );
};
$ ./assa.pl
Use of uninitialized value in printf at ./assa.pl line 9.
#!/usr/bin/perl
at <> 1.
Use of uninitialized value in printf at ./assa.pl line 9.
# Second comment. at <> 5.
Use of uninitialized value in printf at ./assa.pl line 9.
# Third comment. at <> 6.
logical_filename
always return undef
. It seems the only way to set filename is using #line
directive in source. Why filename is not initialized from actual filename passed to PPI::Document->new
?
Dist::Zilla::Role::PPI
usesPPI
in such a way:Thus,
$document
does not know its filename. This makes good user-friendly error messages hardly possible.For example,
scan_for_prereqs
method of any ofPerl::PrereqScanner::XXX
modules is called with 3 arguments:$self
,$ppi_doc
, and$req
. Scanning the document, the method may operate with tokens and other elements. Every token haslocation
property which contains line, column, charatcter, and filename. However, being called fromDist::Zilla
, filename is always undefined.To solve this problem, I propose
PPI::Document
constructor recognizesfilename
attribute, e. g.:The problem could be fixed in
Dist::Zilla
by prepending file content with#line
directive. However:#line
directive: it is not possible to specify a filename containing double quote.#line
directive should be properly encoded, otherwise it will be silently ignored, but even ignored directive will change linenumbers by 1 because it occupies a line.PPI
users, it would be simpler to specify an argument in constructor than construct proper#line
directive.