Multiplay Labs

tech hits and tips from Multiplay

Archive for March, 2012

Installing new modules for use with perlapp

without comments

Another really simple little thing when you find it but the ActiveState doc’s really hide how to install new perl modules that are required for compilation with their PDK’s perlapp when targeting a foreign platform.

perlapp --target <target> --target-install <module>

e.g.

perlapp --target windows --target-install JSON::XS

Written by Dilbert

March 21st, 2012 at 9:19 pm

Posted in Perl,Windows

Find and fix broken symlinks

without comments

Really simple one but useful nether-the-less to find broken symlinks

find -L <directory> -type l

And fix, remove them:

find -L <directory> -type l -print0 | xargs -0 rm

Written by Dilbert

March 20th, 2012 at 2:47 pm

Posted in FreeBSD

Running named under a jail on FreeBSD

without comments

Seems like this is quite a commonly encountered issue but its actually quite easy to fix, simply add the following to /etc/rc.conf

named_chrootdir=""

If you use rndc on that machine you’ll also want to make sure that controls block also includes your jails primary IP as well e.g.

controls {
    inet 127.0.0.1 allow { 127.0.0.1; <jail-ip>; } keys { "rndc-key"; };
    inet <named-ip> allow { 127.0.0.1; <jail-ip>; } keys { "rndc-key"; };
}

Written by Dilbert

March 9th, 2012 at 4:59 pm

Posted in FreeBSD,OS's

Using the jQuery deferred object pipe method to validate success data

with one comment

Often I’ll make an AJAX call with jQuery to retrieve some data and while that data might be not be erroneous, it might not be exactly what I want. A typical solution to this is to validate the data in the success callback and call out to an external error handler if it fails. However, jQuery provides the capability of setting a failure callback and it would be nice to use that without needing to have to resort to some horrible code like:

var errorHandler = function() {
	alert("ERROR!");
};
 
$.ajax({ 
	url: '/my_ajax_call', 
})
.done(function(data) {
	if(data.success !== 'true') {
		errorHandler();
	} else {
		// do the good stuff
	}
})
.fail(errorHandler);

The problem with the above is that you lose all the benefits of jQuery’s deferred objects[1] and as such if you wanted to attach additional error handlers, you’d have to update the done callback to call them each time which just isn’t feasible when you might be passing around your AJAX promise object all over the place.

However, there is a better way which allows you to leverage the full power of the deferred/promise object system by using the pipe[2] method.

$.ajax({ 
	url: '/my_ajax_call', 
})
.pipe(function(data, textStatus, jqXHR) { 
	if(data.success !== 'true') {
		var deferred = $.Deferred(); 
		deferred.rejectWith( this, [ jqXHR, textStatus, 'invalid' ] ); 
		return deferred; 
	}
	return jqXHR; // return the original deferred object
})
.done(function() { 
	alert('Hurray, a success!');
})
.fail(function() { 
	alert('Boo, a failure!');
})

In the above code, pipe accepts as it’s first argument a function that filters the done callback parameters before the callbacks functions are actually called. Returning null from pipe allows the unfiltered values to pass through, but returning a deferred object will apply the callbacks against that new one, rather than the original. As a result, you can return a deferred object in a failure state to force the failure callbacks to trigger. As you’ll note, using the rejectWith method of the deferred object allows by to fully set what the callbacks evaluation context will be and what arguments it will get. For compatibility, I follow the exact same setup as the defaults, but you’ll see I have set a custom error string.

Because pipe is chainable, you can add multiple of these further down the line. Pipe also accepts additional parameters for filtering other callbacks too. This way it’s much easier and cleaner to validate data and handle errors with the additional flexibility of the deferred object system.

Reference:
[1]: http://api.jquery.com/category/deferred-object/
[2]: http://api.jquery.com/deferred.pipe/

Written by Andrew Montgomery-Hurrell

March 9th, 2012 at 11:18 am

Posted in Hackery,Javascript

When optional method parameters aren’t optional in C#

without comments

So csharp added optional parameters in C# 4.0 which is great no more adding loads of method signatures when you want to add an additional parameter to an existing method, or so I thought.

While working on TShock I used optional parameters to extend the functionality of existing methods in the core API, to my surprised I started seeing reports of existing plugins failing with missing method exceptions. After some investigation I discovered that the way C# implements optional parameters is it bakes the defaults into the caller and not into the callee.

The result of this is that until all existing code is recompiled against the updated API, it will fail to run. Not only this but if you update the defaults values of your optional parameters then again all code must be recompiled, not just that containing the update, to ensure the new values are always used.

Clearly this is a PITA for public API’s so wanted to mention it so others are aware of this significant gotcha with optional parameters in csharp.

Written by Dilbert

March 3rd, 2012 at 4:19 pm

Posted in CSharp

Tagged with , ,