Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 85 lines (62 sloc) 2.322 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
use v6;

# Test control blocks (BEGIN/CHECK/INIT/END) used as rvalues
# [TODO] add tests for other control blocks

use Test;

plan 16;

# L<S04/Phasers/"marked with a *" "used within" expression>

{
    my $x = BEGIN { 8 };
    is $x, 8, 'BEGIN block as expression';

    # test that built-ins are available within a BEGIN block:
    my $y = BEGIN { uc 'moin' };
    is $y, 'MOIN', 'can access built-in functions in BEGIN blocks';

    my $z = BEGIN { 'moin'.uc };
    is $z, 'MOIN', 'can access built-in methods in BEGIN blocks';
}

#?pugs skip 'No such subroutine: "&BEGIN"'
{
    my $x = BEGIN 8;
    is $x, 8, 'BEGIN statement prefix as expression';

    # test that built-ins are available within a BEGIN block:
    my $y = BEGIN uc 'moin';
    is $y, 'MOIN', 'can access built-in functions in BEGIN statement prefix';

    my $z = BEGIN 'moin'.uc;
    is $z, 'MOIN', 'can access built-in methods in BEGIN statement prefix';
}

{
    my $hist; # DO NOT INITIALIZE HERE! (my runs after these blocks)

    # Test INIT {} as rval:

    my $init_val;
    my $init = {
        $init_val = INIT { '' ~ ($hist ~= 'I') };
    }

    #?niecza todo 'block returns no value'
    is $init(), 'BCI', 'INIT {} runs only once';
    #?niecza todo 'block returns no value'
    is $init_val, 'BCI', 'INIT {} as rval is its ret val';
    #?niecza todo 'block returns no value'
    is $init(), 'BCI', 'INIT {} runs only once';

    # Test CHECK {} as rval:

    my $check_val;
    my $check = {
        $check_val = CHECK { '' ~ ($hist ~= 'C') };
    }

    #?niecza todo 'block returns no value'
    is $check(), 'BC', 'CHECK {} runs only once';
    #?niecza todo 'block returns no value'
    is $check_val, 'BC', 'CHECK {} as rval is its ret val';
    #?niecza todo 'block returns no value'
    is $check(), 'BC', 'CHECK {} runs only once';

    # Test BEGIN {} as rval:

    my $begin_val;
    my $begin = {
        $begin_val = BEGIN { '' ~ ($hist ~= 'B') };
    }

    is $begin(), 'B', 'BEGIN {} runs only once';
    is $begin_val, 'B', 'BEGIN {} as rval is its ret val';
    is $begin(), 'B', 'BEGIN {} runs only once';

    # Test END {} as rval:
    #?niecza skip 'Excess arguments to EVAL, used 1 of 2 positionals'
    ok !EVAL('my $end_val = END { 3 }'), "END {} can't be used as a rvalue";
}

# vim: ft=perl6 expandtab sw=4
Something went wrong with that request. Please try again.