Skip to content

Commit

Permalink
File names are shortened if the tool is run on files in the current d…
Browse files Browse the repository at this point in the history
…irectory (#7)
  • Loading branch information
glauschwuffel committed Nov 10, 2011
1 parent 53b40c8 commit ad8e45e
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 16 deletions.
60 changes: 49 additions & 11 deletions lib/App/Perlanalyst.pm
Expand Up @@ -106,17 +106,40 @@ sub run {
return $self->analyse() if $self->{analysis};

# No analysis wanted, shall we run a question?
if ( $self->{question} ) {
return $self->_ask_question();
}
return $self->_ask_question() if $self->{question};

# We should never get here. If we do, the logic above is strange
# and we have a bug.
_bug('Strange logic in run()');
}

# stolen from App::Ack
=head2 die ($message)
Exits the program with the given message and a proper error
code for the shell.
=cut

# taken from App::Ack
sub die {
my $program = File::Basename::basename($0);
return CORE::die( $program, ': ', @_, "\n" );
}

=head2 _bug ($message)
This is an internal function that exits the program with
the given message and adds a phrase asking the user to report
this bug.
=cut

sub _bug {
my ($message)=@_;
$message.='. This is a bug. Please report it so we can fix it.';
&die($message); # The ampersand calls the die() in this package.
}

sub analyse {
my ($self) = @_;

Expand Down Expand Up @@ -254,7 +277,7 @@ sub _files {
=cut

sub _display_filename {
my ( $self, $filename ) = @_;
my ( $filename ) = @_;

# remove cwd from the file to make it shorter and readable
my $cwd = getcwd();
Expand All @@ -268,7 +291,11 @@ sub _display_filename {

sub _display_elements_for_file {
my ( $self, $elements, $filename ) = @_;
print $self->_display_filename($filename) . ':' . "\n";

# Don't display the filename if there are no elements.
return unless $elements;

print _display_filename($filename) . ':' . "\n";

for my $element (@$elements) {
print $element->stringify() . "\n";
Expand Down Expand Up @@ -361,7 +388,6 @@ sub _list_modules {

sub _list_filters {
my ($self) = @_;

return $self->_list_modules( 'Filter', 'filters' );
}

Expand All @@ -372,7 +398,6 @@ sub _list_filters {

sub _list_analyses {
my ($self) = @_;

return $self->_list_modules( 'Analysis', 'analyses' );
}

Expand All @@ -383,14 +408,15 @@ sub _list_analyses {

sub _list_questions {
my ($self) = @_;

return $self->_list_modules( 'Question', 'questions' );
}

sub _list_files {
my ($self) = @_;
my $files = $self->_files;
print "$_\n" for @$files;
my @display_names=map {_display_filename($_)} @$files;

print "$_\n" for @display_names;
}

# stolen from App::Ack's get_version_statement
Expand Down Expand Up @@ -426,7 +452,19 @@ END_OF_VERSION
=over 4
=item L<perlanalyst>
=item L<perlanalyst> to see this module in action
=item L<Getopt::Long> for parsing command line arguments
=item L<IO::Interactive> to examine if your program runs interactively
=item L<Module::List> to list installed modules
=item L<Module::Runtime> to load modules at runtime
=item L<Term::ANSIColor> for bringing colour to your terminal
=item L<Term::ProgressBar::Simple> for drawing progress bars
=back
Expand Down
14 changes: 10 additions & 4 deletions scripts/perlanalyst
Expand Up @@ -23,9 +23,6 @@ exit( $app->run ? 0 : 1 );
Perlanalyst is a tool to analyse your Perl documents. This is done via
static analysis, e.g. the code is analysed without running it.
The most simple usage of this tool is to ask a question about the sources
and the tool tells you the answer.
=head1 USAGE EXAMPLES
Before getting into all the gory details, here are some basic usage
Expand All @@ -44,12 +41,17 @@ examples to help get you started.
# the same, but look in another directory
perlananalyst -q Sub::Name=foo ~/perl5/lib/perl5/Test
# see a list of the files that would be examined
perlanalyst --list-files
=head1 FILE SELECTION
The Perlanalyst examines only files that end in C<.pl>, C<.pm> or C<.t> except
if you specify the file names directly on the command line.
It can also list files that would be analysed, without actually searching
them.
=head1 DIRECTORY SELECTION
The program descends through the directory tree of the starting directories
Expand Down Expand Up @@ -82,6 +84,11 @@ List available analyses.
List available filters.
=item B<--list-files>
List files that would be examined. Does nothing else an exits
afterwards.
=item B<--list-questions>
List available questions.
Expand Down Expand Up @@ -159,4 +166,3 @@ L<Perl::Critic> is a different kind of tool. It has the knowledge of experienced
perl programmers built in and tells you if your code smells.
=cut

2 changes: 1 addition & 1 deletion t/files.t
Expand Up @@ -11,7 +11,7 @@ BEGIN {
}

my $dir='t/data';
my @got = sort {$a cmp $b} Perl::Analysis::Static::Files::files($dir);
my @got = sort {$a cmp $b} @{Perl::Analysis::Static::Files::files($dir)};

my @expected = qw(
t/data/lexicals_and_blocks.pl
Expand Down

0 comments on commit ad8e45e

Please sign in to comment.