This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
force copy of substrings when matching against temporaries
[perl5.git] / t / op / eval.t
CommitLineData
a559c259
LW
1#!./perl
2
79072805 3# $RCSfile: eval.t,v $$Revision: 4.1 $$Date: 92/08/07 18:27:48 $
378cc40b 4
fc360e46 5print "1..23\n";
a559c259
LW
6
7eval 'print "ok 1\n";';
8
9if ($@ eq '') {print "ok 2\n";} else {print "not ok 2\n";}
10
11eval "\$foo\n = # this is a comment\n'ok 3';";
12print $foo,"\n";
13
14eval "\$foo\n = # this is a comment\n'ok 4\n';";
15print $foo;
16
378cc40b 17print eval '
79072805 18$foo =;'; # this tests for a call through yyerror()
a559c259
LW
19if ($@ =~ /line 2/) {print "ok 5\n";} else {print "not ok 5\n";}
20
378cc40b 21print eval '$foo = /'; # this tests for a call through fatal()
a559c259 22if ($@ =~ /Search/) {print "ok 6\n";} else {print "not ok 6\n";}
378cc40b
LW
23
24print eval '"ok 7\n";';
25
26# calculate a factorial with recursive evals
27
28$foo = 5;
29$fact = 'if ($foo <= 1) {1;} else {push(@x,$foo--); (eval $fact) * pop(@x);}';
30$ans = eval $fact;
31if ($ans == 120) {print "ok 8\n";} else {print "not ok 8\n";}
32
33$foo = 5;
a687059c 34$fact = 'local($foo)=$foo; $foo <= 1 ? 1 : $foo-- * (eval $fact);';
378cc40b
LW
35$ans = eval $fact;
36if ($ans == 120) {print "ok 9\n";} else {print "not ok 9 $ans\n";}
37
38open(try,'>Op.eval');
39print try 'print "ok 10\n"; unlink "Op.eval";',"\n";
40close try;
41
42do 'Op.eval'; print $@;
99b89507
LW
43
44# Test the singlequoted eval optimizer
45
46$i = 11;
47for (1..3) {
48 eval 'print "ok ", $i++, "\n"';
49}
50
51eval {
52 print "ok 14\n";
53 die "ok 16\n";
54 1;
55} || print "ok 15\n$@";
56
c7cc6f1c
GS
57# check whether eval EXPR determines value of EXPR correctly
58
59{
60 my @a = qw(a b c d);
61 my @b = eval @a;
62 print "@b" eq '4' ? "ok 17\n" : "not ok 17\n";
63 print $@ ? "not ok 18\n" : "ok 18\n";
64
65 my $a = q[defined(wantarray) ? (wantarray ? ($b='A') : ($b='S')) : ($b='V')];
66 my $b;
67 @a = eval $a;
68 print "@a" eq 'A' ? "ok 19\n" : "# $b\nnot ok 19\n";
69 print $b eq 'A' ? "ok 20\n" : "# $b\nnot ok 20\n";
70 $_ = eval $a;
71 print $b eq 'S' ? "ok 21\n" : "# $b\nnot ok 21\n";
72 eval $a;
73 print $b eq 'V' ? "ok 22\n" : "# $b\nnot ok 22\n";
fc360e46
AB
74
75 $b = 'wrong';
76 $x = sub {
77 my $b = "right";
78 print eval('"$b"') eq $b ? "ok 23\n" : "not ok 23\n";
79 };
80 &$x();
c7cc6f1c 81}