Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
adding 2011 adventy day 16
  • Loading branch information
dwarring committed Jun 17, 2014
1 parent cf1c647 commit 1e909c2
Showing 1 changed file with 84 additions and 0 deletions.
84 changes: 84 additions & 0 deletions integration/advent2011-day16.t
@@ -0,0 +1,84 @@
# http://perl6advent.wordpress.com/2011/12/16/where-have-all-the-references-gone/
use v6;
use Test;
plan 11;

class My::Class {
# attribute with accessor (indicated by the dot)
# and default value
has $.foo = 5;

method new() {
# the * specifies the storage, and means "default storage"
pass 'new method called';
self.bless();
}
}
# use it:
is My::Class.new.foo, 5, 'new object';

my @a = (1, 2);
my @b = (3, 4);

{
temp @a;
push @a, @b;
is_deeply @a, [1, 2, 3, 4], 'automatic list flattening';
}

{
temp @a;
my $temp = @b;
push @a, $temp; # does not flatten the two items in $temp,
# because $temp is a scalar

is_deeply @a, [1, 2, [3, 4]], 'item non-flattening';
}

{
temp @a;
push @a, $(@b);
is_deeply @a, [1, 2, [3, 4]], 'item non-flattening';
}

{
temp @a;
push @a, item @b;
is_deeply @a, [1, 2, [3, 4]], 'item non-flattening';
}

{
my @a = 1, 2;
my $scalar = @a;

is +(gather for @a {
# two iterations
take 1;
}), 2, 'for @a { }';

is +(gather for $scalar {
# one iteration only
take 1;
}), 1, 'for $scalar { }';

is +(gather for @$scalar {
# two iterations again
take 1;
}), 2, 'for @$scalar { }';
}

{
my @a = 1, 2, 3, 4, 5;
my @b = 10, 20, 30;
@a[3] = @b;
is_deeply @a, [1, 2, 3, [10, 20, 30], 5], 'item context';
}

sub set_five($x is rw) {
# no dereferencing
$x = 5;
}
my $var;
# no explicit reference taking
set_five $var;
is $var, 5,'reference taking';

0 comments on commit 1e909c2

Please sign in to comment.