Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

actual filename and filename attribute #182

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions lib/PPI/Document.pm
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,9 @@ sub new {
Carp::croak("API CHANGE: Source code should only be passed to PPI::Document->new as a SCALAR reference");
}

# Save the filename
$attr{filename} ||= $source;

# When loading from a filename, use the caching layer if it exists.
if ( $CACHE ) {
my $file_contents = PPI::Util::_slurp( $source );
Expand Down Expand Up @@ -274,6 +277,7 @@ sub load {
sub _setattr {
my ($class, $document, %attr) = @_;
$document->{readonly} = !! $attr{readonly};
$document->{filename} = $attr{filename};
return $document;
}

Expand Down Expand Up @@ -339,6 +343,19 @@ sub get_cache {

=pod

=head2 filename

The C<filename> accessor returns the name of the file in which the document
is stored.

=cut

sub filename {
$_[0]->{filename};
}

=pod

=head2 readonly

The C<readonly> attribute indicates if the document is intended to be
Expand Down
14 changes: 0 additions & 14 deletions lib/PPI/Document/File.pm
Original file line number Diff line number Diff line change
Expand Up @@ -70,23 +70,9 @@ sub new {
die "PPI::Document::File SUPER call returned an object of the wrong type";
}

# Save the filename
$self->{filename} = $filename;

$self;
}

=head2 filename

The C<filename> accessor returns the name of the file in which the document
is stored.

=cut

sub filename {
$_[0]->{filename};
}

=pod

=head2 save
Expand Down
69 changes: 69 additions & 0 deletions t/29_logical_filename.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#!/usr/bin/perl

# Testing of PPI::Element->logical_filename

use strict;
BEGIN {
no warnings 'once';
$| = 1;
$PPI::XS_DISABLE = 1;
$PPI::Lexer::X_TOKENIZER ||= $ENV{X_TOKENIZER};
}

use Test::More tests => 21;
use Test::NoWarnings;
use File::Spec::Functions ':ALL';
use PPI::Document;
use PPI::Document::File;
use PPI::Util ();


for my $class ( qw{ PPI::Document PPI::Document::File } ) {

#####################################################################
# Actual filename is used until #line directive

SCOPE: {
my $file = catfile('t', 'data', 'filename.pl');
ok( -f $file, "$class, test file" );

my $doc = $class->new( $file );
my $items = $doc->find( 'Token::Quote' );
is( @$items + 0, 2, "$class, number of items" );
is( $items->[ 0 ]->logical_filename, "$file", "$class, filename" );
is( $items->[ 1 ]->logical_filename, "moo.pl", "$class, filename" );
}

#####################################################################
# filename attribute overrides actual filename

SCOPE: {
my $file = catfile('t', 'data', 'filename.pl');
ok( -f $file, "$class, test file" );

my $doc = $class->new( $file, filename => 'assa.pl' );
my $items = $doc->find( 'Token::Quote' );
is( @$items + 0, 2, "$class, number of items" );
my $str = $items->[ 0 ];
is( $items->[ 0 ]->logical_filename, "assa.pl", "$class, filename" );
is( $items->[ 1 ]->logical_filename, "moo.pl", "$class, filename" );
}

}

#####################################################################
# filename attribute works for strings too

SCOPE: {
my $class = 'PPI::Document';
my $file = catfile('t', 'data', 'filename.pl');
ok( -f $file, "$class, test file" );
my $text = PPI::Util::_slurp( $file );

my $doc = $class->new( $text, filename => 'tadam.pl' );
my $items = $doc->find( 'Token::Quote' );
is( @$items + 0, 2, "$class, number of items" );
my $str = $items->[ 0 ];
is( $items->[ 0 ]->logical_filename, "tadam.pl", "$class, filename" );
is( $items->[ 1 ]->logical_filename, "moo.pl", "$class, filename" );
}
10 changes: 10 additions & 0 deletions t/data/filename.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/perl

if ( 1 ) {
print "Hello World!\n";
}

#line 1000 moo.pl
print "Goodbye Blue Sky\n";

1;