App-perlimports

 view release on metacpan or  search on metacpan

lib/App/perlimports/Document.pm  view on Meta::CPAN

# becomes:
#
# {
#     Carp => undef,
#     'Data::Dumper' => ['Dumper'],
#     POSIX => [],
# }
#
# In lint mode, it never changes.  In edit mode, it starts out as a list of
# original imports, but with each include that gets processed, this list gets
# updated. We do this so that we can keep track of what previous modules
# are really importing, avoiding duplicate imports.

sub _build_found_imports {
    my $self = shift;

    # We're missing requires which could be followed by an import.
    my $found = $self->ppi_document->find(
        sub {
            $_[1]->isa('PPI::Statement::Include')
                && !$_[1]->pragma     # no pragmas

lib/App/perlimports/Document.pm  view on Meta::CPAN

=item found_imports

A hashref catalog of symbols imported from each package by a use statement,
e.g.

  { Carp => ['croak', ..], ... }

In lint mode, this attribute is never altered.

In edit mode, when L<tidied_document> is called, with each include that gets
processed, this list gets updated to what we think it should be.  We do this
so that we can keep track of what previous modules are really importing, to
avoid duplicate imports (same symbol name from different packages).

=back

=head1 AUTHOR

Olaf Alders <olaf@wundercounter.com>

=head1 COPYRIGHT AND LICENSE

script/perlimports  view on Meta::CPAN


The C<--no-padding> arg allows you to disable the additional padding inside
parentheses.

    # --no-padding
    use Foo qw(bar baz);

=head2 --[no-]tidy-whitespace

C<--tidy-whitespace> is enabled by default. This means that use statements will
be updated even when the only change is in whitespace. Disabling this can help
reduce the churn involved when running C<perlimports>, especially if the codebase
does not have automated tidying.

If you have changed from C<--padding> to C<--no-padding> or vice versa, you'll
probably want to ensure that C<--tidy-whitespace> has also been enabled so that
you can see the whitespace changes.

=head2 --libs

A comma separated list of module directories which are not in your C<@INC>

script/perlimports  view on Meta::CPAN

You are encouraged to make this tool part of your automated tidying workflow.
Some guidance on how to configure this follows.

=head2 VIM

If you're a C<vim> user, you can pipe your import statements to perlimports directly.

    :vnoremap <silent> im :!perlimports --read-stdin --filename '%:p'<CR>

The above statement will allow you to visually select one or more lines of code
and have them updated in place by C<perlimports>. Once you have selected the
code enter C<im> to have your imports (re)formatted.

=head2 VIM and ALE

If you use ALE with vim, you can add something like this to your C<vim>
configuration. Note that this function will save your buffer before running
C<perlimports>.

    function! Perlimports(buffer) abort
      write

t/carp.t  view on Meta::CPAN


    eq_or_diff(
        $doc->tidied_document,
        $expected,
        'verbose is not inserted'
    );

    is_deeply(
        $doc->found_imports,
        { Carp => ['croak'] },
        'found imports updated'
    );
};

done_testing();



( run in 0.544 second using v1.01-cache-2.11-cpan-5511b514fd6 )