[ create a new paste ] login | about

Link: http://codepad.org/tXIMrpVW    [ raw code | fork ]

Perl, pasted on Sep 13:
#!/usr/bin/perl

use strict;
use warnings;

use Benchmark;

my $key  = "foo";
my %types = (
	"a full"   => { A => { B => { foo => "foo" } } },
	"b no_top" => { A => { B => { } } },
	"c only_a" => { A => { } },
	"d none"   => { },
);
my $ref;

my %subs = (
	insane => sub {
		return exists ${ ${ ${ $ref || {} }{A} || {} }{B} || {} }{$key}
	},
	sane => sub {
		return (
			exists $ref->{A}          and
			exists $ref->{A}{B}       and
			exists $ref->{A}{B}{$key}
		);
	},
	sane_with_high_precedence => sub {
		return (
			exists $ref->{A}          &&
			exists $ref->{A}{B}       &&
			exists $ref->{A}{B}{$key}
		);
	},
);

for my $type (keys %types) {
	$ref = $types{$type};

	print "$type\n";
	for my $sub (keys %subs) {
		print "\t$sub: ", $subs{$sub}() ? "true" : "false" , "\n";
	}
}

for my $type (sort keys %types) {
	$ref = $types{$type};

	print "\n$type\n";
	Benchmark::cmpthese -1, \%subs;
}


Create a new paste based on this one


Comments: