Showing posts with label programming. Show all posts
Showing posts with label programming. Show all posts

Friday, September 10, 2010

junk code

Today I was tidying up my junk code directory. I cut this up one lunch time when I couldn't focus on the FTP server code I was bashing my head against - now sorted.

So, whats this for? There was a challenge a while back on the Brainwagon blog, using /dev/urandom to generate a list of 'random' characters to fit a pattern, ten blocks of five characters, each block separated by spaces.

This is used for testing cyphers and other fun stuff =) I spent some time when I was back in London working on some cypher and compression code. So I can vouch for the need for tools to generate large random sets - one time pads - that you can play back over and over to make sure that you reproduce that bug, with different versions of your code. Correctness was the issue at hand. Later came the performance issues.

So, Mark posted a "Unix/sh challenge". My brain needed a break and who can resist a bit lunch time shell coding? Well I tried and I got it pretty close. Anyway I decided to try a Perl version to see if I could make it easier to read, then make it faster.


#!/usr/bin/perl
my $limit = 10_000;
my @cypher;
while (<>) {
my @chars = split(undef, $_);
for my $char (@chars) {
if ($char =~ m/[A-Z]/ ) {
push @cypher, $char;
if ($limit <= scalar(@cypher)) {
my $offest = 0;
while (@cypher) {
for (1..10) {
print " ";
for (1..5) {
print pop @cypher;
}
}
print "\n";
}
exit;
}
}
}
}


72, Kim

Tuesday, September 22, 2009

use Getopt::Long;

This mornings very short moment of enlightenment was related to the use of Getopt::Long.

I had been messing with passing command line arguments to my server deployment script and had completely failed to get the --verbose flag to work the way I expected.

So I read the perldoc on it and found that I had been going about this the wrong way altogether.

I had this;

my $VERBOSE = 0;
GetOptions( "config=s" => \$config_file,
"verbose=i" => \$VERBOSE,
"modules" => \$INSTALL_MODULES );

print "DEBUG: message" if $VERBOSE;


Then on the command line I had to specify --verbose 1 or --verbose=1, which really wasn't what I had in mind. So this morning the mist on the man page cleared and it all clicked.

If you use "verbose!" => \$VERBOSE, then you can use --noverbose to unset the flag if you have previously set it in your code above. I'm not sure why I moved to using "verbose=i" => \$VERBOSE, but "verbose" => \$VERBOSE was not working as expected.

Anyhow, I was talking to Andy about putting "!" character after the command line flag name and now --verbose sets the variable $VERBOSE and it behaves as I previously expected.