App-perlimports

 view release on metacpan or  search on metacpan

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

    lazy    => 1,
    default => sub { shift->_include->module },
);

has _found_imports => (
    is          => 'ro',
    isa         => Maybe [ArrayRef],
    init_arg    => 'found_imports',
    handles_via => 'Array',
    handles     => {
        _all_found_imports => 'elements',
        _has_found_imports => 'count',
    },
);

has _indent => (
    is       => 'ro',
    isa      => Int,
    init_arg => 'indent',
    default  => 4,
);

has _pad_brackets => (
    is       => 'ro',
    isa      => Bool,
    init_arg => 'pad_brackets',
    default  => 0,
);

has _pad_imports => (
    is       => 'ro',
    isa      => Bool,
    init_arg => 'pad_imports',
    default  => 1,
);

has _tidy_whitespace => (
    is       => 'ro',
    isa      => Bool,
    init_arg => 'tidy_whitespace',
    lazy     => 1,
    default  => 1,
);

has _will_never_export => (
    is      => 'ro',
    isa     => Bool,
    lazy    => 1,
    default => sub {
        my $self = shift;
        return exists $self->_document->never_exports->{ $self->module_name }
            || $self->_export_inspector->is_oo_class;
    },
);

sub _build_export_inspector {
    my $self = shift;
    return $self->_document->inspector_for( $self->module_name );
}

# If we have implicit (but not explicit) exports,  we will make a best guess at
# what gets exported by using the implicit list.
sub _build_explicit_exports {
    my $self = shift;
    return $self->_export_inspector->has_explicit_exports
        ? $self->_export_inspector->explicit_exports
        : $self->_export_inspector->implicit_exports;
}

## no critic (Subroutines::ProhibitExcessComplexity)
sub _build_imports {
    my $self = shift;

    # This is not a real symbol, so we should never be looking for it to appear
    # in the code.
    $self->_delete_export('verbose') if $self->module_name eq 'Carp';

    my %found;

    # Stolen from Perl::Critic::Policy::TooMuchCode::ProhibitUnfoundImport
    for my $word ( $self->_document->possibly_imported_tokens ) {
        next if exists $found{"$word"};

        # stop if we've found everything that can be imported (every
        # possible exported symbol is being used)
        last unless $self->_imports_remain( \%found );

        # We don't want (for instance) pragma names to be confused with
        # functions.
        #
        # ie:
        # use warnings;
        # use Test::Warnings; # exports warnings()
        #
        # However, we also want to catch function calls in use statements, like
        # "use lib catfile( 't', 'lib');"
        #
        # or
        #
        # use Mojo::File qw( curfile );
        # use lib curfile->sibling('lib')->to_string;
        my $is_function_call = is_function_call($word);
        if (
               $word->parent
            && $word->parent->isa('PPI::Statement::Include')
            && (   !$is_function_call
                && !( $word->snext_sibling && $word->snext_sibling eq '->' ) )
        ) {
            next;
        }

        # Don't turn "use POSIX ();" into "use POSIX qw( sprintf );"
        # If it's a function and it's a builtin function and it's either not
        # included in found_imports or original imports are not implicit
        # then skip this.
        if (   defined $self->_found_imports
            && ( none { $_ eq $word } @{ $self->_found_imports } )
            && $is_function_call
            && is_perl_builtin($word) ) {
            next;
        }



( run in 1.287 second using v1.01-cache-2.11-cpan-8f98c5d2c55 )