September 30, 2010

Synchronous getJSON jquery call

What a pain. Here's an easy way to make a synchronous getJSON call with jquery:

var jsonObjectInstance = $.parseJSON(
    $.ajax({
         url: "json_data_plz.cgi", 
         async: false, 
         dataType: 'json'
        }
    ).responseText
);

September 2, 2010

MySQL to CSV

Here's a quick example how to dump a MySQL query to CSV

mysql  -e 'SHOW VARIABLES' | 
  perl -F'\t' -lane 'print "\"" . join("\",\"", map { $_ =~ s/"/\"/g; $_ =~ s/\n/\\n/g; $_; } @F) . "\""' > mysql_variables.csv 

August 31, 2010

So how do you really feel? (mmm perl)

So a while back I was playing with map+grep+anonymous arrays when I realized how quickly code can manifest with the "right-to-left" flow you can use in perl. As fast as I could think, this code was on the page. Obviously I had to write a quick one liner to encode the message that is "$the_truth", but that was easy enough.

(Originally this was on one line, but it looks terrible on the blog that way... I've already spent, easily, 10 times the amount of time trying to format the code than I did writing the code)
use strict; 
use warnings; 
my $the_truth = "0a796177796e6120656d2065766f6c20756f7920747562202c6472656e2061206d49"; 

my $tell = sub { 
  return join("",
    map { chr(hex($_)) } 
    grep { $_ } 
    reverse(split(/(.{2})/, $_[0]))) 
}; 

print $tell->($the_truth);