Acme-PlayCode

 view release on metacpan or  search on metacpan

lib/Acme/PlayCode/Plugin/DoubleToSingle.pm  view on Meta::CPAN

package Acme::PlayCode::Plugin::DoubleToSingle;

use Moose::Role;

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

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

    if ( $token->isa('PPI::Token::Quote::Double') ) {
        # XXX?
        # why treat 
        # bless( {
        #   'separator' => '"',
        #   'content' => '"c\\n"'
        # }, 'PPI::Token::Quote::Double' );
        # "c\\n" as not interpolations
        # bug?
        #
        # why "\012" is not interpolations??
        # another bug?
        
        # skip all '\' because I don't know which is fine
        if ( not $token->interpolations and
             not $token->content =~ /\\/ ) {
            my $str = $token->string;
            if ( $str !~ /\'/) {
                return "'$str'";
            } else {
                if ( $str !~ /\~/ ) {
                    return 'q~' . $str . '~';
                } else {
                    return $token->content;
                }
            }
        }
    } elsif ( $token->isa('PPI::Token::Quote::Interpolate') ) {
        # PPI::Token::Quote::Interpolate no ->interpolations
        my $str = $token->string;
        if ( $str =~ /^\w+$/ ) {
            return "'$str'";
        }
    }
    
    $orig->($self, @_);
};

no Moose::Role;

1;
__END__

=head1 NAME

Acme::PlayCode::Plugin::DoubleToSingle - Play code with Single and Double

=head1 SYNOPSIS

    use Acme::PlayCode;
    
    my $app = new Acme::PlayCode;
    
    $app->load_plugin('DoubleToSingle');
        
    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

=head1 DESCRIPTION

    my $a = "a";



( run in 0.542 second using v1.01-cache-2.11-cpan-39bf76dae61 )