Skip to content

Instantly share code, notes, and snippets.

View rsimoes's full-sized avatar

Richard Simões rsimoes

  • Center for Technology and Civic Life
  • Austin, Texas
View GitHub Profile
package My::Types;
use strict;
use warnings;
use Type::Utils;
use Type::Library -base,
-declare => 'Percentage';
use Types::Standard qw(Num);
use Math::BigFloat;
package Foo;
use Moo::Role;
use MooX::ClassAttribute;
class_has 'foo' => (
is => 'ro',
isa => sub { $_[0] == 1 or die 'not 1' }
);
@rsimoes
rsimoes / set-builder_synopsis.pl
Created July 14, 2012 01:52
Set::Builder synopsis
use Set::Builder qw(Set Var for_all);
# set constructor, roster style:
my $set = Set { 0..9 };
# element variable constructor:
my ($x, $y) = (Var) x 2;
# subset consisting of the numbers 0 and 1
my $subset1 = Set { $x ~~ $set | $x ~~ Set { 0..1 } };
@rsimoes
rsimoes / lazy_io_var.pl
Created March 10, 2012 05:49
Proof of concept: Lazy IO variable with AnyEvent, Variable::Magic, Data::Alias, and PadWalker
#!/usr/bin/env perl
use v5.14;
use warnings;
use AnyEvent::HTTP "http_get";
use Variable::Magic qw(wizard cast dispell);
use Data::Alias "alias";
use PadWalker "peek_my";
sub get_page {
@rsimoes
rsimoes / linearly-typed.pl
Created February 22, 2012 14:48
Long-winded linearly typed variables
#!/usr/bin/env perl
use strict;
use warnings;
use Data::Dump;
use Data::Alias "alias";
use Scalar::Util qw(weaken isweak);
my $foo = ["a".."z"];
alias my @bar = @$foo;
@rsimoes
rsimoes / net-gnutls-low-level.pl
Created February 20, 2012 17:30
Net::GnuTLS low-level bindings
use Socket qw(getaddrinfo SHUT_RDWR SOCK_STREAM);
use Net::GnuTLS qw(GNUTLS_CLIENT GNUTLS_CRD_CERTIFICATE GNUTLS_SHUT_RDWR
GNUTLS_X509_FMT_PEM);
my %hints = ( socktype => SOCK_STREAM );
my ( $err, $addr_info ) = getaddrinfo( "metaperl.org", "echo", \%hints );
socket( my $SKT, @{$addr_info}{qw(family type protocol)} ) || die "socket: $!";
connect( $SKT, $addr_info->{addr} ) || die "connect: $!";
my $creds = Net::GnuTLS::Certificate::allocate_credentials();
@rsimoes
rsimoes / net-gnutls-high-level.pl
Created February 20, 2012 17:31
Net::GnuTLS high-level bindings
use IO::Socket::IP;
use Socket qw(SOCK_STREAM)
use Net::GnuTLS::Client;
use Net::GnuTLS::Credentials;
# Connect to server:
my $socket = IO::Socket::IP->new(
PeerHost => "metacpan.org",
PeerPort => "https",
@rsimoes
rsimoes / xml_parse_bench.pl
Created February 18, 2012 02:22
XML parsing benchmarks
use v5.14;
use strict;
use warnings;
use utf8::all;
use Data::Dump;
use LWP::Simple qw(get);
use Benchmark qw(cmpthese timethese);
use XML::LibXML;
use XML::Parser;
use XML::SAX::PurePerl;
@rsimoes
rsimoes / moose_defaults.pod
Created January 2, 2012 12:05
Moose defaults vs builders

Defaults allow subclassing

If we subclass our Person class, we can override the size attribute's default:

package Lilliputian;
 
use Moose;
extends 'Person';
 
has +size => ( default => "small" );
@rsimoes
rsimoes / concapl.pl
Created December 30, 2011 20:15
Concatenative, stack-oriented Perl DSL
"What is 2 + 3?" . say . readline
. { 2 . 3 . add . eqn }
. { "Correct!" . say } . { "Incorrect" . say }
. ifte