Tags / Perl
Permutation Generator
This little program uses currying to create a nested function reference that prints out a table with all possible permutations of the @states
array in as many columns as you like.
#!/usr/bin/env perl
use strict;
use warnings;
use feature 'say';
## Scott Wiersdorf
## Created: Sat Aug 18 14:29:03 MDT 2012
## permutation/truth table generator
my $inputs = shift @ARGV || 3; ## table columns
my @states = ('T', 'F', '-'); ## possible states
my $func = sub { say join "\t" => @_ };
for (1..$inputs) {
$func = loop_maker($func);
}
$func->();
exit;
sub loop_maker {
my $inner = shift;
return sub {
for my $state ( @states ) {
$inner->(@_, $state);
}
};
}
The output with an argument of ‘2’ looks like:
Perl SSL Debugging
I just spent about 3 hours trying to figure out why a Mojolicious daemon wasn’t permitting SSL connections. Here’s what I checked:
- the server was accessible (
iptables
, routing, etc.) - the port was accessible (I could set mojo’s
listen
tohttp://*:443
) and it would respond fine on my laptop - the entire
/usr/local
hierarchy,/etc
and/home/scott
were identical to my development environment (save the machine specific differences) and had the same permissions and ownership.
So basically at this point I narrowed it down to SSL. Something in the SSL setup wasn’t correct.
Perl Hash Reference Slices
Working with Perl’s references can sometimes be confusing. This document illustrates several ways to efficiently take a slice of a hash reference.
Here is a hash reference and an array of keys called @order
:
my $row = { foo => 'bar',
baz => 'blech',
one => 'uno',
tres => 'three',
cuatro => 'four or so' };
my @order = qw(one tres cuatro baz foo);
Here is one way to get a list of values in the order of @order
: