Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

137 lines (112 sloc) 2.795 kb
use v6;
use Test;
plan 24;
# L<S04/"Conditional statements"/Conditional statement modifiers work as in Perl 5>
# test the for statement modifier
{
my $a = '';
$a ~= $_ for ('a', 'b', 'a', 'b', 'a');
is($a, "ababa", "post for with parens");
}
# without parens
{
my $a = '';
$a ~= $_ for 'a', 'b', 'a', 'b', 'a';
is($a, "ababa", "post for without parens");
}
{
my $a = 0;
$a += $_ for (1 .. 10);
is($a, 55, "post for 1 .. 10 with parens");
}
{
my $a = 0;
$a += $_ for 1 .. 10;
is($a, 55, "post for 1 .. 10 without parens");
}
{
my @a = (5, 7, 9);
my $a = 3;
$a *= $_ for @a;
is($a, 3 * 5 * 7 * 9, "post for array");
}
{
my @a = (5, 7, 9);
my $i = 5;
my sub check(Int $n){
is($n, $i, "sub Int with post for");
$i += 2;
}
check $_ for @a;
}
{
my $a = "";
$a ~= "<$_>" for "hello";
is $a, "<hello>", 'iterating one constant element works';
}
{
my $a = ""; my $v = "hello";
$a ~= "<$_>" for $v;
is $a, "<hello>", 'iterating one variable element works';
}
#?niecza todo "closure for"
{
my $a = 0;
{ $a++ } for 1..3;
is $a, 3, 'the closure was called';
}
#?niecza todo "closure for"
{
my $a = 0;
-> $i { $a += $i } for 1..3;
is $a, 6, 'the closure was called';
}
# L<S04/The C<for> statement/for and given privately temporize>
{
my $i = 0;
$_ = 10;
$i += $_ for 1..3;
is $_, 10, 'outer $_ did not get updated in lhs of for';
is $i, 1+2+3, 'postfix for worked';
}
# L<S04/The C<for> statement/When used as statement modifiers on implicit blocks>
{
$_ = 42;
my @trace;
@trace.push: $_ for 2, 3;
is @trace.join(':'), '2:3', 'statement modifier "for" sets $_ correctl';
is $_, 42, '"for" statement modifier restored $_ of outer block';
}
# RT 66622
{
my $rt66622 = 66622 for 1, 2, 3;
is $rt66622, 66622, 'statement modifier "for" makes no implicit block';
}
eval_dies_ok '1 for <a b> for <c d>;', 'double statement-modifying for is not allowed';
# RT #66606
{
my $x = 1 for ^3;
is $x, 1;
(my @a).push: $_ for ^3;
is @a.join(','), '0,1,2';
}
# RT #89208
is ((sub r { "OH HAI" })() for 5), "OH HAI", 'Anon sub in statement modifier for works.';
# RT #118769
{
my @x = <x x x>;
$_ = 'foo' for @x;
is @x, <foo foo foo>, 'can assign to $_ in a statement_mod "for" loop (1)';
my @y = <& a& &b>;
s:g/\&/\\\&/ for @y;
is @y, ('\&', 'a\&', '\&b'), 'can assign to $_ in a statement_mod "for" loop (2)';
}
# RT #100746
#?rakudo.moar todo "RT #100746"
#?rakudo.jvm todo "RT #100746"
{
$_ = 'bogus';
my @r = gather { take "{$_}" for <cool but dry> }
is @r[0], 'cool', 'for modifies the $_ that is visible to the {} interpolator';
}
# vim: ft=perl6
Jump to Line
Something went wrong with that request. Please try again.