Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BEGIN blocks don't parse as expressions in Rakudo #608

Closed
p6rt opened this issue Jan 11, 2009 · 6 comments
Closed

BEGIN blocks don't parse as expressions in Rakudo #608

p6rt opened this issue Jan 11, 2009 · 6 comments

Comments

@p6rt
Copy link

p6rt commented Jan 11, 2009

Migrated from rt.perl.org#62188 (status was 'resolved')

Searchable as RT62188$

@p6rt
Copy link
Author

p6rt commented Jan 11, 2009

From @masak

I'm toying around with precompilation in Rakudo r35404.

I can print the value of a 'time' call directly.

$ ./perl6 -e 'say time' # works fine
1231679122.805

But I don't seem to be able to put the 'time' call in a BEGIN block,
compile it, and then print the result of the block at runtime.

$ ./perl6 --target=PIR --output=precompiled.pir -e 'my $a = do BEGIN {
time }; say $a'
$ ../../parrot precompiled.pir # doesn't work fine
_block27

@p6rt
Copy link
Author

p6rt commented Jan 11, 2009

From @masak

Rakudo r35404​:

$ ./perl6 -e 'BEGIN { say "OH HAI" }' # works
OH HAI
$ ./perl6 -e 'BEGIN { 5 }' # works
$ ./perl6 -e 'my $a = BEGIN { 5 }' # worksn't, but should acc. to S04
Statement not terminated properly at line 1, near "{ 5 }"
[...]
$ ./perl6 -e 'my $a = do BEGIN { 5 }' # parses (but see #​62186)

@p6rt
Copy link
Author

p6rt commented Jul 14, 2009

From @kyleha

This is tested in S04-closure-traits/rvalue.t

@p6rt
Copy link
Author

p6rt commented May 8, 2010

From @kyleha

This is an automatically generated mail to inform you that tests are now available in t/spec/S04-phasers/rvalue.t

commit 4e1049963df253c5287810a4c579541fd55f16d6
Author​: moritz <moritz@​c213334d-75ef-0310-aa23-eaa082d1ae64>
Date​: Sat May 8 09​:01​:01 2010 +0000

  [t/spec] simpler tests for BEGIN blocks as expressions; tests RT #​62188 and RT #​74836
 
  git-svn-id​: http://svn.pugscode.org/pugs@&#8203;30585 c213334d-75ef-0310-aa23-eaa082d1ae64

Inline Patch
diff --git a/t/spec/S04-phasers/rvalue.t b/t/spec/S04-phasers/rvalue.t
index 877b069..55ca63a 100644
--- a/t/spec/S04-phasers/rvalue.t
+++ b/t/spec/S04-phasers/rvalue.t
@@ -5,47 +5,74 @@ use v6;
 
 use Test;
 
-plan 10;
+plan 16;
 
 # L<S04/Phasers/"marked with a *" "used within" expression>
 
-my $hist = '';
+{
+    my $x = BEGIN { 8 };
+    is $x, 8, 'BEGIN block as expression';
 
-# Test INIT {} as rval:
+    # test that built-ins are available within a BEGIN block:
+    my $y = BEGIN { ucfirst 'moin' };
+    is $y, 'Moin', 'can access built-in functions in BEGIN blocks';
 
-my $init_val;
-my $init = {
-    $init_val = INIT { $hist ~= 'I' };
+    my $z = BEGIN { 'moin'.ucfirst };
+    is $z, 'Moin', 'can access built-in methods in BEGIN blocks';
 }
 
-is $init(), 'BCI', 'INIT {} runs only once';
-is $init_val, 'BCI', 'INIT {} as rval is its ret val';
-is $init(), 'BCI', 'INIT {} runs only once';
+{
+    my $x = BEGIN 8;
+    is $x, 8, 'BEGIN statement prefix as expression';
 
-# Test CHECK {} as rval:
+    # test that built-ins are available within a BEGIN block:
+    my $y = BEGIN ucfirst 'moin';
+    is $y, 'Moin', 'can access built-in functions in BEGIN statement prefix';
 
-my $check_val;
-my $check = {
-    $check_val = CHECK { $hist ~= 'C' };
+    my $z = BEGIN 'moin'.ucfirst;
+    is $z, 'Moin', 'can access built-in methods in BEGIN statement prefix';
 }
 
-is $check(), 'BC', 'CHECK {} runs only once';
-is $check_val, 'BC', 'CHECK {} as rval is its ret val';
-is $check(), 'BC', 'CHECK {} runs only once';
+#?rakudo skip 'lexicals in phasers'
+{
+    my $hist = '';
 
-# Test BEGIN {} as rval:
+    # Test INIT {} as rval:
 
-my $begin_val;
-my $begin = {
-    $begin_val = BEGIN { $hist ~= 'B' };
-}
+    my $init_val;
+    my $init = {
+        $init_val = INIT { $hist ~= 'I' };
+    }
+
+    is $init(), 'BCI', 'INIT {} runs only once';
+    is $init_val, 'BCI', 'INIT {} as rval is its ret val';
+    is $init(), 'BCI', 'INIT {} runs only once';
+
+    # Test CHECK {} as rval:
+
+    my $check_val;
+    my $check = {
+        $check_val = CHECK { $hist ~= 'C' };
+    }
 
-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';
+    is $check(), 'BC', 'CHECK {} runs only once';
+    is $check_val, 'BC', 'CHECK {} as rval is its ret val';
+    is $check(), 'BC', 'CHECK {} runs only once';
 
-# Test END {} as rval:
+    # Test BEGIN {} as rval:
 
-ok !eval 'my $end_val = END { 3 }', "END {} can't be used as a rvalue";
+    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:
+
+    ok !eval 'my $end_val = END { 3 }', "END {} can't be used as a rvalue";
+}
 
 # vim: ft=perl6

@p6rt
Copy link
Author

p6rt commented May 8, 2010

The RT System itself - Status changed from 'new' to 'open'

@p6rt
Copy link
Author

p6rt commented May 8, 2010

@moritz - Status changed from 'open' to 'resolved'

@p6rt p6rt closed this as completed May 8, 2010
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant