Darshan Shah
Darshan Shah

Reputation: 19

how to check valid file in windows using perl?

I tried -e and -f switch, but they don't work.


print "Enter Board File:";
$brdFile = <>;
$a = -e $brdFile;
printf("Value of A is %d\n",$a);

Output:

Enter Board File:C:\SATAHDD.brd Value of A is 0

Location shown above is correct.

Upvotes: 1

Views: 68

Answers (2)

Ralph Marshall
Ralph Marshall

Reputation: 223

I'd like to suggest a few improvements to your original code, although I agree with toolic that your problem is almost certainly the missing call to chomp. I present this version of the code that I think represents best practices for this problem.

use strict; # Prevents use of undeclared variables, barewords, etc.
use warnings; # Lets you know about runtime problems such as the chomp

print "Enter Board File: ";
my ($brdFile) = <>;
chomp $brdfile;

# Unless you need it later in your program for some reason there is
# no requirement that you store the result in a variable. You can also 
# glue together an output line without using printf if all you need
# is concatenation of string literals and variables
print "\nResult of -e $brdFile is ", -e $brdFile, "\n";

Upvotes: 0

toolic
toolic

Reputation: 62037

You need to chomp the input to remove the newline character:

print "Enter Board File:";
$brdFile = <>;
chomp $brdFile;
$a = -e $brdFile;
printf("Value of A is %d\n",$a);

If you use warnings;, you should get a warning message about the newline.

Upvotes: 2

Related Questions