http://qs321.pair.com?node_id=607515

sawoy has asked for the wisdom of the Perl Monks concerning the following question:

Hello,

i found some interesting issue during my work.
in simplified way it looks like:

1. enviroment:

perl is v5.8.8, system is debian Eth.

2. files:
$ cat ./conf.conf $a = 'a'; $ cat ./test.pl #!/usr/bin/perl use strict; use warnings; my $file = 'conf.conf'; do $file; if ($! || $@) { die("Can't load $file due: '$!' or '$@'"); } exit 0;

It is major - loaded files should not have \n at the end.

3. result of test.pl run:
$ ./test.pl
Can't load conf.conf due: 'Bad file descriptor' or '' at ./test.pl line 11.

If i add newline symbol then all is ok.

Can somebody tell me: - why perl do that ?
- is it normal (known issue or something else) ?
- is it for my perl version only or not? (haven't now access to other servers for check)


Updated:
1. fix `cat ./conf.conf` -
i used this for show that file has not \n at the end.

2. 2sgifford:
ok, i understand.
but my answer is "why perl can't read file w/o \n in the end "?

Thanks,
alex|sawoy|slex.

Replies are listed 'Best First'.
Re: do-ing of file without \n at the end
by sgifford (Prior) on Mar 30, 2007 at 16:43 UTC
    You can't rely on checking $! unless do contains undef. From the docs:
    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.

    Otherwise $! can contain just random garbage, which is probably what you're running into.

    Update: To reply to your update, sawoy, perl is probably reading the file just fine, but because $! contains gibberish and you are exiting if it is nonzero, you are exiting anyways. Try using this instead:

    #!/usr/bin/perl use strict; use warnings; use vars qw($a); my $file = 'conf.conf'; do $file or die "Can't load $file: $!\n"; print "a is $a\n"; exit 0;

    That works for me.

    Note, though, that you need to ensure that $file ends with something true (not undef, 0, or the empty string). If you use this instead:

    defined(do $file) or die "Can't load $file: $!\n";
    you will only have to make sure it doesn't end with something that evaluates to undef, but there's way I know of to tell the difference between an error and the file simply ending with undef.
      ok, understood. i'll check all supported code for not full correct check of do() execution :

      And now issue transforms to:
      - create file 'test.pl' with content:
      print "err = '$!'\n"; exit 0;
      without \n in the end

      - run file by command like:
      `perl ./test.pl`

      - see in output:

      $ perl ./test3.pl
      err = 'Bad file descriptor'

      and question now is "why perl sets $! for file w/o \n in the end"...

      Thanks,
      alex|sawoy|slex
        $! is undefined if no error has occured, and so can be set to absolutely anything. It's not clear to me why Perl ends up setting it to this particular value under these circumstances (I see the same thing on my machine), but that's really a trivia question; since the behavior is undefined, any value is valid.
Re: do-ing of file without \n at the end
by Old_Gray_Bear (Bishop) on Mar 30, 2007 at 16:39 UTC
    From the Camel -- "The do FILE form use the value of FILE as a file name and executes the contents of the file as a perl script."

    So I should be able to take the contents of your conf.conf file and run it through the Perl processor to see what the expected result should be.

    $ perl -e "$a = 'a';sawoy@server3:~/test_do_issue$" syntax error at -e line 1, near "=" Execution of -e aborted due to compilation errors. waydream{}:tmp$
    I don't think that the do is your problem. I suspect that the intreter doesn't know how to handle "sawoy@server3:~/test_do_issue$"....

    Update -- fixed the quoting issue. Sigh, I gotta get a better prorof reader.

    ----
    I Go Back to Sleep, Now.

    OGB

Re: do-ing of file without \n at the end
by mreece (Friar) on Mar 30, 2007 at 17:04 UTC
    i created the conf.conf file with
    echo -n \$a=\'a\'\; > conf.conf
    and verified it has no newline (with hexdump -C conf.conf)

    i get the same error on v5.8.8 built for i686-linux but it works fine on v5.8.6 built for darwin-thread-multi-2level.

      Thanks for info.
      So this issue is not standard/default for perl.