A successful system or library call does not set the variable $! to zero.
I am aware of this, but in my case, this is not the explanation for the problem, though I see now that I should have made it clear from the beginning. Consider the modified code:

use strict; use warnings; my $do_me="do_me.pl"; foreach my $ending ('',"\n") { print "Create file with",($ending?'':'out')," newline\n"; open(OUT,'>',$do_me) or die "$!"; print OUT "q(string)$ending"; close OUT; $!=0; my $result=do $do_me; print "Can not read $do_me: $!\n" if $!; print "Can not evaluate $do_me: $@\n" if $@; print("$result\n") if defined $result; }
The difference to the original code is that I now reset $! explicitly before doing the file. However, I still get the error. Since $! isn't set inside the file-to-be-evaluated, it must have been set by Perl while processing the file.

If we are picky and take the documentation, which says

If "do" cannot read the file, it returns undef and sets $! to the error. If "do" can read the file but cannot compile it, it returns undef and sets an error message in $@. If the file is successfully compiled, "do" returns the value of the last expression evaluated.
, at face value, we can of course argument like this: Since our do does not return undef, we can safely ignore $!. Hence, this is not a bug.

However, this argument would sound a bit nitpicking to my ears, for the following reason: Imagine that we are doing a file which (legally) returns undef, which might be reasonable if the code in the file is executed only for its side effects, not for its return value. In this case, there is no way to decide whether the file had been read and executed, or whether Perl simply was not able to process the file. Here is a simplified version of my test case demonstrating this problem:

use strict; use warnings; my $do_me="do_me.pl"; foreach my $ending ('',"\n") { print "Create file with",($ending?'':'out')," newline\n"; open(OUT,'>',$do_me) or die "$!"; print OUT "undef$ending"; close OUT; $!=0; my $result=do $do_me; print "Can not read $do_me: $!\n" if $!; print "Can not evaluate $do_me: $@\n" if $@; print("$result\n") if defined $result; }
Actually, this is a simplified example derived from a real application.

-- 
Ronald Fischer <ynnor@mm.st>

In reply to Re^2: Why do we need a \n with do FILENAME? by rovf
in thread Why do we need a \n with do FILENAME? by rovf

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.