Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: do-ing of file without \n at the end

by sgifford (Prior)
on Mar 30, 2007 at 16:43 UTC ( [id://607523]=note: print w/replies, xml ) Need Help??


in reply to do-ing of file without \n at the end

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.

Replies are listed 'Best First'.
Re^2: do-ing of file without \n at the end
by sawoy (Initiate) on Mar 31, 2007 at 04:38 UTC
    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.
        Bad file descriptor is error 0 on some systems. It means someone did $! = 0; under the false assumption that 0 meant success. Or as a favour for those who make that assumption.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://607523]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (7)
As of 2024-03-29 00:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found