Daniel Rench

Web application development : Servers : Networks : E-Mail : DNS : Databases : Programming for hire

previous : contact : linkedin : code : links : pictures : facebook : twitter

Shebang: What?


% /usr/local/php5/bin/php --version
PHP 5.2.6 (cli) (built: May 11 2008 13:09:39) 
Copyright (c) 1997-2008 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2008 Zend Technologies
    with Zend Extension Manager v1.2.2, Copyright (c) 2003-2007, by Zend Technologies
    with Zend Optimizer v3.3.3, Copyright (c) 1998-2007, by Zend Technologies

% echo '<?= phpversion() ?>' | /usr/local/php5/bin/php
5.2.6

% printf '#!/usr/local/php5/bin/php\n<?= phpversion() ?>' > version
% chmod +x version
% ./version
5.2.6

% mv version x.php
% ./x.php
4.4.9

In case you're wondering, this machine is running Linux, not Windows.

November 6, 2009 update: I asked and got a quick answer on serverfault: somebody changed /proc/sys/fs/binfmt_misc/php.

modiff: The Perl Module Differ

Here's something else I wrote (like acuff) for working on Apache/Perl installations, particularly unfamiliar ones: modiff, the Perl module differ.

Say you need to update a certain Perl module. You check out the latest version, make your edits, "make install", and everything's great. Usually. Only this time you've just overwritten a hack that guy who got laid off never committed 6 months ago, and now everything's broken. Even rolling back to the previous svn revision can't save you now.

You should have run modiff first:

% cd ~/project/Really-Important-Module && svn update && modiff
U    lib/Really/Important/Module.pm
Updated to revision 32767.
*** /usr/local/lib/perl5/site_perl/5.8.7/Really/Important/Module.pm   Tue Apr  1 16:59:59 2008
--- lib/Really/Important/Module.pm    Wed Jan  7 20:35:37 2009
***************
*** 2,9 ****

  sub init {
        my $self = shift;
!       # $self->{conf} = parse_config('/etc/x.conf');
!       # This always bombs. Comment out until I figure out why
        return 1;
  }

--- 2,8 ----

  sub init {
        my $self = shift;
!       $self->{conf} = parse_config('/etc/x.conf');
        return 1;
  }

I first wrote modiff in Ruby but I eventually needed to use it on a server that did not have Ruby, and you probably will too. The version here is the sensible Perl rewrite.

Note that modiff works only if your module's source is in CPAN-style where *.pm files are under lib/. For nonstandard directory layouts or detecting changes to other files like *.pod or anything XS-related, you're on your own.

October 5, 2009 update: modiff is now part of my "futilities" collection on github.

acuff: Apache Configuration File Finder

If you've ever worked on an unfamiliar Apache installation you know how it is trying to follow all the includes (which may in turn include more files) to track down the source of a mysterious rewrite rule or LocationMatch. So I wrote acuff. It searches for an Apache executable in your $PATH and typical locations like /usr/local/apache2/bin and spits out its config filenames (with full paths) to standard output. My typical usage goes something like this:

% acuff | xargs grep -il '<location /logout'
/usr/local/etc/apache22/conf/app.conf

It's in Perl, and anything running Apache is bound to have Perl on it. It uses no extra CPAN modules but it does need Perl 5 (this being 2008 it's completely reasonable to ignore Perl 4 and earlier, I hope). Enjoy.

October 5, 2009 update: acuff is now part of my "futilities" collection on github.

EHANN: Javascript (and Perl and Ruby) Bloom Filters

I've never seen a Javascript Bloom filter implementation so I had to write one, and then had to port it to Perl and Ruby. You'll understand why soon if you don't already.

Quick aside: more than half of "my" bloomfilter.js is somebody else's BSD-licensed Javascript MD5 library but you could pretty easily replace it with some other hash library if you felt the compulsion.

I don't think this one would be practical for really large input sets (like a spell check dictionary built from /usr/dict/words), but I'm sure you'll think of something if you work at it. Get moving on that.

