Skip to content

Instantly share code, notes, and snippets.

View manchicken's full-sized avatar
🦀
I pinch.

manchicken manchicken

🦀
I pinch.
View GitHub Profile
@manchicken
manchicken / anonymous_sub.pl
Last active December 22, 2015 02:08
Anonymous Subroutine
my $anonsub = sub { return join ',', @_; };
$anonsub->(1,2,3,4);
@manchicken
manchicken / simple_closure.pl
Last active December 22, 2015 02:08
Here's a simple closure
sub make_closure {
my @inputs = @_;
return sub { join ',', @inputs, @_; };
}
my $closure = make_closure(1,2,3,4);
say $closure->('X');
undef $closure;
@manchicken
manchicken / closure_demo2.pl
Created August 31, 2013 22:20
Part two of the closure demonstration, using DestroyDetector to prove when the variables are reaped.
# First, let's prove that DestroyDetector announces when undef'ed.
my $foo1 = DestroyDetector->new('foo1');
$foo1->announce("about to undef foo1");
undef($foo1);
# Now, let's define a function which returns a closure...
sub closure_one {
my ($value) = @_;
my $detector = DestroyDetector->new($value);
@manchicken
manchicken / perl_closures.pl
Last active December 22, 2015 02:08
From my Perl closures blog post
#!/usr/bin/env perl
use 5.010; # Perl 5.10 minimum!
use strict;use warnings;
# This is just to demonstrate a simple closure
sub make_closure {
my @inputs = @_;
return sub { join ',', @inputs, @_; };
}
@manchicken
manchicken / UseOrRequire.pm
Last active December 22, 2015 03:09
Demo for use versus require
use strict;use warnings;
require 5.010;
package UseOrRequire;
sub INIT {
say 'INIT CALLED!';
}
sub BEGIN {
@manchicken
manchicken / Animal.pm
Last active December 22, 2015 04:29
Using Roles in Moose
package Animal;
use Moose;
has 'name' => (is=>'rw',isa=>'Str');
sub say_my_name {
my ($self) = @_;
say $self->name;
}
@manchicken
manchicken / build.sh
Last active December 22, 2015 06:19
CUnit Demo
#!/bin/bash -x
gcc cunit_demo.c -lc -lcunit -L"/usr/local/lib" -o cunit_demo
@manchicken
manchicken / BinSearch.pm
Last active December 22, 2015 08:09
The Inline::C demo using a binary search in C from Perl
# /*
use strict;
use warnings;
use 5.010;
package BinSearch;
use Inline C => 'DATA' => (LIBS=>'m');
Inline->init;
@manchicken
manchicken / binsearch_with_sort.pl
Created September 6, 2013 21:52
A sample on how to benchmark to test the performance of one thing over another.
sub binsearch_with_sort {
my ($needle, $haystack) = @_;
my $sorted = [sort @{$haystack}];
my $left = 0;
my $right = scalar(@{$sorted});
my $look = undef;
if ($right <= 1) {
@manchicken
manchicken / ends_in_four.pl
Created September 7, 2013 22:56
Sorry, today's gist is a little simple, but sometimes simple is important.
#!/usr/bin/env perl
use strict; use warnings;
if (scalar(@ARGV) != 1) {
die "Usage: $0 INTEGER";
}
my $in = $ARGV[0];
if ($in !~ m/^-?[0-9]+$/) {
die "Usage: $0 INTEGER (and it really must be an integer)";