Re: Checking to see if file exists.



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


Relevant Pages

  • Re: why use the sealed ?
    ... > I'll want to add functionality to something at a later date. ... > I sortof wish you could add functionality to (or replace members of) ... > exceeds past the end of the string. ... > application to be a Remoting Client. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Predicting the Future and Kolmogorov Complexity
    ... to the source string), k scales with the string, thus destroying the ... excess of symmetry or a lack of it that strikes you as particularly ... The same thing is true of radio signals. ... What do I mean by levels of functionality? ...
    (talk.origins)
  • Re: basic_string causes crash in _vsnprintf???
    ... and any other output string sets used. ... want or revert back to the old stl. ... >> this functionality so while it was not a standard STL implementation ... > In STLport it's just luck that it "works". ...
    (microsoft.public.vc.stl)
  • RE: Checking to see if file exists.
    ... if (-e $tmpfile) { ... The "Do magic stuff" includes zipping and archiving the PGP file of to an ... If you already have a string, ...
    (perl.beginners)
  • Re: Get/Set vs Public Variables
    ... string firstname; ... EVER will need any functionality in getters and setters for the two string ... databinding that it can bind only to properties and not to variables. ...
    (microsoft.public.dotnet.languages.csharp)