Skip to content

Instantly share code, notes, and snippets.

#!/usr/bin/perl
use Modern::Perl;
use Benchmark 'cmpthese';
sub with_eval
{
eval
{
my ($a, $b) = (2, 4);
@zoul
zoul / REPL.pm
Created October 4, 2009 07:51
How to write a simple REPL interface to a Perl module
package REPL;
use Modern::Perl;
use Moose;
sub foo
{
return "foo";
}
@zoul
zoul / Sound.h
Created October 9, 2009 08:09
Simple wrapper around Audio Services
/*
- Trivial wrapper around system sound as provided by Audio Services.
- Don’t forget to link against the Audio Toolbox framework.
- Assumes ARC support.
*/
@interface Sound : NSObject
// Path is relative to the resources dir.
- (id) initWithPath: (NSString*) path;
@zoul
zoul / rss-filter.pl
Created December 21, 2009 21:02
Filter RSS using an XSL template
#!/usr/bin/perl
use Modern::Perl;
use XML::RSS::Tools;
use Perl6::Slurp;
my $style = slurp \*DATA;
my $feed = XML::RSS::Tools->new;
#$feed->set_http_client('lite');
#$feed->debug(1);
@zoul
zoul / crawler.pl
Created January 5, 2010 18:48
Simple web crawler in Perl
#!/usr/bin/perl
use Modern::Perl;
use WWW::Mechanize;
my $root = 'http://naima:3000/cs/';
my $domain = 'http://naima';
my $mech = WWW::Mechanize->new;
sub visit {
@zoul
zoul / objc-varargs.m
Created March 19, 2010 13:36
Variable arguments in Objective-C
- (void) processStrings: (NSString*) str1, ...
{
va_list args;
va_start(args, str1);
NSString *arg = str1;
while (arg != nil) {
// do something with arg
arg = va_arg(args, NSString*);
}
va_end(args);
@zoul
zoul / UIDevice+Model.h
Created March 20, 2010 10:14
Detect specific iPhone/iPod/iPad model
typedef enum {
kDeviceTypeUnknown = 0,
kDeviceTypeSimulator,
kDeviceTypeiPhone1G,
kDeviceTypeiPhone3G,
kDeviceTypeiPhone3GS,
kDeviceTypeiPhone4,
kDeviceTypeiPod1G,
kDeviceTypeiPod2G,
kDeviceTypeiPad
@zoul
zoul / UINavigationItemPlus.h
Created August 31, 2010 07:40
Multiple buttons in UINavigationItem
@interface UINavigationItem (Extensions)
- (void) setRightBarButtonItemsWithTotalWidth: (NSUInteger) width
items: (UIBarItem*) firstItem, ...;
@end
@zoul
zoul / DownloadOperation.h
Created September 14, 2010 06:36
Asynchronous NSURLConnection in concurrent NSOperation
@interface DownloadOperation : NSOperation
{
NSURLRequest *request;
NSURLConnection *connection;
NSMutableData *receivedData;
}
@property(readonly) BOOL isExecuting;
@property(readonly) BOOL isFinished;
@zoul
zoul / UIImage+ForceLoad.m
Created September 14, 2010 07:06
Forces UIImage to load and decode its data
@implementation UIImage (ForceLoading)
- (void) forceLoad
{
const CGImageRef cgImage = [self CGImage];
const int width = CGImageGetWidth(cgImage);
const int height = CGImageGetHeight(cgImage);
const CGColorSpaceRef colorspace = CGImageGetColorSpace(cgImage);