$self->{_doc} = $ppi_document;
$self->index_locations();
$self->_disable_shebang_fix();
+ $self->{_forced_filename} = $args{'-forced-filename'};
$self->{_is_module} = $self->_determine_is_module(\%args);
return;
sub filename {
my ($self) = @_;
- my $doc = $self->{_doc};
-
- return $doc->can('filename') ? $doc->filename() : undef;
+ if ($self->{_forced_filename}) {
+ return $self->{_forced_filename};
+ }
+ else {
+ my $doc = $self->{_doc};
+ return $doc->can('filename') ? $doc->filename() : undef;
+ }
}
#-----------------------------------------------------------------------------
=over
-=item C<< new(-source => $source_code, '-program-extensions' => [program_extensions]) >>
+=item C<< new(-source => $source_code, '-forced-filename' => $filename, '-program-extensions' => [program_extensions]) >>
Create a new instance referencing a PPI::Document instance. The
C<$source_code> can be the name of a file, a reference to a scalar
containing actual source code, or a L<PPI::Document|PPI::Document> or
L<PPI::Document::File|PPI::Document::File>.
+In the event that C<$source_code> is a reference to a scalar containing
+actual source code or a L<PPI::Document|PPI::Document>, the resulting
+L<Perl::Critic::Document|Perl::Critic::Document> will not have a filename.
+This may cause L<Perl::Critic::Document|Perl::Critic::Document> to incorrectly
+classify the source code as a module or script. To avoid this problem, you
+can optionally set the C<-forced-filename> to force the L<Perl::Critic::Document|Perl::Critic::Document>
+to have a particular C<$filename>. Do not use this option if C<$source_code>
+is already the name of a file, or is a reference to a L<PPI::Document::File|PPI::Document::File>.
+
The '-program-extensions' argument is optional, and is a reference to a list
of strings and/or regular expressions. The strings will be made into regular
expressions matching the end of a file name, and any document whose file name
use Test::Deep;
-use Test::More tests => 41;
+use Test::More tests => 43;
#-----------------------------------------------------------------------------
#-----------------------------------------------------------------------------
+my $nameless_code = 'use strict';
+my $nameless_doc = Perl::Critic::Document->new( -source => \$nameless_code,
+ '-forced-filename' => 'Build.PL' );
+
+is($nameless_doc->filename(), 'Build.PL', 'Got forced filename');
+is($nameless_doc->is_module(), 0, 'Forced name affects module determination');
+
+#-----------------------------------------------------------------------------
+
# ensure we return true if this test is loaded by
# t/08_document.t_without_optional_dependencies.t
1;