#!./perl
BEGIN {
chdir 't' if -d 't';
}
print "1..91\n";
eval 'print "ok 1\n";';
eval "\$foo\n = # this is a comment\n'ok 3';";
print $foo,"\n";
eval "\$foo\n = # this is a comment\n'ok 4\n';";
print $foo;
print eval '
$foo =;'; # this tests for a call through yyerror()
print eval '$foo = /'; # this tests for a call through fatal()
print eval '"ok 7\n";';
# calculate a factorial with recursive evals
$foo = 5;
$fact = 'if ($foo <= 1) {1;} else {push(@x,$foo--); (eval $fact) * pop(@x);}';
$foo = 5;
$fact = 'local($foo)=$foo; $foo <= 1 ? 1 : $foo-- * (eval $fact);';
do './Op.eval'; print $@;
# Test the singlequoted eval optimizer
$i = 11;
for (1..3) {
eval 'print "ok ", $i++, "\n"';
}
eval {
print "ok 14\n";
1;
} || print "ok 15\n$@";
# check whether eval EXPR determines value of EXPR correctly
{
my @b = eval @a;
print $@ ? "not ok 18\n" : "ok 18\n";
my $b;
@a = eval $a;
$_ = eval $a;
eval $a;
$b = 'wrong';
$x = sub {
my $b = "right";
};
&$x();
}
my $b = 'wrong';
my $X = sub {
my $b = "right";
};
&$X();
# check navigation of multiple eval boundaries to find lexicals
my $x = 25;
eval <<'EOT'; die if $@;
print "# $x\n"; # clone into eval's pad
eval $_[0]; die if $@;
}
$x++;
$x++;
$x++;
# calls from within eval'' should clone outer lexicals
eval <<'EOT'; die if $@;
eval $_[0]; die if $@;
}
$x++;
$x++;
$x++;
# calls outside eval'' should NOT clone lexicals from called context
my $ok = 'ok';
eval <<'EOT'; die if $@;
# $x unbound here
eval $_[0]; die if $@;
}
{
}
# can recursive subroutine-call inside eval'' see its own lexicals?
my $l = shift;
if ($l < $x) {
++$l;
eval 'print "# level $l\n"; recurse($l);';
die if $@;
}
else {
print "ok $l\n";
}
}
{
}
$x++;
# do closures created within eval bind correctly?
eval <<'EOT';
sub create_closure {
my $self = shift;
return sub {
print $self;
};
}
$x++;
# does lexical search terminate correctly at subroutine boundary?
{
my $r = "not ok $x\n";
eval 'terminal($r)';
}
$x++;
print $@;
# does scalar eval"" pop stack correctly?
{
my $c = eval "(1,2)x10";
$x++;
}
# return from eval {} should clear $@ correctly
{
my $status = eval {
eval { die };
print "# eval { return } test\n";
return; # removing this changes behavior
};
print "not " if $@;
print "ok $x\n";
$x++;
}
# ditto for eval ""
{
my $status = eval q{
eval q{ die };
print "# eval q{ return } test\n";
return; # removing this changes behavior
};
print "not " if $@;
print "ok $x\n";
$x++;
}
# Check that eval catches bad goto calls
# (BUG ID 20010305.003)
{
eval {
print ($@ ? "ok 41\n" : "not ok 41\n");
last;
foreach my $i (1) {
print "# jumped into foreach\n";
}
};
print "not ok 41\n" if $@;
}
# Make sure that "my $$x" is forbidden
# 20011224 MJD
{
eval q{my $$x};
print $@ ? "ok 42\n" : "not ok 42\n";
eval q{my @$x};
print $@ ? "ok 43\n" : "not ok 43\n";
eval q{my %$x};
print $@ ? "ok 44\n" : "not ok 44\n";
eval q{my $$$x};
print $@ ? "ok 45\n" : "not ok 45\n";
}
# [ID 20020623.002] eval "" doesn't clear $@
{
$@ = 5;
eval q{};
}
# DAPM Nov-2002. Perl should now capture the full lexical context during
# evals.
my $zzz = 1;
eval q{
sub fred1 {
eval q{ print eval '$zzz' == 1 ? 'ok' : 'not ok', " $_[0]\n"}
}
};
eval q{
sub fred2 {
print eval('$zzz') == 1 ? 'ok' : 'not ok', " $_[0]\n";
}
};
# sort() starts a new context stack. Make sure we can still find
# the lexically enclosing sub
sub do_sort {
my $zzz = 2;
my @a = sort
{ print eval('$zzz') == 2 ? 'ok' : 'not ok', " 51\n"; $a <=> $b }
2, 1;
}
# more recursion and lexical scope leak tests
eval q{
my $r = -1;
my $yyy = 9;
sub fred3 {
my $l = shift;
my $r = -2;
return 1 if $l < 1;
return 0 if eval '$zzz' != 1;
return 0 if $yyy != 9;
return 0 if eval '$yyy' != 9;
return 0 if eval '$l' != $l;
}
print $r == 120 ? 'ok' : 'not ok', " 52\n";
$r = eval'fred3(5)';
print $r == 120 ? 'ok' : 'not ok', " 53\n";
$r = 0;
eval '$r = fred3(5)';
print $r == 120 ? 'ok' : 'not ok', " 54\n";
$r = 0;
print $r == 120 ? 'ok' : 'not ok', " 55\n";
};
print $r == 120 ? 'ok' : 'not ok', " 56\n";
$r = eval'fred3(5)';
print $r == 120 ? 'ok' : 'not ok', " 57\n";
$r = 0;
eval'$r = fred3(5)';
print $r == 120 ? 'ok' : 'not ok', " 58\n";
$r = 0;
print $r == 120 ? 'ok' : 'not ok', " 59\n";
# check that goto &sub within evals doesn't leak lexical scope
my $yyy = 2;
my $test = 60;
sub fred4 {
my $zzz = 3;
$test++;
$test++;
}
eval q{
sub fred5 {
my $zzz = 4;
$test++;
$test++;
goto &fred4;
}
};
# [perl #9728] used to dump core
{
$test++;
}
# evals that appear in the DB package should see the lexical scope of the
# thing outside DB that called them (usually the debugged code), rather
# than the usual surrounding scope
$test=79;
our $x = 1;
{
my $x=2;
}
{
my $x = 3;
}
$NO_ENDING = 1;
# [perl #19022] used to end up with shared hash warnings
# The program should generate no output, so anything we see is on stderr
stderr => 1);
} else {
}
$test++;
# And a buggy way of fixing #19022 made this fail - $k became undef after the
# eval for a build with copy on write
{
my %h;
$h{a}=1;
} else {
}
$test++;
eval "\$k";
} else {
}
$test++;
}
}
# check for context in string eval
{
my(@r,$r,$c);
$r = 'ab';
@r = eval $code;
$r = eval $code;
eval $code;
}