Skip to content

Commit ea2ca44

Browse files
jrngitster
authored andcommitted
userdiff/perl: catch sub with brace on second line
Accept sub foo { } as an alternative to a more common style that introduces perl functions with a brace on the first line (and likewise for BEGIN/END blocks). The new regex is a little hairy to avoid matching # forward declaration sub foo; while continuing to match "sub foo($;@) {" and sub foo { # This routine is interesting; # in fact, the lines below explain how... While at it, pay attention to Perl 5.14's "package foo {" syntax as an alternative to the traditional "package foo;". Requested-by: Ævar Arnfjörð Bjarmason <[email protected]> Signed-off-by: Jonathan Nieder <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 12f0967 commit ea2ca44

File tree

2 files changed

+40
-5
lines changed

2 files changed

+40
-5
lines changed

t/t4018-diff-funcname.sh

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,11 @@ package Beer;
3535
use strict;
3636
use warnings;
3737
use parent qw(Exporter);
38-
our @EXPORT_OK = qw(round);
38+
our @EXPORT_OK = qw(round finalround);
39+
40+
sub other; # forward declaration
41+
42+
# hello
3943
4044
sub round {
4145
my ($n) = @_;
@@ -46,6 +50,12 @@ sub round {
4650
print "$n bottles of beer on the wall.\n";
4751
}
4852
53+
sub finalround
54+
{
55+
print "Go to the store, buy some more\n";
56+
print "99 bottles of beer on the wall.\n");
57+
}
58+
4959
__END__
5060
5161
=head1 NAME
@@ -54,20 +64,23 @@ Beer - subroutine to output fragment of a drinking song
5464
5565
=head1 SYNOPSIS
5666
57-
use Beer qw(round);
67+
use Beer qw(round finalround);
5868
5969
sub song {
6070
for (my $i = 99; $i > 0; $i--) {
6171
round $i;
6272
}
73+
finalround;
6374
}
6475
6576
song;
6677
6778
=cut
6879
EOF
6980
sed -e '
81+
s/hello/goodbye/
7082
s/beer\\/beer,\\/
83+
s/more\\/more,\\/
7184
s/song;/song();/
7285
' <Beer.perl >Beer-correct.perl
7386

@@ -121,6 +134,10 @@ test_expect_success 'preset perl pattern' '
121134
test_expect_funcname "sub round {\$" perl
122135
'
123136

137+
test_expect_success 'perl pattern accepts K&R style brace placement, too' '
138+
test_expect_funcname "sub finalround\$" perl
139+
'
140+
124141
test_expect_success 'perl pattern is not distracted by sub within POD' '
125142
test_expect_funcname "=head" perl
126143
'
@@ -129,6 +146,10 @@ test_expect_success 'perl pattern gets full line of POD header' '
129146
test_expect_funcname "=head1 SYNOPSIS\$" perl
130147
'
131148

149+
test_expect_success 'perl pattern is not distracted by forward declaration' '
150+
test_expect_funcname "package Beer;\$" perl
151+
'
152+
132153
test_expect_success 'custom pattern' '
133154
test_config diff.java.funcname "!static
134155
!String

userdiff.c

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,23 @@ PATTERNS("pascal",
6060
"|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+"
6161
"|<>|<=|>=|:=|\\.\\."),
6262
PATTERNS("perl",
63-
"^package .*;\n"
64-
"^sub .* \\{\n"
65-
"^[A-Z]+ \\{\n" /* BEGIN, END, ... */
63+
"^package .*\n"
64+
"^sub [[:alnum:]_':]+[ \t]*"
65+
"(\\([^)]*\\)[ \t]*)?" /* prototype */
66+
/*
67+
* Attributes. A regex can't count nested parentheses,
68+
* so just slurp up whatever we see, taking care not
69+
* to accept lines like "sub foo; # defined elsewhere".
70+
*
71+
* An attribute could contain a semicolon, but at that
72+
* point it seems reasonable enough to give up.
73+
*/
74+
"(:[^;#]*)?"
75+
"(\\{[ \t]*)?" /* brace can come here or on the next line */
76+
"(#.*)?$\n" /* comment */
77+
"^[A-Z]+[ \t]*" /* BEGIN, END, ... */
78+
"(\\{[ \t]*)?" /* brace can come here or on the next line */
79+
"(#.*)?$\n"
6680
"^=head[0-9] .*", /* POD */
6781
/* -- */
6882
"[[:alpha:]_'][[:alnum:]_']*"

0 commit comments

Comments
 (0)