develooper Front page | perl.perl5.porters | Postings from September 2000

Re: [ID 20000927.001] do and eval sometimes don't clear $@

Thread Previous | Thread Next
From:
Simon Cozens
Date:
September 28, 2000 04:27
Subject:
Re: [ID 20000927.001] do and eval sometimes don't clear $@
Message ID:
20000928122449.B31872@perlhacker.org
On Wed, Sep 20, 2000 at 07:32:56PM -0400, Andrew Pimlott wrote:
> The documentation for eval says
>     If there was no error, C<$@> is guaranteed to be a null string.
> and the documentation for do (the do EXPR form) says
>         do 'stat.pl';
>     is just like
>         scalar eval `cat stat.pl`;

Easy-peasy. Just clear the error SV when entering an eval or do:

==== //depot/bleadperl/pp_ctl.c#3 - /home/simon/patchbay/bleadperl/pp_ctl.c ====
@@ -3013,6 +3013,7 @@
     name = SvPV(sv, len);
     if (!(name && len > 0 && *name))
 	DIE(aTHX_ "Null filename used");
+    sv_setsv(ERRSV, &PL_sv_no);
     TAINT_PROPER("require");
     if (PL_op->op_type == OP_REQUIRE &&
       (svp = hv_fetch(GvHVn(PL_incgv), name, len, 0)) &&
@@ -3291,6 +3292,7 @@
     STRLEN len;
     OP *ret;
 
+    sv_setsv(ERRSV, &PL_sv_no);
     if (!SvPV(sv,len) || !len)
 	RETPUSHUNDEF;
     TAINT_PROPER("eval");
==== //depot/bleadperl/t/comp/require.t#2 - /home/simon/patchbay/bleadperl/t/comp/require.t ====
@@ -8,7 +8,7 @@
 
 # don't make this lexical
 $i = 1;
-print "1..23\n";
+print "1..24\n";
 
 sub do_require {
     %INC = ();
@@ -138,6 +138,13 @@
 $i++; do_require(bytes_to_utf16('n', qq(print "ok $i\\n"; 1;\n), 1)); # BE
 $i++; do_require(bytes_to_utf16('v', qq(print "ok $i\\n"; 1;\n), 1)); # LE
 
+# do 'nothing' clears $@
+$i++; 
+eval {die "shouldn't see this"};
+do "no such file";
+print "not " if $@;
+print "ok $i\n";
+
 END { 1 while unlink 'bleah.pm'; 1 while unlink 'bleah.do'; }
 
 # ***interaction with pod (don't put any thing after here)***
==== //depot/bleadperl/t/op/eval.t#2 - /home/simon/patchbay/bleadperl/t/op/eval.t ====
@@ -1,6 +1,6 @@
 #!./perl
 
-print "1..40\n";
+print "1..42\n";
 
 eval 'print "ok 1\n";';
 
@@ -206,3 +206,20 @@
     print "ok $x\n";
     $x++;
 }
+
+# More checks for clearing $@
+{
+    eval { die "shouldn't see this\n" };
+    eval { };
+    print "not " if $@;
+    print "ok $x\n";
+    $x++;
+}
+
+{
+    eval { die "shouldn't see this\n" };
+    eval { print "# Something that isn't a syntax error\n"; };
+    print "not " if $@;
+    print "ok $x\n";
+    $x++;
+}

 % ./perl harness op/eval.t comp/require.t
op/eval.............ok
comp/require........ok
All tests successful.
Files=2, Tests=66,  0 wallclock secs ( 0.10 cusr +  0.02 csys =  0.12 CPU)

-- 
"I think i'll take my girlfriend to vegas for a win'98 burn/upgrade...."
        -- Megahal (trained on asr), 1998-11-06

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About