Posts

Showing posts from October, 2016

Run a server from Node.js

Image
Step 1: Download and install Node.js from https://nodejs.org/en/ Step 2: Launch node from command prompt and verify it has installed correctly Step 3: Create a new file server.js as follows: var http = require('http'); var fs = require('fs'); var url = require('url'); http.createServer( function (request, response) {    var pathname = url.parse(request.url).pathname;    fs.readFile(pathname.substr(1), function (err, data)    {       if (err)       {          console.log(err);          response.writeHead(404, {'Content-Type': 'text/html'});          response.write(err.toString());       }

Create JSON from HTML in Perl

Image
Assumption: Your HTML file is not pretty-encoded i.e. all content is contained in a single line. Code: #!/usr/bin/perl use strict; use warnings; use utf8; use JSON; use autodie; open my $fh1, '<files.html' or die "Cannot open files.html: $!\n"; my $html = <$fh1>; $html =~ s/<html><head><title>JSON Viewer<\/title><\/head><body><table border="1"><thead><tr><th width="300">Keys<\/th><th width="500">Values<\/th><\/tr><\/thead><tbody>//g; $html =~ s/<\/tbody><\/table><\/body><\/html>//g; my @rows = split("<\/tr>", $html); my %data; foreach my $row (@rows) {     my @cols = split("<\/td>", $row);     my $key;         foreach my $col (@cols)     {         if (not $col =~ /<br\/>/)       ...

Create HTML from JSON in Perl

Image
#!/usr/bin/perl use strict; use warnings; use utf8; use JSON; use autodie; use HTML::TreeBuilder; my $string; {     local $/;     open my $fh1,"<files.json" or die "open failed <files.json>: $!\n";     $string = <$fh1>;     close $fh1 or die "close failed <files.json>: $!\n"; } # Parse JSON in PERL my $data = decode_json($string); # Initialize HTML my $html = "<html><head><title>JSON Viewer</title></head><body><table border=\"1\"><thead><tr><th width=\"300\">Keys</th><th width=\"500\">Values</th></tr></thead><tbody>"; # Update HTML foreach my $key (keys(%$data)) {     $html = $html."<tr><td>".$key."</td><td>";     my $values = $data->{$key};     foreach my $value (@{$values})     {  ...