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:
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.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; }
If we are picky and take the documentation, which says
, 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:
Actually, this is a simplified example derived from a real application.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; }
|
|---|
| 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 | |
by rovf (Priest) on Jul 07, 2010 at 08:56 UTC | |
by ikegami (Patriarch) on Jul 07, 2010 at 15:50 UTC | |
|
Re^3: Why do we need a \n with do FILENAME?
by kennethk (Abbot) on Jul 06, 2010 at 16:23 UTC | |
by rovf (Priest) on Jul 07, 2010 at 09:23 UTC | |
by kennethk (Abbot) on Jul 07, 2010 at 14:32 UTC | |
by ikegami (Patriarch) on Jul 07, 2010 at 16:24 UTC | |
by rovf (Priest) on Jul 08, 2010 at 07:58 UTC | |
by ikegami (Patriarch) on Jul 08, 2010 at 17:06 UTC |