use v6;
use Test;
plan 77;
sub bare_return { return };
ok(! bare_return(), "A bare return is a false value");
my @l = <some values>;
@l = bare_return();
is( @l, [], "A bare return is an empty list in array/list context");
my $s = "hello";
$s = bare_return();
ok($s.notdef, "A bare return is undefined in scalar context");
sub foo_scalar {
my $foo = 'foo';
return $foo;
}
is(foo_scalar(), 'foo', 'got the right return value');
sub foo_scalar2 {
my $foo = 'foo';
$foo;
}
is(foo_scalar2(), 'foo', 'got the right return value');
sub foo_scalar3 {
return 'foo';
}
is(foo_scalar3(), 'foo', 'got the right return value');
sub foo_scalar4 {
'foo';
}
is(foo_scalar4(), 'foo', 'got the right return value');
sub foo_array {
my @foo = ('foo', 'bar', 'baz');
return @foo;
}
my @foo_array_return = foo_array();
isa_ok(@foo_array_return, Array);
is(+@foo_array_return, 3, 'got the right number of return value');
is(@foo_array_return[0], 'foo', 'got the right return value');
is(@foo_array_return[1], 'bar', 'got the right return value');
is(@foo_array_return[2], 'baz', 'got the right return value');
sub foo_array2 {
my @foo = ('foo', 'bar', 'baz');
@foo;
}
my @foo_array_return2 = foo_array2();
isa_ok(@foo_array_return2, Array);
is(+@foo_array_return2, 3, 'got the right number of return value');
is(@foo_array_return2[0], 'foo', 'got the right return value');
is(@foo_array_return2[1], 'bar', 'got the right return value');
is(@foo_array_return2[2], 'baz', 'got the right return value');
sub foo_array3 {
return ('foo', 'bar', 'baz');
}
my @foo_array_return3 = foo_array3();
isa_ok(@foo_array_return3, Array);
is(+@foo_array_return3, 3, 'got the right number of return value');
is(@foo_array_return3[0], 'foo', 'got the right return value');
is(@foo_array_return3[1], 'bar', 'got the right return value');
is(@foo_array_return3[2], 'baz', 'got the right return value');
sub foo_array4 {
('foo', 'bar', 'baz');
}
my @foo_array_return4 = foo_array4();
isa_ok(@foo_array_return4, Array);
is(+@foo_array_return4, 3, 'got the right number of return value');
is(@foo_array_return4[0], 'foo', 'got the right return value');
is(@foo_array_return4[1], 'bar', 'got the right return value');
is(@foo_array_return4[2], 'baz', 'got the right return value');
sub foo_array_ref {
my $foo = ['foo', 'bar', 'baz'];
return $foo;
}
my $foo_array_ref_return = foo_array_ref();
isa_ok($foo_array_ref_return, Array);
is(+$foo_array_ref_return, 3, 'got the right number of return value');
is($foo_array_ref_return[0], 'foo', 'got the right return value');
is($foo_array_ref_return[1], 'bar', 'got the right return value');
is($foo_array_ref_return[2], 'baz', 'got the right return value');
sub foo_array_ref2 {
my $foo = ['foo', 'bar', 'baz'];
$foo;
}
my $foo_array_ref_return2 = foo_array_ref2();
isa_ok($foo_array_ref_return2, Array);
is(+$foo_array_ref_return2, 3, 'got the right number of return value');
is($foo_array_ref_return2[0], 'foo', 'got the right return value');
is($foo_array_ref_return2[1], 'bar', 'got the right return value');
is($foo_array_ref_return2[2], 'baz', 'got the right return value');
sub foo_array_ref3 {
return ['foo', 'bar', 'baz'];
}
my $foo_array_ref_return3 = foo_array_ref3();
isa_ok($foo_array_ref_return3, Array);
is(+$foo_array_ref_return3, 3, 'got the right number of return value');
is($foo_array_ref_return3[0], 'foo', 'got the right return value');
is($foo_array_ref_return3[1], 'bar', 'got the right return value');
is($foo_array_ref_return3[2], 'baz', 'got the right return value');
sub foo_array_ref4 {
['foo', 'bar', 'baz'];
}
my $foo_array_ref_return4 = foo_array_ref4();
isa_ok($foo_array_ref_return4, Array);
is(+$foo_array_ref_return4, 3, 'got the right number of return value');
is($foo_array_ref_return4[0], 'foo', 'got the right return value');
is($foo_array_ref_return4[1], 'bar', 'got the right return value');
is($foo_array_ref_return4[2], 'baz', 'got the right return value');
sub foo_hash {
my %foo = ('foo', 1, 'bar', 2, 'baz', 3);
return %foo;
}
my %foo_hash_return = foo_hash();
ok(%foo_hash_return ~~ Hash);
is(+%foo_hash_return.keys, 3, 'got the right number of return value');
is(%foo_hash_return<foo>, 1, 'got the right return value');
is(%foo_hash_return<bar>, 2, 'got the right return value');
is(%foo_hash_return<baz>, 3, 'got the right return value');
my $keys;
lives_ok({ $keys = +(foo_hash().keys) },
"can call method on return value (hashref)");
is($keys, 3, "got right result");
lives_ok({ foo_hash()<foo> },
"can hash de-ref return value (hashref)");
sub foo_hash_ref {
my $foo = { 'foo' => 1, 'bar' => 2, 'baz' => 3 };
return $foo;
}
my $foo_hash_ref_return = foo_hash_ref();
ok($foo_hash_ref_return ~~ Hash);
is(+$foo_hash_ref_return.keys, 3, 'got the right number of return value');
is($foo_hash_ref_return<foo>, 1, 'got the right return value');
is($foo_hash_ref_return<bar>, 2, 'got the right return value');
is($foo_hash_ref_return<baz>, 3, 'got the right return value');
lives_ok({ $keys = +(foo_hash_ref().keys) },
"can call method on return value (hashref)");
is($keys, 3, "got right result");
lives_ok({ foo_hash_ref()<foo> },
"can hash de-ref return value (hashref)");
{
sub userdefinedcontrol_a (&block) { block(); return 24 }
sub official_a {
userdefinedcontrol_a { return 42 };
}
is official_a(), 42, "bare blocks are invisible to return";
}
{
sub userdefinedcontrol_b (&block) { block(); return 24 }
sub official_b {
{
{
userdefinedcontrol_b { return 42 };
}
}
}
is official_b(), 42, "nested bare blocks are invisible to return";
}
{
sub userdefinedcontrol_c ($value, &block) { block($value); return 24 }
sub official_c($value) {
{
userdefinedcontrol_c $value, -> $x { return $x };
}
}
is official_c(42), 42, "pointy blocks are invisible to return";
}
{
sub userdefinedcontrol3 (&block) { block(); return 36 }
sub userdefinedcontrol2 (&block) { userdefinedcontrol3(&block); return 24 }
sub userdefinedcontrol1 (&block) { userdefinedcontrol2(&block); return 12 }
sub official_d {
userdefinedcontrol1 { return 42 };
}
is official_d(), 42,
"subcalls in user-defined control flow are invisible to return";
}
class Foo {
method userdefinedcontrol3 (&block) { block(); 36 }
submethod userdefinedcontrol2 (&block) { self.userdefinedcontrol3(&block); 24 }
method userdefinedcontrol1 (&block) { self.userdefinedcontrol2(&block); 12 }
method officialmeth {
self.userdefinedcontrol1({ return 42 });
}
submethod officialsubmeth {
self.userdefinedcontrol1({ return 43 });
}
our sub official {
Foo.new.userdefinedcontrol1({ return 44 });
}
}
is Foo.new.officialmeth(), 42,
"return correctly from official method only";
is Foo.new.officialsubmeth(), 43,
"return correctly from official submethod only";
is Foo::official(), 44,
"return correctly from official sub only";
{
sub named() {
return 1, 2, :c(3);
}
is named().elems, 3, 'return with named arguments';
is named().[2].key, 'c', ' ... correct key';
is named().[2].value, '3', ' ... correct value';
}
{
sub rt61732_c { 1; CATCH {} }
is rt61732_c(), 1, 'sub with empty catch block returns value before block';
}
{
sub rt61732_d { 1;; }
is rt61732_d(), 1, 'get right value from sub with double ;';
}
{
sub rt63912 { return 1, 2; }
lives_ok { rt63912() }, 'can call sub that returns two things (no parens)';
}
{
class RT72836 {
method new() { }
}
lives_ok {my $c = RT72836.new},
'can use value returned from empty routine';
}