To get your brain in gear, here's a small demonstration.

First, I put the text of a really excellent Wire song (I won't say which), one word per line (minus punctuation) in a file. Then using the Ruby version I ran the file through this:

require 'bloomfilter'

bf = BloomFilter.new({ 'bits' => 0xff, 'salts' => %w(z A) })

STDIN.each_line { |l| l.chomp!; bf.add(l.downcase) }

print(b.to_json)

Or maybe I used the Perl one and ran it through this:

require 'bloomfilter.pl'; # what is this, 1994?

my $bf = BloomFilter->new({ bits => 0xff, salts => [qw(z A)] });

while(<>) { chomp; $bf->add(lc $_); }
print $bf->as_JSON();

Doesn't matter. Both give the same output (though I changed the formatting to make it a little easier to read):

{
 "buckets":[1509645767,990260715,1062483727,150062916,551224088,354022058,1818090233,1463314593,110],
 "bucketsize":31,
 "bits":255,
 "salts":["z","A"]
};

Type some Wire lyrics into the box below and smell the magic:

 

The filter check code runs 'onchange' for that text box. Just type (or paste in) some text, then click outside the box and you should get some feedback.

I encourage you to view the source of this page to see what's really going on but here's a quick example of the Javascript interface for the impatient:

// choose an appropriate number of bits and salts
// (don't ask me; you can figure it out)

var bf = new BloomFilter({ bits: 0xffff, salts: ['x','y','z','!'] });

bf.add('one');
bf.add('two');
bf.add('three');

bf.test('one'); // will definitely return true
bf.test('two'); // will definitely return true
bf.test('three'); // will definitely return true

bf.test('eins'); // will probably return false
bf.test('zwei'); // will probably return false
bf.test('drei'); // will probably return false

If you find bugs in this, please let me know. Also I'd really like to hear from anyone who finds other cool uses for this (aside from Wire lyric guessing games). And just to get this out of the way, I have no idea if this code can interoperate with the Bloom::Filter module on the CPAN but I should probably find out.

Hash::Server: a (mostly) memcached-compatible network interface to Perl hashes

You can download Hash::Server here until I get it on the CPAN (after a decade with Perl as my primary programming language I've finally signed up for a PAUSE account, so it could happen). Maybe you'll find this useful.

April 29, 2009 update: this code is now on github.



SYNOPSIS

        use Hash::Server ();
        use DB_File (); # for example
        tie(my %db, 'DB_File', './hash.db') or die $!;
        Hash::Server->new({ hash => \%db, localport => 11211 })->Bind();


DESCRIPTION

While memcached's original purpose was as a networked Memoize, it's also a simple way to distribute key/value hashes. But the official memcached (as the name implies) stores its data in memory, and is temporary. Given there are so many memcached clients for so many languages, it seemed a shame that there were no other ``backing stores'' available.

This module allows you to distribute any Perl hash structure you like with the memcached protocol. Typical use would probably involve a tied hash (as in the DB_File example above); if you want to serve a straight untied hash, you might as well use the real memcached, especially given the limitations of this module...


CAVEATS

This module is not a complete implementation of the memcached protocol.

  • It ignores any expiration times you provide; values are always assumed permanent.
  • It ignores any incoming ``flag'' value you provide when storing, and returns 0 by (default) when getting. Changing $Hash::Server::DEFAULT_FLAG to another value may help you on the client-side to differentiate a nonexistent key due to a server outage, or a nonexistent key because the key just does not exist.
  • With standard memcached (flags field aside, which you can't get with the Perl clients anyway), there is no difference between a nonexistent key and a down server. For pure caching, that's fine, but your application may need to know the difference. You can set $Hash::Server::UNDEFINED_KEY to some value that makes sense to you, such as 0 or q{} (empty string) and test for that, knowing that if your client returns 'undef', it means it didn't get that answer from the server.
  • The ``stats'' command is unimplemented.


SEE ALSO

http://code.sixapart.com/svn/memcached/trunk/server/doc/protocol.txt


AUTHOR

Daniel Rench, <citric@cubicone.tmetic.com>


COPYRIGHT AND LICENSE

Copyright (C) 2007 by Daniel Rench

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.7 or, at your option, any later version of Perl 5 you may have available.

It might help somebody: Flash 9 with Firefox on a Mac

When I install Firefox on a Mac, I always sudo chown -R 0:0 /Applications/Firefox.app after. What, you don't? But when trying to install Flash 9, I got this:

1008:5-5000 Access Denied Error
You do not have enough access privileges for this installation.
So I tried running the Flash install as root. Same error. Then I did this: sudo chown -R drench /Applications/Firefox.app and ran the install again, which worked, and it made me sad.

Element.getAttribute() doesn't get enough publicity

For years I've been using hacks like overloading the title and class attributes of HTML elements because I didn't know any better. Now I know I can just make up attributes, like 'regex' here, which I defined as a pattern that a form field must match:

<html>
<head><title>attributes</title>
<script>

function make_validator (form) {

	return function () {
		var pat, r;

		for (var i=0, e; e = form[i]; ++i) {

			pat = e.getAttribute('regex');
			if (! pat) continue;

			r = RegExp(pat).exec(e.value);
			if (! r) {
				e.focus();
				alert("Bad value for " + e.name);
				return false;
			}
		}
		return true;
	};
}

window.onload = function () {

	for (var i=0, form; form=document.forms[i]; ++i)
		form.onsubmit = make_validator(form);

};

</script>
</head>

<body>
<form>
	Number: <input type="text" name="number" regex="^\d+$">
	<input type="submit">
</form>

<form>
	Username: <input type="text" name="username" regex="^\w{1,8}$">
	<input type="submit">
</form>

</body>
</html>

We are all bigots / So filled with hatred / We release our poisons

... like the Bad R&R blog.

More like Rock & Roll Bigot if you ask me (who, "full disclosure" is a contributor to the site).

Yet another 'radical' idea in Unix file layout

Long before this ImageMagick overflow bug I've been wanting to ditch it completely in favor of GraphicsMagick (still hasn't happened). Aside from the usual bugs, arbitrary API changes and other endearing quirks, ImageMagick's greatest offense is how its utility programs have incredibly generic names like display and identify. GraphicsMagick does away with those by running everything through the gm command, so there's less confusion about what happens when you type gm identify somefile.jpg as you so often do.

This kind of thing is very common; cvs, svn and openssl (to name a few that I use often) all fit this pattern. Decent shells are aware of these programs' "subcommands" and will tab-complete them for you. But the chances that your shell's tab completion is an exact match with whatever version of, say, svn you're running are low. Maybe svn added a new command, but your shell completion doesn't know about it yet. Or maybe your shell completion has all the latest updates but you're stuck running an old version of svn that doesn't have it, allowing your tab completion to taunt you.

Imagine then if your shell was smart enough to detect directories in $PATH entries and gave them special treatment, looking for "subcommands" under them. Then svn could install itself under a directory called /usr/local/bin/svn/, where you'd have /usr/local/bin/svn/checkout, /usr/local/bin/svn/commit, /usr/local/bin/svn/log, etc. Instead of svn checkout you would run svn/checkout. And with a link from checkout to co you would still have your svn co shortcut. The commands themselves wouldn't necessarily need to look different; the shell could automatically translate svn co to svn/co for you and you might not even realize anything changed.

Update: Anonymous commentor made all kinds of good points (particularly about just using something like "gm-whatever") so this is a "nevermind".

The inexplicable popularity differences of two photos

Changing the diaper on the pretend baby

I realize that by linking to this photo I'm only making things worse, but why is the photo above among my top-viewed flickr pictures? And why is the one below one of the very least popular? It's among my favorites, but not yours? What's your problem?

Lemon #17

older things