in reply to Re: Why do we need a \n with do FILENAME?
in thread Why do we need a \n with do FILENAME?

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>

Replies are listed 'Best First'.
Re^3: Why do we need a \n with do FILENAME?
by ikegami (Patriarch) on Jul 06, 2010 at 16:08 UTC

    Since $! isn't set inside the file-to-be-evaluated, it must have been set by Perl while processing the file.

    Yes, and completely irrelevant. $! cannot be used to determine if an error occurred.

    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

    No, it's not reasonable. The "do" module should return true on success just like "require" modules.

      The "do" module should return true on success
      May I ask how you could justify the should in your sentence? I read the perldoc again, but am unable to find any recommendation on the return value. In particular, it does not say that the user should signal an error by returning false. The only reference which I could found to require is, that do should not be used to load modules.

      IMHO, the best use for do is in those cases, where you want to evaluate at run-time Perl code which happens to be stored in a file. Without do, you would have to open the file, read it and eval the result. do is a shorthand way for doing this, but it means that a return value of undef (or other false value, such as 0 or '') should not be required to have a special meaning of 'failure'. The present implementation of do would nearly perfectly do this job, if it would not suffer from the single restriction that the caller can not distinugish between the case of the code in the filename returning undef, or Perl being unable to open the file. Moreover, this flaw in the implementation shows up only if the file does not end in a newline, and it would be easy to correct.

      So, technically speaking, you are right in that the documentation indeed allows for this behaviour. But isn't it a pity that we could make a function much more useful, by just a minor change in implementation? Anyway, the question which I used when starting this thread, is answered: It is not a bug, but undefined behaviour, and it means that I have to restrict what our users are permitted to place into the file which our application will do...
      -- 
      Ronald Fischer <ynnor@mm.st>

        May I ask how you could justify the should in your sentence?

        You've already explained why. If you return undef, you can't determine if an error occurred. As you put it, "the caller can not distinguish between the case of the code in the filename returning undef, or Perl being unable to open the file".

        It is not a bug, but undefined behaviour

        It's not undefined behaviour. The result of the function can be ambiguous, but the behaviour is defined.

Re^3: Why do we need a \n with do FILENAME?
by kennethk (Abbot) on Jul 06, 2010 at 16:23 UTC
    I do appreciate your point about undef as a legal return value, however an examination of the code sample from do shows usage expects a defined test on the return value, likely for this very reason. As I said, I suspect there is a try-catch in the file access subroutines, and so you are getting a false negative from an internal failure. You could add an extra layer of security with a -r test, guarantee an appended newline on your file name or guarantee a defined return value, but all of these are workarounds. I would be particularly hesitant to simply append the newline, as that strikes me as fragile.
      examination of the code sample from do shows usage expects a defined test on the return value, likely for this very reason.
      Actually the sample does not check on define, but on truth (which might have led ikegami into thinking that the file should always return true), but I believe that for this very reason this is just an example of how do might be used - otherwise the example would have explicitly say  unless (defined($return = do $file)) to make it explicit that an undef return value does not make sense. Indeed, for reading a configuration file (which is what the example is about), it makes sense to require that the config file should return true in success; but I don't think this can be taken to mean that this always should be the case.

      -- 
      Ronald Fischer <ynnor@mm.st>
        Actually the sample does not check on define, but on truth

        For the $! case, do says

        warn "couldn't do $file: $!"    unless defined $return;

        The truth case is checking the success of the contained code, not the OS's ability to locate and execute file. I personally would go code diving before weighing in on the appropriateness of perl's behavior.

        which might have led ikegami into thinking that the file should always return true

        You could require your included file to return something defined. Requiring the included file to return something true is much clearer and simpler, though, and Perl programmers are already accustomed to doing that.