Acme::PlayCode::Plugin::PrintComma - Play code with printing comma


Acme-PlayCode documentation Contained in the Acme-PlayCode distribution.

Index


Code Index:

NAME

Top

Acme::PlayCode::Plugin::PrintComma - Play code with printing comma

SYNOPSIS

Top

    use Acme::PlayCode;

    my $app = new Acme::PlayCode;

    $app->load_plugin('PrintComma');

    my $played_code = $app->play( $code );
    # or
    my $played_code = $app->play( $filename );
    # or
    $app->play( $filename, { rewrite_file => 1 } ); # override $filename with played code

DESCRIPTION

Top

    print "a " . "print 'a' . 'b'" . "c\n";

becomes

    print "a ", "print 'a' . 'b'", "c\n";

SEE ALSO

Top

Acme::PlayCode, Moose, PPI, MooseX::Object::Pluggable

AUTHOR

Top

Fayland Lam, <fayland at gmail.com>

COPYRIGHT & LICENSE

Top


Acme-PlayCode documentation Contained in the Acme-PlayCode distribution.

package Acme::PlayCode::Plugin::PrintComma;

use Moose::Role;

our $VERSION   = '0.10';
our $AUTHORITY = 'cpan:FAYLAND';

use vars qw/$printcomma_start/;

around 'do_with_token' => sub {
    my $orig = shift;
    my $self = shift;
    my ( $token ) = @_;

    my $token_flag = $self->token_flag;
    my @tokens = $self->tokens;

    $printcomma_start = 0 unless ( defined $printcomma_start );

    if ( $token->isa('PPI::Token::Word') and $token->content eq 'print' ) {
        $printcomma_start = 1;
    } elsif ( $token->isa('PPI::Token::Structure') ) {
        $printcomma_start = 0;
    } elsif ( $printcomma_start and $token->isa('PPI::Token::Operator')
        and $token->content eq '.' ) {
        if ( $tokens[$token_flag - 1]->isa('PPI::Token::Whitespace') ) {
            $self->output->[-1] = 'Acme::PlayCode::!@#$%^&*()_+';
        }
        return ',';
    }
    
    $orig->($self, @_);
};

no Moose::Role;

1;
__END__