perl: optional line breaks in long code examples
[sheet.git] / perl.plp
1 <(common.inc.plp)><:
2
3 Html({
4         title => 'perl version cheat sheet',
5         version => '1.5',
6         keywords => [qw'
7                 perl version feature features comparison
8                 sheet cheat overview summary
9         '],
10         stylesheet => [qw'light dark red'],
11         data => ['perl.inc.pl'],
12 });
13
14 use experimental 'signatures';
15 :>
16 <h1>Perl release summary</h1>
17
18 <p>The most significant features introduced for recent versions of the Perl
19 scripting language.
20 <:
21 my $info = Data('perl');
22
23 use feature 'signatures';
24 sub vname ($v) {
25         return sprintf 'v%d%03d', unpack 'C*', $v;
26 }
27 sub linkversion ($v) {
28         return showlink(sprintf('%vd', $v), '#'.vname($v));
29 }
30
31 eval {
32         use List::Util 'first';
33         use Time::Piece;
34         use Time::Seconds;
35
36         my $now = Time::Piece->new;
37         if (my $ts = $get{at}) {
38                 $now = $now->strptime($ts, '%Y-%m-%d');
39                 say "Compatibility details emulated for <em>$ts</em>.";
40         }
41         my $ts = $now->strftime('%F');
42         my @versions = sort grep { $info->{$_}{release} le $ts } keys %{$info};
43
44         # perlpolicy: «We "officially" support the two most recent stable release
45         # series. [...] we will attempt to fix critical issues»
46         $info->{ $versions[-2] }{versum} //= "active core support";
47         $info->{ $versions[-1] }{versum} //= "latest stable release";
48
49         # perlpolicy: «we will attempt to fix critical issues in the two most
50         # recent stable 5.x release series»
51         my $coreeol = ($now - ONE_YEAR * 3)->strftime('%F');
52         my $vcore = first { $info->{$_}{release} ge $coreeol } @versions;
53         print "<p>Core security support is provided for 3 years";
54         print ", so typical users should run at least ", linkversion($_)
55                 for $vcore // ();
56         say '.';
57         $info->{$vcore}{versum} //= "official security patches";
58
59         # «We encourage vendors to ship the most recent supported release of Perl
60         # at the time of their code freeze» with debian&ubuntu having 5 years LTS
61         my $vendoreol = ($now - ONE_YEAR * 5)->strftime('%F');
62         my $vdebian = first {
63                 $info->{$_}{release} ge $vendoreol && $info->{$_}{distro}{debian}
64         } @versions;
65         say sprintf "Stable distributions such as Debian %s maintain %s+.",
66                 $info->{$_}{distro}{debian}, linkversion($_) for $vdebian // ();
67         $info->{$vdebian}{versum} //= "still maintained by common vendors";
68
69         # extended support given at random
70         my $nowcmp = $now->strftime('%F');
71         my $vdino = first { $info->{$_}{support} ge $nowcmp } @versions;
72         say "Enterprise platforms retain versions up to $_."
73                 for map { linkversion($_) } $vdino // ();
74         return 1;
75 } or Alert('Missing version recommendations', $@);
76 say '</p>';
77
78 for my $vernum (reverse sort keys %{$info}) {
79         my $verrow = $info->{$vernum};
80         defined $verrow->{unstable} and next unless exists $get{v};
81
82         say sprintf '<div class="section" id="%s">', vname($vernum);
83         my $title = $verrow->{release} // '?';
84         $title .= ": $_" for $verrow->{versum} // ();
85         say sprintf '<h2>%vd <small>%s</small></h2>', $vernum, $title;
86         say '<dl>';
87         for (@{ $verrow->{new} }) {
88                 my ($topic, $desc, $attr) = @{$_};
89                 $desc .= featattrs($attr);
90                 my $ref = defined $attr->{name} && sprintf ' id="%s"', $attr->{name};
91                 say sprintf '<dt%s>%s<dd>%s', $ref, $topic, $desc || '<br/>';
92         }
93         if (my $mods = $verrow->{modules}) {
94                 for (@{$mods}) {
95                         my ($name, $desc, $attr) = @{$_};
96                         my $ref = lc $name =~ s/::/_/gr;
97                         $desc .= featattrs($attr);
98                         printf '<dt id="%s"><code>use %s</code>', $ref, $name;
99                         say '<dd>', $desc;
100                 }
101         }
102         say sprintf '<dt>Unicode</dt><dd>v%s', $_ for $verrow->{unicode} || ();
103         say '</dl>';
104         say "</div>\n";
105 }
106
107 sub featattrs ($attr) {
108         $attr or return '';
109         ref $attr or $attr = {eg => $attr};
110         my $title;
111         if (defined $attr->{experimental}) {
112                 $title = 'experimental';
113         }
114         if (defined $attr->{dropped}) {
115                 return '' unless exists $get{v};
116                 $title = sprintf 'removed in %vd', $_ for $attr->{dropped} || ();
117         }
118         elsif ($attr->{stable}) {
119                 $title .= sprintf ' until %vd', $attr->{stable};
120         }
121         if ($attr->{experimental}) {
122                 $title = sprintf '<span title="experimental::%s">%s</span>',
123                         $attr->{experimental}, $title;
124                 $attr->{name} //= $attr->{experimental};
125         }
126         if ($attr->{feature}) {
127                 my $prefix = sprintf '<span title="%s">feature</span>',
128                         $attr->{feature};
129                 $title = join ', ', $prefix, $title // ();
130                 $attr->{name} //= $attr->{feature};
131         }
132         $title = $title ? sprintf ' <em class="ex">(%s)</em>', $title : '';
133
134         if (my $eg = $attr->{eg}) {
135                 my $pre = Entity($eg);
136                 $pre =~ s<\N{ZERO WIDTH SPACE}>{</code><wbr/><code>}g;
137                 $pre = " <small>{<code>$pre</code>}</small>";
138                 $title = $pre . $title;
139         }
140         return $title;
141 }