Skip to content

Commit 5fe03dd

Browse files
committed
PPC0034: Use code fences for Perl code blocks, so highlighters DTRT
1 parent e2a1d42 commit 5fe03dd

File tree

1 file changed

+92
-58
lines changed

1 file changed

+92
-58
lines changed

ppcs/ppc0034-signature-refalias.md

Lines changed: 92 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,23 @@ Allow declaring ref-aliased parameters in subroutine signatures.
1515

1616
As of Perl version 5.42, we can do:
1717

18-
use v5.42;
19-
use experimental 'declared_refs';
18+
```perl
19+
use v5.42;
20+
use experimental 'declared_refs';
2021

21-
sub foo {
22-
my (\%a) = @_;
23-
...
24-
}
22+
sub foo {
23+
my (\%a) = @_;
24+
...
25+
}
26+
```
2527

2628
but we cannot do:
2729

28-
sub foo(\%a) {
29-
...
30-
}
30+
```perl
31+
sub foo(\%a) {
32+
...
33+
}
34+
```
3135

3236
## Rationale
3337

@@ -43,68 +47,86 @@ declared as ref-alias (requires the `refaliasing` feature and the
4347
`declared_refs` feature, see "Assigning to References" and "Declaring
4448
a Reference to a Variable" in perlref for more details):
4549

46-
sub go_over(\@things) { say "Oooh, $_!" for @things }
50+
```perl
51+
sub go_over(\@things) { say "Oooh, $_!" for @things }
52+
```
4753

4854
This subroutine must still be called with a scalar value, but the
4955
value must now be a reference to an array. Equivalently:
5056

51-
sub look_at(\%these) {
52-
say "$_ means $these{$_}" for sort keys %these;
53-
}
57+
```perl
58+
sub look_at(\%these) {
59+
say "$_ means $these{$_}" for sort keys %these;
60+
}
61+
```
5462

5563
must be called with a reference to a hash, and:
5664

57-
sub normalise(\$string) {
58-
$string = lc($string);
59-
}
65+
```perl
66+
sub normalise(\$string) {
67+
$string = lc($string);
68+
}
69+
```
6070

6171
must be called with a reference to a scalar. As with normal scalar
6272
parameters, ref-aliased parameters can be ignored:
6373

64-
sub ignore(\@) { ... }
74+
```perl
75+
sub ignore(\@) { ... }
76+
```
6577

6678
and have default values:
6779

68-
sub walk($cb, \@nodes, \%seen ||= {}) {
69-
for my $node (@nodes) {
70-
next if $seen{$node->id}++;
71-
$cb->($node);
72-
walk($cb, $node->children, \%seen);
73-
}
80+
```perl
81+
sub walk($cb, \@nodes, \%seen ||= {}) {
82+
for my $node (@nodes) {
83+
next if $seen{$node->id}++;
84+
$cb->($node);
85+
walk($cb, $node->children, \%seen);
7486
}
87+
}
88+
```
7589

7690
The last example is equivalent to:
7791

78-
sub walk($cb, $nodes, $seen ||= {}) {
79-
for my $node ($nodes->@*) {
80-
next if $seen->{$node->id}++;
81-
$cb->($node);
82-
walk($cb, $node->children, $seen);
83-
}
92+
```perl
93+
sub walk($cb, $nodes, $seen ||= {}) {
94+
for my $node ($nodes->@*) {
95+
next if $seen->{$node->id}++;
96+
$cb->($node);
97+
walk($cb, $node->children, $seen);
8498
}
99+
}
100+
```
85101

86102
with different legibility trade-offs.
87103

88104
Notice that arguments are still passed by reference, so any
89105
modification to their contents will be seen by the caller. In other
90106
words:
91107

92-
sub my_push(\@items, $new_one) {
93-
push @items, $new_one;
94-
}
108+
```perl
109+
sub my_push(\@items, $new_one) {
110+
push @items, $new_one;
111+
}
112+
```
95113

96114
works exactly the same way as:
97115

98-
sub my_push($items, $new_one) {
99-
push $items->@*, $new_one;
100-
}
116+
```perl
117+
sub my_push($items, $new_one) {
118+
push $items->@*, $new_one;
119+
}
120+
```
101121

102122
or
103123

104-
sub my_push {
105-
my (\@items, $new_one) = @_;
106-
push @items, $new_one;
107-
}
124+
```perl
125+
sub my_push {
126+
my (\@items, $new_one) = @_;
127+
push @items, $new_one;
128+
}
129+
```
108130

109131
## Backwards Compatibility
110132

@@ -115,7 +137,9 @@ confusion for existing code.
115137

116138
`PPI` can parse it just fine:
117139

118-
sub foo(\@a=[])
140+
```perl
141+
sub foo(\@a=[])
142+
```
119143

120144
produces:
121145

@@ -142,24 +166,28 @@ None foreseen.
142166

143167
`LWP::Protocol::http::hlist_remove` looks like this:
144168

145-
sub hlist_remove {
146-
my($hlist, $k) = @_;
147-
$k = lc $k;
148-
for (my $i = @$hlist - 2; $i >= 0; $i -= 2) {
149-
next unless lc($hlist->[$i]) eq $k;
150-
splice(@$hlist, $i, 2);
151-
}
169+
```perl
170+
sub hlist_remove {
171+
my($hlist, $k) = @_;
172+
$k = lc $k;
173+
for (my $i = @$hlist - 2; $i >= 0; $i -= 2) {
174+
next unless lc($hlist->[$i]) eq $k;
175+
splice(@$hlist, $i, 2);
152176
}
177+
}
178+
```
153179

154180
It could become:
155181

156-
sub hlist_remove(\@hlist, $k) {
157-
$k = lc $k;
158-
for (my $i = @hlist - 2; $i >= 0; $i -= 2) {
159-
next unless lc($hlist[$i]) eq $k;
160-
splice(@hlist, $i, 2);
161-
}
182+
```perl
183+
sub hlist_remove(\@hlist, $k) {
184+
$k = lc $k;
185+
for (my $i = @hlist - 2; $i >= 0; $i -= 2) {
186+
next unless lc($hlist[$i]) eq $k;
187+
splice(@hlist, $i, 2);
162188
}
189+
}
190+
```
163191

164192
## Prototype Implementation
165193

@@ -178,17 +206,23 @@ Paul for that expression).
178206

179207
Paul Evans suggested using an attribute instead:
180208

181-
sub foo (%a :refalias)
209+
```perl
210+
sub foo (%a :refalias)
211+
```
182212

183213
which would extend to named parameters:
184214

185-
sub foo (:%a :refalias)
215+
```perl
216+
sub foo (:%a :refalias)
217+
```
186218

187219
and be similar to a proposed "aliased scalars" feature:
188220

189-
sub foo ($a :alias) { $a=1 }
190-
# equivalent to:
191-
sub foo { $_[0]=1 }
221+
```perl
222+
sub foo ($a :alias) { $a=1 }
223+
# equivalent to:
224+
sub foo { $_[0]=1 }
225+
```
192226

193227
One of the problems with the attribute-based approach is that `sub
194228
foo(%a)` and `sub foo(%a :refalias)` have very different signatures

0 commit comments

Comments
 (0)