Skip to content

Instantly share code, notes, and snippets.

@pdl
pdl / pod-in-hash.pl
Created October 23, 2012 11:18
Using POD in dispatch tables: a curious issue not documented in the spec
#!perl -w
my $hashref = {
#
# POD may not appear here
#
p => sub{
=pod
@pdl
pdl / DeparseRange.pl
Created October 17, 2012 22:24
Turn an array of integers into a string of an array with ranges
use strict;
use warnings;
use List::Util qw(reduce);
my @in = (1,2,3,4,5,6,8,9,10,12,16,17,18,21);
print join (',',
map { defined $_->[1] ? "$_->[0]..$_->[-1]": $_->[0] }
@{
@pdl
pdl / nested-subs
Created September 3, 2012 16:55
Nested Subroutines in Perl - some odd behaviour
use strict;
use warnings;
my @LINES = qw(artichoke beetroot cabbage);
while (my $in = shift(@LINES)) {
my $results = [];
sub result {
push @$results, shift;
}
result(uc $in);
@pdl
pdl / syntax-grep-cmp-hashes
Created June 21, 2012 16:50
grep, cmp, and hashes: A weird syntax error in which perl apparently refuses to pick one of an ambiguous pair.
use strict;
use warnings;
my @comparanda;
my @similar;
my $opts = {};
@similar = grep { 0 == ($a->{$opts->{'name'}} cmp $b->{$opts->{'name'}}) } @comparanda; # case 1 - this is what I mean
@similar = grep { (0 == $a->{$opts->{'name'}}) cmp $b->{$opts->{'name'}} } @comparanda; # case 2 - I can see why perl might think this
@similar = grep { 0 == $a->{$opts->{'name'}} cmp $b->{$opts->{'name'}} } @comparanda; # case 3 is either 1 or 2
@pdl
pdl / Dancer diff for from_json utf8 bug
Created April 3, 2012 15:46
diff for recreating an apparent bug in from_json under Perl Dancer
diff -r MyWeb-App/lib/MyWeb/App.pm MyWeb-App-2/lib/MyWeb/App.pm
3c3
<
---
> set serializer => 'JSON';
9a10,14
> post '/test/' => sub {
> my $data = from_json( param('json'), {utf8 => 0});
> return { msg => $data->{q} }
> };