Skip to content

Instantly share code, notes, and snippets.

@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} }
> };
@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 / 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 / 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 / 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 / pl2py.pl
Created October 23, 2012 14:28
Convert perl scripts to python with the awesome power of regular expressions.
#!perl -w
=head1 NAME
pl2py.pl
=head1 DESCRIPTION
Attempts to convert perl scripts to python with the awesome power of regular expressions.
@pdl
pdl / overload-stringify.pl
Created November 11, 2012 17:28
Why do overloaded objects bechave differently depending on their construction?
package Acme::Object::Overloaded;
use overload '""' => sub{ 'String' }, '0+' => sub{ 123 }, 'fallback' => '0+';
sub new{
bless {}, shift;
}
1;
package main;
@pdl
pdl / subclasshash.pl
Created January 1, 2013 18:35
Is there a perl module that allows inheritance like a compile-time modifiable symbol table?
use strict;
use warnings;
use 5.010;
{
package A;
our $hash={1=>'i',2=>'j'};
}
{
@pdl
pdl / self-sorting-script.pl
Created January 24, 2013 20:45
A script which sorts itself
no less; use strict; use warnings;
open (my $fhIN, '<', $0) or die; my @lines = <$fhIN>;
open (my $fhOUT, '>', $0) or die;
print $fhOUT sort {$a cmp $b} @lines;
@pdl
pdl / first_or_all.pl
Created July 17, 2013 14:20
Demonstrate simple function to return the first item in an array if called in scalar context.
#!perl
use strict;
use warnings;
use Test::More;
sub first_or_all {
return @_ if wantarray;
return shift;
}