+print <<'';
+<p id="intro">Alternate rendition of Fyrd's <a href="http://caniuse.com/">when can I use...</a> page
+
+my ($canihas, $usage);
+my $minusage = $get{threshold} // .7;
+given ($get{usage} // 'wm') {
+ when (!$_) {
+ # none
+ }
+ when (!m{ \A [a-z]\w+ (?:/\d[\d-]*\d)? \z }x) {
+ printf "<p>Invalid browser usage data request: <em>%s</em>",
+ 'identifier must be alphanumeric name or <q>0</q>';
+ }
+ $canihas = do "data/browser/usage-$_.inc.pl" or do {
+ printf "<p>Browser usage data not found: <em>%s</em>", $! || $@;
+ break;
+ };
+ $usage = $_;
+ my $ref = $canihas->{-title} || 'unknown';
+ $ref = sprintf '<a href="%s">%s</a>', $_, $ref
+ for $canihas->{-site} || $canihas->{-source} || ();
+ $ref .= " $_" for $canihas->{-date} || ();
+ print "\nwith $ref browser usage statistics";
+}
+
+my @browsers;
+if ($usage) { # first() does not work inside given >:(
+ # adapt version usage to actual support data
+ my %engineuse; # prefix => usage sum
+ for my $browser (keys %versions) {
+ my $row = $canihas->{$browser} // {};
+ my $verlist = $versions{$browser} or next;
+ if ($minusage and sum(values %$row) < $minusage) {
+ delete $versions{$browser};
+ next;
+ }
+ my %supported = map { $_ => 1 } @$verlist;
+
+ # cascade unknown versions
+ $row->{$_} //= undef for @$verlist; # ensure stable keys during iteration
+ while (my ($version, $usage) = each %$row) {
+ next if defined $supported{$version};
+ my $next = first { paddedver($_) ge paddedver($version) } @$verlist
+ or warn("No fallback found for $browser v$version; $usage% ignored"), next;
+ $row->{$next} += $usage;
+ $row->{$version} = 0; # balance browser total
+ }
+
+ # build row list for each version
+ if ($minusage) {
+ my @vershown; # $verlist replacement
+ my ($rowusage, @verrow) = (0); # replacement row tracking
+ for (@$verlist) {
+ push @verrow, $_; # queue each version
+ if (($rowusage += $row->{$_}) >= $minusage) {
+ push @vershown, [@verrow]; # add row
+ ($rowusage, @verrow) = (0); # reset row tracking
+ }
+ }
+ push @vershown, \@verrow if @verrow; # always add latest
+ @$verlist = @vershown;
+ }
+ else {
+ @$verlist = map { [$_] } @$verlist;
+ }
+
+ # reusable aggregates (grouped by prefix (engine) and browser)
+ $engineuse{ $caniuse->{agents}->{$browser}->{prefix} } +=
+ $row->{-total} = sum(values %$row);
+ }
+
+ # order browser columns by usage grouped by engine
+ @browsers = sort {
+ $engineuse{ $caniuse->{agents}->{$b}->{prefix} } <=>
+ $engineuse{ $caniuse->{agents}->{$a}->{prefix} }
+ ||
+ $canihas->{$b}->{-total} <=> $canihas->{$a}->{-total}
+ } keys %versions;
+}
+else {
+ # order browser columns by name grouped by engine
+ @{$_} = map { [$_] } @{$_} for values %versions;
+ @browsers = sort {
+ $caniuse->{agents}->{$b}->{prefix} cmp
+ $caniuse->{agents}->{$a}->{prefix}
+ ||
+ $a cmp $b
+ } keys %versions;
+}
+:>.
+</p>
+
+<:
+$canihas ||= {
+ map {
+ $_ => +{
+ map {
+ my $zero = $#$_ - 2; # baseline index
+ ($_->[$zero - 2] => .5), # past
+ ($_->[$zero - 1] => 10 ), # previous
+ ($_->[$zero + 2] => 0 ), # future
+ ($_->[$zero + 1] => .5), # next
+ ($_->[$zero ] => 30 ), # current
+ } $caniuse->{agents}->{$_}->{versions}
+ }
+ } @browsers
+}; # fallback hash based on release semantics
+my $usagemax = (max(map { ref $_ eq 'HASH' && sum(values %$_) } values %$canihas) // 1) / 100;
+
+my $usagepct = 1; # score multiplier for 0..100 result
+# normalise usage percentage to only include shown browsers
+$usagepct = 100.01 / featurescore({ # yes for every possible version
+ map { $_ => { map {$_ => 'y'} map { @{$_} } @{$versions{$_}} } } keys %versions
+});
+