Acme-Pythonic
view release on metacpan or search on metacpan
lib/Acme/Pythonic.pm view on Meta::CPAN
$line =~ /^\s*(?:BEGIN|CHECK|INIT|END):$/) {
chop $line;
if ($unbalanced_paren) {
$line .= ")";
$unbalanced_paren = 0;
} else {
($bracket_opened_by) = $line =~ /($id)\s*$/o;
}
}
} elsif (!$joining) {
$$line_with_modifier =~ s/\(// if $might_be_modifier;
$unbalanced_paren = 0;
$line .= ';';
}
# Handle indentation. Language::Pythonesque was the basis of
# this code.
my $prev_indent = @stack ? $stack[-1]{indent} : 0;
if ($prev_indent < $indent) {
push @stack, {indent => $indent, id_at_sob => $id_at_sob};
$$prev_line_with_code .= " {" unless $$prev_line_with_code =~ s/(?=\s*$tc)/ {/o;
} elsif ($prev_indent > $indent) {
do {
my $prev_id_at_sob = $stack[-1]{id_at_sob};
pop @stack;
$prev_indent = @stack ? $stack[-1]{indent} : 0;
$$prev_line_with_code .= "\n" . ((' ' x $prev_indent) . "}");
$$prev_line_with_code .= ";" if needs_semicolon($prev_id_at_sob);
} while $prev_indent > $indent;
$$prev_line_with_code =~ s/;$/ / if $might_be_modifier;
}
$id_at_sob = $bracket_opened_by;
} continue {
$line =~ s/^\s*pass;?\s*$//;
$prev_line_with_code = \$line if !$joining && $line =~ /\S/;
$line .= $comment;
}
$_ = join "\n", @lines;
s/$;BLANKED_OUT_\d+$;/$bos{$&}/go;
}
# In the trials I've done seems like the Python interpreter understands
# any of the three conventions, even if they are not the ones in the
# platform, and even if they are mixed in the same file.
#
# In addition, it guarantees make test works no matter the platform.
sub normalize_newlines {
s/\015\012/\n/g;
tr/\015/\n/ unless "\n" eq "\015";
tr/\012/\n/ unless "\n" eq "\012";
}
# Put an opening paren in the places we forgive parens. It will be later
# closed or removed as needed in the main subroutine.
sub left_parenthesize {
$_[0] =~ s/^(\s*\b(?:if|elsif|unless)\b\s*)/$1(/ ||
$_[0] =~ s/^(\s*(?:$id\s*:)?\s*\b(?:while|until)\b(\s*))/$2 eq '' ? "$1 (" : "$1("/eo ||
$_[0] =~ s/^(\s*(?:$id\s*:\s*)?\bfor(?:each)?\b\s*)(.*)/fortype_guesser($1,$2)/oxe
}
# Tries its best at guessing a for(each) type or, at least, where to put
# the opening paren.
#
# Returns a string which is a copy of the original with the paren
# inserted.
sub fortype_guesser {
my ($for, $rest) = @_;
my $guess = "";
# Try to match "for VAR in LIST", and "for VAR LIST"
if ($rest =~ m/^((?:my|our)? \s* \$ $id\s+) in\s* ((?: (?:[\$\@%&\\]) | (?:\b\w) ) .*)$/ox ||
$rest =~ m/^((?:my|our)? \s* \$ $id\s*) ((?: (?:[\$\@%&\\]) | (?:\b\w) ) .*)$/ox) {
$guess = "$for$1($2";
} else {
# We are not sure whether this is a for or a foreach, but it is
# very likely that putting parens around gets it right.
$rest =~ s/^\s*in\b//; # fixes "foreach in LIST"
$guess = "$for($rest";
}
return $guess;
}
# Guesses whether a block started by $id_at_sob needs a semicolon after the
# ending bracket.
sub needs_semicolon {
my $id_at_sob = shift;
return 0 if !$id_at_sob;
return 1 if $id_at_sob =~ /^(do|sub|eval)$/;
my $proto = $id_at_sob =~ /::/ ? prototype($id_at_sob) : prototype("${CALLER}::$id_at_sob");
return 0 if not defined $proto;
return $proto =~ /^;?&$/;
}
# We follow perlstyle here, as we did until now.
sub cuddle_elses_and_friends {
s/^([ \t]*})\s*(?=(?:elsif|else|continue)\b)/$1 /gm;
s/^([ \t]*})\s*(?=(?:if|unless|while|until|for|foreach)\b(?!.*{$tc?$))/$1 /gm;
}
1;
__END__
=head1 NAME
Acme::Pythonic - Python whitespace conventions for Perl
=head1 SYNOPSIS
use Acme::Pythonic; # this semicolon yet needed
sub delete_edges:
my $G = shift
while my ($u, $v) = splice(@_, 0, 2):
if defined $v:
$G->delete_edge($u, $v)
else:
my @e = $G->edges($u)
while ($u, $v) = splice(@e, 0, 2):
$G->delete_edge($u, $v)
=head1 DESCRIPTION
Acme::Pythonic brings Python whitespace conventions to Perl. Just C<use>
it and Pythonic code will become valid on the fly. No file is generated,
no file is modified.
This module is thought for those who embrace contradictions. A humble
contribution for walkers of the Whitespace Matters Way in their pursuit
of highest realization, only attained with L<SuperPython>.
=head1 OVERVIEW
Acme::Pythonic provides I<grosso modo> these conventions:
( run in 1.468 second using v1.01-cache-2.11-cpan-13bb782fe5a )