<commit_before>use v6;

# Test closure traits in eval strings

use Test;

plan 27;

# L<S04/Closure traits/Code "generated at run time" "still fire off"
#   "can't" "travel back in time" >

my ($handle);

our $h;

{
    my $h;

    eval '$handle = { $h ~= "1"; START { $h ~= "F" }; $h ~= "2" }';
    ok $! !~~ Exception, 'eval START {...} works';

    ok $h ~~ undef, 'START {...} has not run yet';
    lives_ok { $handle() }, 'can run code with START block';
    is $h, '1F2', 'START {...} fired';
    lives_ok { $handle() }, 'can run code with START block again';
    is $h, '1F212', 'START {...} fired only once';

    # TODO: test that it runs again for a clone of $handle
}

{
    my $h;

    eval '$handle = { $h =~ "r"; INIT { $h ~= "I" }; $h ~= "R" }';
    ok $! !~~ Exception, 'eval INIT {...} works';
    ok $h ~~ undef, 'INIT did not run at compile time';
    #?rakudo 4 todo 'Could not find non-existent sub INIT'
    lives_ok { $handle() }, 'can run code with INIT block';<commit_msg>[t/spec] clone of code with START block<commit_after>use v6;

# Test closure traits in eval strings

use Test;

plan 31;

# L<S04/Closure traits/Code "generated at run time" "still fire off"
#   "can't" "travel back in time" >

my ($handle);

our $h;

{
    my $h;

    eval '$handle = { $h ~= "1"; START { $h ~= "F" }; $h ~= "2" }';
    ok $! !~~ Exception, 'eval START {...} works';

    ok $h ~~ undef, 'START {...} has not run yet';
    lives_ok { $handle() }, 'can run code with START block';
    is $h, '1F2', 'START {...} fired';
    lives_ok { $handle() }, 'can run code with START block again';
    is $h, '1F212', 'START {...} fired only once';

    # test that it runs again for a clone of $handle
    my $start_clone = $handle.clone;
    lives_ok { $start_clone() }, 'can run clone of code with START block';
    is $h, '1F2121F2', 'START {...} fired again for the clone';
    lives_ok { $start_clone() }, 'can run clone of START block code again';
    is $h, '1F2121F212', 'cloned START {...} fired only once';
}

{
    my $h;

    eval '$handle = { $h =~ "r"; INIT { $h ~= "I" }; $h ~= "R" }';
    ok $! !~~ Exception, 'eval INIT {...} works';
    ok $h ~~ undef, 'INIT did not run at compile time';
    #?rakudo 4 todo 'Could not find non-existent sub INIT'
    lives_ok { $handle() }, 'can run code with INIT block';
{'fraction_non_alphanumeric': 0.1893516530699871, 'fraction_numerical': 0.018033490768570203, 'mean_word_length': 2.764135702746365, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6483575', 'n_tokens_mistral': 894, 'n_tokens_neox': 860, 'n_words': 318}