Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

do-ing of file without \n at the end

by sawoy (Initiate)
on Mar 30, 2007 at 16:07 UTC ( #607515=perlquestion: print w/replies, xml ) Need Help??

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


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 ./ #!/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 run:
$ ./
Can't load conf.conf due: 'Bad file descriptor' or '' at ./ 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)

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


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 '' with content:
      print "err = '$!'\n"; exit 0;
      without \n in the end

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

      - see in output:

      $ perl ./
      err = 'Bad file descriptor'

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

        $! 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.


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.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://607515]
Approved by Old_Gray_Bear
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (2)
As of 2023-05-28 01:57 GMT
Find Nodes?
    Voting Booth?

    No recent polls found