Devel-Decouple

 view release on metacpan or  search on metacpan

lib/Devel/Decouple.pm  view on Meta::CPAN


sub modules {
    my $self = shift;
    return exists $self->{_MODULES_}
        ? @{ $self->{_MODULES_} }
        : undef;
}

sub called_imports{
    my $self = shift;
    return exists $self->{_CALLED_IMPORTS_}
        ? @{ $self->{_CALLED_IMPORTS_} }
        : undef;
}

sub all_functions {
    my $self = shift;
    
    return $self->module
        ? @{Class::Inspector->functions( $self->module )}
        : undef;
}

sub revert{
    my $self      = shift;
    my @functions = @_;
    
    map { $self->{_PATCHES_}{$_} = undef } @functions;
    
    return $self;
}

sub report{
    my $self = shift;
    
    return $self->document
        ? $self->_build_report
        : croak qq{ 'report' called on uninitialized object };
}

### EXPORTS FOR SYNTACTIC SUGAR: ###########

sub from(\@;%) {
    return @_;
}

sub function($) {
    return shift;
}

sub functions(\@$;%) {
    my $functions = shift;
    my $code      = shift;
    my %args      = @_;
    
    my %map = map { $_ => $code } @{$functions};
    
    return %map, %args;
}

sub as(&) {
    return shift;
}

sub default_sub() {
    return '_DEFAULT_';
}

sub preserved() {
    return '_PRESERVED_';
}

### PRIVATE METHODS: #######################

sub _build {
    my $self   = shift;
    
    $self->_build_imports();
    $self->_set_code_substitutions();
}

sub _build_report {
    my $self    = shift;
    my $spacing = 20;
    my $indent  = 4;
    
    # format a simplistic report...
    my $report = "\nFunction-import usage statistics for ".($self->module||$self->document).":\n";
    
    for my $module ( $self->modules ){
        $report .= " "x($indent)."$module\n";
        map { $report .= " "x(2*$indent)."$_"." "x($spacing-length($_))."calls: ".
              $self->{_CALLED_IMPORT_STATS_}{$module}{$_}{ _NUMBER_OF_CALLS_ }."\tlines: ". 
              (join ',', @{$self->{_CALLED_IMPORT_STATS_}{$module}{$_}{_LINE_NUMBERS_}}).".\n" }
            sort keys %{$self->{_CALLED_IMPORT_STATS_}{$module}};
    }
    
    return $report;
}

sub _build_imports {
    my $self = shift;
    
    my ($CallFinder,$Document) = $self->_create_call_finder;
    my @found = $CallFinder->in( $Document );
    
    for my $module ( $self->_get_modules ){
        no strict 'refs';
        for my $function ( @{$module.'::EXPORT'}, @{$module.'::EXPORT_OK'} ){
            for my $token ( @found ){
                if ( $token eq $function ){
                    $self->{_CALLED_IMPORT_STATS_}{$module}{$function}{_NUMBER_OF_CALLS_}++;
                    push @{$self->{_CALLED_IMPORT_STATS_}{$module}{$function}{_LINE_NUMBERS_}}, $token->line_number;
                }
            }
        }
    }
    
    $self->{ _MODULES_ } = [ keys %{ $self->{_CALLED_IMPORT_STATS_} }];
    for my $module ( $self->modules ){
        push @{$self->{_CALLED_IMPORTS_}}, $_ for keys %{$self->{_CALLED_IMPORT_STATS_}{$module}};



( run in 1.655 second using v1.01-cache-2.11-cpan-d80b1682f3f )