Permalink
Browse files

Perlito5 - javascript - add bug: "return" and context inside eval

  • Loading branch information...
fglock committed Sep 7, 2012
1 parent 966c1c8 commit 3b69c4eedab47629fb693abe4c9cc9db26b2316c
Showing with 91 additions and 0 deletions.
  1. +23 −0 TODO-perlito5
  2. +68 −0 eval-test.pl
View
@@ -142,6 +142,29 @@ TODO list for Perlito5
* Javascript backend
+-- verify "return" behaviour inside eval (Luciano Rocha++):
+
+ $ perl -le 'sub foo { my $x = eval { return "bar" }; return "baz-$x" }; my $bar = foo(); warn ( foo() )'
+ baz-bar at -e line 1.
+ $ perl -le 'sub foo { my $x = eval qq{ return "bar" }; return "baz-$x" }; my $bar = foo(); warn ( foo() )'
+ baz-bar at -e line 1.
+
+-- "or" has SCALAR context (Abigail++):
+
+ $ node perlito5.js -Isrc5/lib -e '
+
+ eval {
+ my $result = wantarray ? "LIST" : defined wantarray ? "SCALAR" : "VOID";
+ say "Eval block: $result";
+ 1;
+ } or do {warn "Oops"};
+
+
+ '
+ Eval block: VOID # should be SCALAR
+
+ See: file "eval-test.pl"
+
-- "~~" operator not implemented; See also "when" implementation
-- "when" should use a "break" exception inside "given", and a "next" exception inside "for".
View
@@ -0,0 +1,68 @@
+# this code from Abigail++
+
+use feature 'say';
+
+eval {
+ my $result = wantarray ? "LIST" : defined wantarray ? "SCALAR" : "VOID";
+ say "Eval block: $result";
+ 1;
+} or do {warn "Oops"};
+
+
+eval <<'--' or do {warn "Oops"};
+ my $result = wantarray ? "LIST" : defined wantarray ? "SCALAR" : "VOID";
+ say "Eval string: $result";
+ 1;
+--
+
+
+sub foo {
+ my $result = wantarray ? "LIST" : defined wantarray ? "SCALAR" : "VOID";
+ say "Sub: $result";
+ 1;
+}
+
+foo or do {warn "Oops"};
+
+
+sub {
+ my $result = wantarray ? "LIST" : defined wantarray ? "SCALAR" : "VOID";
+ say "Anon: $result";
+ 1;
+} -> () or do {warn "Oops"};
+
+
+
+# Now, note that if the eval is in list context, wantarray pick that up:
+
+
+sub foo {1;}
+
+foo (eval {
+ my $result = wantarray ? "LIST" : defined wantarray ? "SCALAR" : "VOID";
+ say "Eval: $result";
+ 1;
+});
+
+
+
+# And if the eval is in void context, wantarray picks that up as well:
+
+eval {
+ my $result = wantarray ? "LIST" : defined wantarray ? "SCALAR" : "VOID";
+ say "Eval: $result";
+ 1;
+};
+
+
+
+
+__END__
+Eval block: SCALAR
+Eval string: SCALAR
+Sub: SCALAR
+Anon: SCALAR
+
+Eval block: LIST
+
+Eval: VOID

0 comments on commit 3b69c4e

Please sign in to comment.