Skip to content

Instantly share code, notes, and snippets.

@smls
smls / gist:8693027
Last active Aug 29, 2015
Variables/containers/objects in Perl vs Perl 6
View gist:8693027
## Storing three integers in a variable in Perl 5 ##
my $a = [2, 4, 6]; # a scalar variable aliased to a reference to an ARRAY object containing three SCALAR objects
my @a = (2, 4, 6); # an array variable aliased to an ARRAY object containing three SCALAR objects
## Storing three integers in a variable in Perl 6 ##
my $a = (2, 4, 6); # an item variable bound to a Scalar object containing one Parcel object containing three Int objects
my $a = list 2, 4, 6; # an item variable bound to a Scalar object containing one List object containing three Int objects
View Simple Perl 6 implementation of Text::Conjunct's conjunct()
multi conjunct ($, *[]) { '' }
multi conjunct ($, $word) { $word }
multi conjunct ($conj, *@words) { "{@words[0..*-2].join: ', '} $conj @words[*-1]" }
say conjunct "and";
say conjunct "and", "one apple";
say conjunct "and", "one apple", "two oranges";
say conjunct "and", "one apple", "two oranges", "three pears";
View fraction-frenzy-debug-output.txt
Finding FF(2)...
step 2: 1/3 + 1/4 + 1/5 + 1/9 + 2/15 + 1/25 + 1/-30 + 1/-45 + 1/-75 + 1/900
step 5: 1/3 + 1/4 + 1/5 + 1/9 + 1/15 + 1/25 + 1/30 + 1/-30 + 1/-45 + 1/45 + 1/75 + 1/-75 + 1/-450 + 1/900
step 4: 1/3 + 1/4 + 1/5 + 1/9 + 1/15 + 1/25 + 1/-450 + 1/900
FF(2) = [3 4 5 9 15 25 -450 900]
View perl6_binary_parsing.md

Binary Parsing in Perl 6

When parsing things like binary file format headers, you generally need to know three things for each data field:

  • how many, and what kind of, bytes to gobble
    (e.g. input stream --> Buf:0x<0b 00>)
  • how to unpack those bytes into a number, or other low-level type
    (e.g. Buf:0x<0b 00> --> 11)
  • how to transform that number into meaningful data