Browse files

Various LineCache fixes and cleanups. script_cache and file_cache hashes

are now globals for out-of-package inspection. update_script_cache now
stores an evcal string if it is given.
List.pm: distinguish between a file with no lines and a file not found.
Location.pm: guard against map_file returning undef.
  • Loading branch information...
1 parent b51c3b6 commit e52bb26130ada328b0d07366e81346fb3c9fdf1d Rocky Bernstein committed Jan 7, 2012
View
2 lib/Devel/Trepan/CmdProcessor/Command/List.pm
@@ -227,7 +227,7 @@ sub run($$)
# We now have range information. Do the listing.
my $max_line = DB::LineCache::size($filename);
$filename = DB::LineCache::map_file($filename);
- unless ($max_line) {
+ unless (defined $max_line) {
$proc->errmsg("File \"$filename\" not found.");
return;
}
View
29 lib/Devel/Trepan/CmdProcessor/Location.pm
@@ -1,4 +1,4 @@
-# Copyright (C) 2011 Rocky Bernstein <rocky@cpan.org>
+# Copyright (C) 2011, 2012 Rocky Bernstein <rocky@cpan.org>
use strict;
use Exporter;
use warnings;
@@ -40,7 +40,8 @@ sub canonic_file($$;$)
my $is_eval = DB::LineCache::filename_is_eval($filename);
return $is_eval ? $filename : (basename($filename) || $filename);
} elsif ($resolve) {
- $filename = DB::LineCache::map_file($filename);
+ my $mapped_filename = DB::LineCache::map_file($filename);
+ $filename = $mapped_filename if defined($mapped_filename);
my $is_eval = DB::LineCache::filename_is_eval($filename);
return $is_eval ? $filename : (abs_path($filename) || $filename);
} else {
@@ -77,30 +78,6 @@ sub resolve_file_with_dir($$)
return undef;
}
-# # Get line +line_number+ from file named +filename+. Return "\n"
-# # there was a problem. Leading blanks are stripped off.
-# sub line_at(filename, line_number,
-# opts = {
-# :reload_on_change => @settings[:reload],
-# :output => @settings[:highlight]
-# })
-# # We use linecache first to give precidence to user-remapped
-# # file names
-# line = LineCache::getline(filename, line_number, opts)
-# unless line
-# # Try using search directories (set with command "directory")
-# if filename[0..0] != File::SEPARATOR
-# try_filename = resolve_file_with_dir(filename)
-# if try_filename &&
-# line = LineCache::getline(try_filename, line_number, opts)
-# LineCache::remap_file(filename, try_filename)
-# }
-# }
-# }
-# return nil unless line
-# return line.lstrip.chomp
-# }
-
sub text_at($;$)
{
my ($self, $opts) = @_;
View
55 lib/Devel/Trepan/DB/LineCache.pm
@@ -1,6 +1,6 @@
#!/usr/bin/env perl
#
-# Copyright (C) 2011 Rocky Bernstein <rockb@cpan.org>
+# Copyright (C) 2011, 2012 Rocky Bernstein <rockb@cpan.org>
#
#
=head1 NAME DB::LineCache
@@ -35,11 +35,13 @@ source lines.
use Digest::SHA;
-use version; $VERSION = '0.1.0';
+use version; $VERSION = '0.1.1';
# A package to read and cache lines of a Perl program.
package DB::LineCache;
use English qw( -no_match_vars );
+use vars qw(%file_cache %script_cache);
+
use strict; use warnings;
no warnings 'once';
no warnings 'redefine';
@@ -59,8 +61,6 @@ my $perl_formatter = Devel::Trepan::DB::Colors::setup();
# The file cache. The key is a name as would be given by Perl for
# __FILE__. The value is a LineCacheInfo object.
-my %file_cache;
-my %script_cache;
# Maps a string filename (a String) to a key in %file_cache (a
# String).
@@ -258,7 +258,6 @@ sub getline($$;$)
my $reload_on_change = $opts->{reload_on_change};
my $filename = map_file($file_or_script);
($filename, $line_number) = map_file_line($filename, $line_number);
- ## print "+++FILENAME $filename\n";
my $lines = getlines($filename, $opts);
if (defined $lines && @$lines && $line_number > 0 &&
$line_number <= scalar @$lines) {
@@ -470,7 +469,10 @@ sub update_script_cache($$)
return 0 unless filename_is_eval($script);
my $string = $opts->{string};
my $lines_href = {};
- unless (defined($string)) {
+ if (defined($string)) {
+ my @lines = split(/\n/, $string);
+ $lines_href->{plain} = \@lines;
+ } else {
if ($script eq $DB::filename) {
# Should be the same as the else case,
# but just in case...
@@ -527,6 +529,7 @@ sub update_cache($;$)
}
my $lines_href;
my @trace_nums = ();
+ my $stat;
if ($use_perl_d_file) {
my @list = ($filename);
if ($is_eval) {
@@ -535,7 +538,6 @@ sub update_cache($;$)
}
push @list, $file2file_remap{$path} if exists $file2file_remap{$path};
for my $name (@list) {
- my $stat;
no strict; # Avoid string as ARRAY ref error message
if (scalar @{"main::_<$name"}) {
$stat = File::stat::stat($path);
@@ -582,15 +584,15 @@ sub update_cache($;$)
$read_file = 1;
}
}
-
- my $stat = undef;
+
+ # File based reading is done here.
if (-f $path ) {
- $stat = File::stat::stat($path);
+ $stat = File::stat::stat($path) unless defined $stat;
} elsif (!$read_file) {
if (basename($filename) eq $filename) {
# try looking through the search path.
for my $dirname (@INC) {
- $path = File::Spec::catfile->($dirname, $filename);
+ $path = File::Spec->catfile($dirname, $filename);
if ( -f $path) {
$stat = File::stat::stat($path);
last;
@@ -631,8 +633,22 @@ unless (caller) {
no strict;
print scalar(@{"main::_<$file"}), "\n";
use strict;
+
+ my $script_name = '(eval 234)';
+ update_script_cache($script_name, {string => "now\nis\nthe\ntime"});
+ print join(', ', keys %DB::LineCache::script_cache), "\n";
+ my $lines = $DB::LineCache::script_cache{$script_name}{lines_href}{plain};
+ print join("\n", @{$lines}), "\n";
+ $lines = DB::LineCache::getlines($script_name);
+ printf "%s has %d lines\n", $script_name, scalar @$lines;
+ printf("Line 1 of $script_name is:\n%s\n",
+ DB::LineCache::getline($script_name, 1));
+ my $max_line = DB::LineCache::size($script_name);
+ printf("%s has %d lines via DB::LineCache::size\n",
+ $script_name, scalar @$lines);
+ exit;
- my $lines = DB::LineCache::getlines(__FILE__);
+ $lines = DB::LineCache::getlines(__FILE__);
printf "%s has %d lines\n", __FILE__, scalar @$lines;
my $full_file = abs_path(__FILE__);
$lines = DB::LineCache::getlines(__FILE__);
@@ -674,21 +690,6 @@ unless (caller) {
$DB::filename = '(eval 4)';
my $filename = map_script($DB::filename, "\$x=1;\n\$y=2;\n\$z=3;\n");
print "mapped eval is $filename\n";
-
- # print("#{__FILE__} is %scached." %
- # yes_no(LineCache::cached?(__FILE__)))
- # print "Full path: #{DB::LineCache::path(__FILE__)}"
- # LineCache::checkcache # Check all files in the cache
- # LineCache::clear_file_cache
- # print("#{__FILE__} is now %scached." %
- # yes_no(LineCache::cached?(__FILE__)))
- # digest = SCRIPT_LINES__.select{|k,v| k =~ /digest.rb$/}
- # print digest.first[0] if digest
- # line = LineCache::getline(__FILE__, 7)
- # print "The 7th line is\n#{line}"
- # LineCache::remap_file_lines(__FILE__, 'test2', (10..20), 6)
- # print LineCache::getline('test2', 10)
- # print "Remapped 10th line of test2 is\n#{line}"
}
1;
View
9 t/10test-linecache.t
@@ -11,11 +11,18 @@ BEGIN {
use_ok( 'Devel::Trepan::DB::LineCache' );
}
+note 'Test update_script_cache';
+my $lines = "now\nis\nthe\ntime";
+my $script_name = '(eval 234)';
+DB::LineCache::update_script_cache($script_name, {string => $lines});
+my $ary_ref = $DB::LineCache::script_cache{$script_name}{lines_href}{plain};
+is(join("\n", @$ary_ref), $lines);
+
note 'Test getlines';
my $file=__FILE__;
# my $line_count = scalar(@{"main::_<$file"});
-my $lines = DB::LineCache::getlines(__FILE__);
+$lines = DB::LineCache::getlines(__FILE__);
my $line_count = scalar @$lines;
ok($line_count > __LINE__, "Compare getlines count to __LINE__");

0 comments on commit e52bb26

Please sign in to comment.