1999 twenty-four merry days of Perl Feed

Testing various syntax highlighting languages

perl - 1999-12-06

Perl

sub do_it {
    state $scalar = 137;
    state @array = qw( 1, 3, 7 );
    state %hash = map { $_, 1 } (1, 3, 7);

    if( exists $hash{$args[0]} ) { ... } # leave off the ->
}

Perl with indented heredoc

if( ... ) {
    my $string = <<~'HERE';
This string is outdented!
        Here's another line!
        This is nicer!
HERE

    ...
}

Go

package main

import "C"
import "fmt"

func main() {}

//export WishMerryChristmas
func WishMerryChristmas() {
    fmt.Println("We wish you a Merry Christmas");
}

ini

title    = Perl Advent Calendar 1999
tagline = twenty-four merry days of Perl
year = 1999
end_date = 1999-12-25
uri = https://perladvent.org/1999/
category = Perl
editor = PerlAdvent Org

article_dir = articles
share_dir = share
css_href = prism.css
js_src = prism.js

[Palette]
generic00 = #fff
generic01 = #d00

bodyBG = #333

feedLinkFG= #0a0
titleFG = genericWhite

taglineBG = #fff
taglineFG = #a00

codeBG = genericBlack
codeFG = genericWhite
codeNumbersBG = #800
codeNumbersBorder = genericWhite

css

/* PPI HTML Style */

.code-listing .keyword { color: #89f; }
.code-listing .match { color: #ff0; }
/* ... */
.code-listing .single,
.code-listing .double { color: #0cf; }

/* Vim Syntax Style */

.code-listing .synComment { color: #0f0; }
.code-listing .synConstant { color: #0ff; }
/* ... */
.code-listing .synError { color: #f00; }
.code-listing .synTodo { color: #aa0; }

yaml

name: weather
appspec: { version: '0.001' }
title: Weather forecast
class: App::Weather
# no global options; -h|--help will be there automatically
options: []
subcommands:
  forecast:
    summary: Show forecast for a city
    op: forecast # the method in App::Weather
    parameters:
      - spec: country=s --Country name
      - spec: city=s --City name
    options:
      - spec: show-temperature|T --Display temperature
      - spec: fahrenheit --Temperature in Fahrenheit
      - spec: celsius --Temperature in Celsius
  list:
    subcommands:
      countries:
        summary: List countries
        op: weather_countries
      cities:
        summary: List cities
        op: weather_cities
        options:
          # The first element of the spec here is actually very similar
          # to the syntax for Getopt::Long
          - spec: country|c=s --Country name

C

/* types */
#define JSON_TYPE_SCALAR 0x0000
#define JSON_TYPE_BOOL 0x0001
#define JSON_TYPE_INT 0x0002
#define JSON_TYPE_FLOAT 0x0003
#define JSON_TYPE_STRING 0x0004
/* flags */
#define JSON_TYPE_CAN_BE_NULL 0x0100
/* null type */
#define JSON_TYPE_NULL JSON_TYPE_CAN_BE_NULL

javascript

#!/usr/bin/env node
var json = '';

process.stdin.resume();

process.stdin.on('data', function(chunk) { json += chunk });

process.stdin.on('end', function() {
    var input = JSON.parse(json),
        kid_name = input.kid_name,
        xmas_list = input.xmas_list,
        toy_count = 0;

    xmas_list.forEach(function (item) {
        console.log(kid_name + " would like " + item.gift + " (" + item.quantity + ").");
        toy_count += item.quantity;
    });

    console.log("Dearest Elf, please make " + toy_count + " gifts for " + kid_name + ".");
});

HTML

<html>
    <head>
<script src="https://webperlcdn.zero-g.net/v0.07-beta/webperl.js"
integrity="sha256-jL8SB7St5ou4+hb0frK0k6VCQXsWQ1wolDrdU7i4juc="
crossorigin="anonymous"></script>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"
integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
crossorigin="anonymous"></script>
</head>
    <body>
        fib(
            <input id="in" type="number" min="1" max="49">
        ) = <span id="out"></span>

        <script type="text/perl">
use Memoize qw( memoize );
sub fib {
return 1 if $_[0] <= 2;
return fib($_[0] - 1) + fib($_[0] - 2);
}
memoize('fib');

my $jq = js('jQuery');
$jq->('#in')->on('change', sub {
$jq->('#out')->text(
fib( $jq->('#in')->val )
);
});
</script>
    </body>
</html>

JSON

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Action": [
                "ses:SendEmail",
                "ses:SendRawEmail"
            ],
            "Resource": "*",
            "Effect": "Allow"
        }
    ]
}

XML

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>org.perladvent.example</string>

    <!-- this is the program we want to run. We can't specify
a script here with a shebang line, it has to be an
actual executable, so we specify our perl and pass
the name of the script as an argument -->
    <key>ProgramArguments</key>
    <array>
        <string>/usr/bin/perl</string>
        <string>/Users/Nougat/servers/example.pl</string>
    </array>

    <!-- we don't want to be loaded right away, we want to loaded
on demand when someone tries to connect to the port -->
    <key>OnDemand</key>
    <true/>

    <!-- here's where we're listening - on port 54321 -->
    <key>Sockets</key>
    <dict>
        <key>Listeners</key>
        <array>
            <dict>
                <key>SockFamily</key>
                <string>IPv4</string>
                <key>SockServiceName</key>
                <string>54321</string>
            </dict>
        </array>
    </dict>

    <!-- finally we want to configure launchd to emulate
inetd in 'nowait' mode. This'll mean that launchd
will handle all the port stuff for us. It'll
execute our program when someone connects and pass data
sent to the port to the program via STDIN and anything
output to STDOUT will be sent back across the port -->
    <key>inetdCompatibility</key>
    <dict>
        <key>Wait</key>
        <false/>
    </dict>
</dict>
</plist>

SQL

DROP DATABASE IF EXISTS prototype;
CREATE DATABASE prototype;

\c prototype;

CREATE TABLE stocking_address (
    stocking_address_id UUID NOT NULL,
    street_address TEXT NOT NULL,
    lat float,
    lon float
);

ALTER TABLE ONLY stocking_address
    ADD CONSTRAINT stocking_address_pkey
    PRIMARY KEY (stocking_address_id);

CREATE TABLE child (
    child_id UUID NOT NULL,
    stocking_address_id UUID,
    name TEXT NOT NULL
);

ALTER TABLE ONLY child
    ADD CONSTRAINT child_pkey
    PRIMARY KEY (child_id);

TOML

# Valid log levels are:
# debug, info, notice, warning, error, critical, alert, emergency
# critical, alert and emergency are not currently used.
#
# Please use boolean values in this config file. Negated options (--no-*) are
# not permitted here. Explicitly set options to true or false.
#
# Some of these values deviate from the regular perlimports defaults. In
# particular, you're encouraged to leave preserve_duplicates and
# preserve_unused disabled.

cache = false # setting this to true is currently discouraged
ignore_modules = []
ignore_modules_filename = ""
ignore_modules_pattern = "" # regex like "^(Foo|Foo::Bar)"
ignore_modules_pattern_filename = ""
libs = ["lib", "t/lib"]
log_filename = ""
log_level = "warn"
never_export_modules = []
never_export_modules_filename = ""
padding = true
preserve_duplicates = false
preserve_unused = false
tidy_whitespace = true

SH

# commit all the code that have "use strict"
bash$ ack -l --perl --print0 'use strict' | xargs -0 git add
bash$ git commit -m 'add strictures'

Kotlin

package example

fun reverseString(str: String) : String {
    return str.reversed()
}
Gravatar Image This article contributed by: Thibault DUPONCHELLE <thibault.duponchelle@gmail.com>