Re: Checking to see if file exists.
- From: kennethwolcott@xxxxxxxxx (Kenneth Wolcott)
- Date: Tue, 29 Jul 2008 17:37:31 -0700
On Tue, Jul 29, 2008 at 2:32 PM, tvadnais <tvadnais@xxxxxxxxxxxxx> wrote:
I am totally confounded by what appears to be a bug in the "does file
exist"
functionality.
Here is the code as it stands now:
my $tmpfile = substr ($file, 0, index($file, ".pgp"));
print cwd(); ## debug code: to make sure we're in the correct
directory
chomp($tmpfile); ## debug code: Added this to see if there was any thing
funny in the file name.
if (-e $tmpfile) {
# if (-e "$tmpfile"){ ##This doesn't work
# if (-e "F0715PAY.TXT") { ## This does work, but it needs to be a
variable
## Do magic stuff to file.
} else {
## Send error message
}
I stepped through the code with the debugger for the above snippet and this
is what I got (with a few minor edits for clarity sake)
The following files were found in directory: F0715PAY.TXT.pgp,
J0715PAY.TXT.pgp, C0715PAY.TXT.pgp
main::(rmain.pl2): my $tmpfile = substr ($file, 0, index($file, ".pgp"));
DB<> n
DB<> x $tmpfile
0 'F0715PAY.TXT.pgp'
DB<> /xfer/test/RDY
main::(rmain.pl2): chomp($tmpfile);
DB<> n
main::(rmain.pl2): if (-e $tmpfile) {
DB<> x $tmpfile
0 'F0715PAY.TXT'
DB<> n
main::(rmain.pl2): LogMsg (MSG => "Couldn't find $F0715PAY.TXT", Echo =>
$debug);
In a nutshell:
If I hard code in a file that I know exists, then the conditional (-e
filename) comes back true.
If that same file name is passed to the conditional in variable form, then
(-e $filename) and (-e "$filename") returns false.
I also tried passing in the full path name, with no success.
Yes, I do have permissions to read the file.
The OS is AIX UNIX if that makes any difference.
The "Send error message" includes email notification that the PGP
unencryption didn't work, so it's critical I don't just blindly assume that
the PGP decryption worked.
The "Do magic stuff" includes zipping and archiving the PGP file of to an
archive directory.
Any thoughts you anyone has would be greatly appreciated.
Tim
See what happens if you don't quote the variable in your if statement and
use -f rather than -e.
Replace this:
if (-e "$tmpfile")
with this:
if (-f $tmpfile)
perldoc -q quoting
Found in /usr/lib/perl5/5.10/pods/perlfaq4.pod
What's wrong with always quoting "$vars"?
The problem is that those double-quotes force
stringification--coercing
numbers and references into strings--even when you don't want them to
be strings. Think of it this way: double-quote expansion is used to
produce new strings. If you already have a string, why do you need
more?
If you get used to writing odd things like these:
print "$var"; # BAD
$new = "$old"; # BAD
somefunc("$var"); # BAD
You'll be in trouble. Those should (in 99.8% of the cases) be the
simpler and more direct:
print $var;
$new = $old;
somefunc($var);
Otherwise, besides slowing you down, you're going to break code when
the thing in the scalar is actually neither a string nor a number,
but
a reference:
func(\@array);
sub func {
my $aref = shift;
my $oref = "$aref"; # WRONG
}
You can also get into subtle problems on those few operations in Perl
that actually do care about the difference between a string and a
number, such as the magical "++" autoincrement operator or the
syscall() function.
Stringification also destroys arrays.
@lines = `command`;
print "@lines"; # WRONG - extra blanks
print @lines; # right
I hope this helps,
Ken Wolcott
- Follow-Ups:
- RE: Checking to see if file exists.
- From: Tvadnais
- RE: Checking to see if file exists.
- References:
- Checking to see if file exists.
- From: Tvadnais
- Checking to see if file exists.
- Prev by Date: Re: Regular expression: How to determine wether entry is a number?
- Next by Date: RE: Checking to see if file exists.
- Previous by thread: Checking to see if file exists.
- Next by thread: RE: Checking to see if file exists.
- Index(es):
Relevant Pages
|