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:
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.defined(do $file) or die "Can't load $file: $!\n";
In reply to Re: do-ing of file without \n at the end
by sgifford
in thread do-ing of file without \n at the end
by sawoy
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |