Tk

Feb. 2, 2013, 7:49 p.m.
#!/usr/bin/perl

use Tk;

sub Exit {
    exit(0);
}

sub showHelp {
    $tl = $root->Toplevel;
    $tl->title("Help");
    $tl->Label(-text => '(c) tswr')->pack();
}

(Read more)

Hamming Code

Feb. 2, 2013, 7:45 p.m.

Source

<html>
<head>
<title>Snippet for "Hamming code"</title>
<style>
body { padding: 5px; background-color: lightgoldenrodyellow }
</style>
<script type="text/javascript">
function copy() {
    var input = document.getElementById('input');
    var output = document.getElementById('output');
    output.value = input.value;
}
</script>
</head>
<body>
    <h4>Snippet for "Hamming code"</h4>
    <input type='text' id='input'></input>
    <input type='button' onclick='copy()' value='copy'></input><br>

    <input type='text' id='output'></input>
</body>
</html>

Demo


IPC

Feb. 2, 2013, 12:36 p.m.

alarm

use Socket;

eval {
        local $SIG{ALRM} = sub { die "time is out" };
        alarm 3;
        socket SOCK, PF_INET, SOCK_STREAM, getprotobyname('tcp');
        connect SOCK, sockaddr_in(81, inet_aton("microsoft.com"))
                                         or die "connect failed";
        alarm 0;
        send SOCK, "GET / HTTP/1.0\n\n", 0;
        print while <SOCK>;
        shutdown SOCK, 2;
};
print "$@\n" if $@;

die

$warnings = 0;

$SIG{__WARN__} = sub { print @_ if $warnings };
$SIG{__DIE__} = sub { print "Goodbye, my darling!\n" };

warn "i'm only warning...";
die "i'm dying...";

(Read more)

Entropy

Feb. 2, 2013, 12:32 p.m.
var message = "ananas";
var hash = new Array();
for (var i = 0; i < message.length; i++) {
    var char = message.charAt(i);
    if (typeof(hash[char]) == 'undefined')
        hash[char] = 1;
    else
        hash[char]++;
}
for (var key in hash) {
    WSH.Echo(key + " : " + hash[key]);
}

Readfile

Feb. 2, 2013, 12:26 p.m.
function readfile(filename) {
    var ForReading = 1;
    var fso, f, s = "";
    fso = new ActiveXObject("Scripting.FileSystemObject");
    f = fso.OpenTextFile(filename, ForReading, false);
    try {
        s = f.ReadAll();
    } catch(e) {
        // ...
    }
    return s;
}

var s = readfile("1.txt");
WScript.Echo(s);
var a = s.split("\r\n");
WScript.Echo(":" + a[0] + ":");