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

As described in $! and Error Indicators in perlvar, "A successful system or library call does not set the variable [$!] to zero." This means you cannot test $! and expect to get a meaningful result. Note that despite your reported error, your code executes the script with no problem. Consider:

#!/usr/bin/perl use strict; use warnings; my $do_me="do_me.pl"; print "(1)Can not read $do_me: $!\n" if $!; foreach my $ending ("\n", '') { print "Create file with",($ending?'':'out')," newline\n"; open(OUT,'>',$do_me) or die "$!"; print "(2)Can not read $do_me: $!\n" if $!; print OUT "q(string)$ending"; close OUT; my $result=do $do_me; print "(3)Can not read $do_me: $!\n" if $!; print "Can not evaluate $do_me: $@\n" if $@; print("$result\n") if defined $result; } print "(4)Can not read $do_me: $!\n" if $!;

outputs:

(1)Can not read do_me.pl: Bad file descriptor Create file with newline (2)Can not read do_me.pl: Bad file descriptor string Create file without newline (3)Can not read do_me.pl: Bad file descriptor string (4)Can not read do_me.pl: Bad file descriptor

As to why you get that pattern, my guess is internal try-catch structures within the open and do blocks.

Replies are listed 'Best First'.
Re^2: Why do we need a \n with do FILENAME?
by rovf (Priest) on Jul 06, 2010 at 15:57 UTC
    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>

      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>
      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>