Web-based recursion

autarch on 2007-05-02T20:09:51

Reg Braithwaite put out a call for programs that would print out the 128-bit HD-DVD key that has been making news lately.

My entry was this:

#!/usr/bin/perl

use strict; use warnings;

use File::Temp qw( tempdir ); use File::Which qw( which ); use WWW::Mechanize;

my $mech = WWW::Mechanize->new(); $mech->get('http://weblog.raganwald.com/2007/05/128-bit-programming-challenge.html');

my @binary = $mech->content() =~ m{([01]{8})
print join ' ', map { uc sprintf( '%02x', eval "0b$_" ) } @binary; print "\n";

my @hex = map { split / / } $mech->content() =~ m{([0-9A-F]{2} [0-9A-F]{2} [0-9A-F]{2} [0-9A-F]{2})
my ($php_frag) = $mech->content() =~ m{PHP:(.+)
}; $php_frag =~ s{}{\n}gi;

if ( $php_frag && which('php') ) { my $dir = tempdir( CLEANUP => 1 ); my $file = "$dir/hex.php";

my $php = <<"EOF"; EOF

open my $fh, '>', $file or die "Cannot write to $file: $!"; print $fh $php or die "Cannot write to $file: $!"; close $fh or die "Cannot write to $file: $!";

system( scalar which('php'), $file ); }

if ( @ARGV && $ARGV[0] eq '--recurse' ) { print "\nAre you sure about this?\n\n"; $mech->get('http://www.urth.org/user/autarch/hex');

my $dir = tempdir( CLEANUP => 1 ); my $file = "$dir/hex.pl";

open my $fh, '>', $file or die "Cannot write to $file: $!"; print $fh $mech->content() or die "Cannot write to $file: $!"; close $fh or die "Cannot write to $file: $!";

chmod 0755, $file or die "Cannot chmod $file to 0755: $!";

exec( $file, '--recurse' ); }


To really appreciate it, you have to read his journal entry first, then at least the first few comments. If I'd spent more time, I might've tried to make it parse all the comments, figure out the language, and execute the appropriate program.

Ok, back to work.