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