Acme-PPIx-MetaSyntactic
view release on metacpan or search on metacpan
lib/Acme/PPIx/MetaSyntactic.pm view on Meta::CPAN
{
$n->{$word->symbol} ||= $case->($self->_get_name);
$word->set_content($sigil . $n->{$word->symbol});
}
elsif ($word->isa("PPI::Token::ArrayIndex")) # like $#foo
{
$n->{"\@$rest"} ||= $case->($self->_get_name);
$word->set_content($sigil . $n->{"\@$rest"});
}
}
for my $qq (@{ $self->document->find("PPI::Token::Quote") || [] })
{
# A string that "co-incidentally" happens to have the name as a locally
# defined sub. This might be a __PACKAGE__->can("foo"), so change it!
#
if ($ls->{$qq->string})
{
my $txt = "$qq";
$txt =~ s/${\quotemeta($qq->string)}/$n->{$qq->string}/eg;
$qq->set_content($txt);
}
# An interpolated string. We'll do our best to find any variables
# within it and rename them, but PPI doesn't really look inside
# interpolated strings (yet?).
#
elsif ($qq->isa("PPI::Token::Quote::Double") or $qq->isa("PPI::Token::Quote::Interpolate"))
{
my $txt = "$qq";
$txt =~ s/([\$\@]\w+)/$n->{$1}?substr($1,0,1).$n->{$1}:$1/eg;
$qq->set_content($txt);
}
}
}
1;
__END__
=pod
=encoding utf-8
=head1 NAME
Acme::PPIx::MetaSyntactic - rename functions and variables in a PPI::Document using Acme::MetaSyntactic
=head1 SYNOPSIS
my $acme = "Acme::PPIx::MetaSyntactic"->new(document => \<<'END');
use v5.010;
use constant PLACE => "World";
sub join_spaces {
return join " ", @_;
}
my @greetings = qw(Hello);
say join_spaces($greetings[0], PLACE);
END
say $acme->document;
Example output:
use v5.010;
use constant VULTURE => "World";
sub fraud {
return join " ", @_;
}
my @gang_of_thieves = qw(Hello);
say fraud($gang_of_thieves[0], VULTURE);
=head1 DESCRIPTION
This module uses L<PPI> to parse some Perl source code, find all the
variables and function names defined in it, and reassign them random names
using L<Acme::MetaSyntactic>.
=head2 Constructor
This module is object-oriented, though there's really very little reason
for it to be.
=over
=item C<< new(%attributes) >>
Moose-style constructor.
=back
=head2 Attributes
All attributes are read-only.
=over
=item C<< document >>
The L<PPI::Document> that will be munged.
Can be coerced from a C<< Str >> (filename), C<< ScalarRef[Str] >> (string
of Perl source), C<< ArrayRef[Str] >> (lines of Perl source) or
C<< FileHandle >>.
Required.
Once the C<document> attribute has been set, a trigger automatically runs
the relabelling.
=item C<< theme >>
The L<Acme::MetaSyntactic> object that will be used to obtain new names.
If your source code is more than a couple of lines; choose one that provides
a large selection of names.
Can be coerced from C<< Str >> (theme name).
Defaults to the C<< "haddock" >> theme.
=item C<< local_subs >>
HashRef where the keys are the names of subs which are considered locally
defined (i.e. not Perl built-ins, and not imported) and thus available for
relabelling. Values are expected to all be C<< "1" >>.
Can be coerced from C<< ArrayRef[Str] >>.
Defaults to a list built by scanning the C<document> with PPI.
=item C<< names >>
( run in 0.966 second using v1.01-cache-2.11-cpan-483215c6ad5 )