Solved

perl, how to find out if a file exist

Posted on 2011-09-29
10
281 Views
Last Modified: 2012-05-12
In perl, how do I check if a file exist?
#if file exist, print message
0
Comment
Question by:rgbcof
  • 4
  • 2
  • 2
  • +1
10 Comments
 
LVL 10

Accepted Solution

by:
jeromee earned 200 total points
ID: 36815628
Check this out: http://perldoc.perl.org/functions/-X.html

my $myfile = "/tmp/test.txt";
if( -e $myfile ) {
    print "$myfile exists\n";
} else {
    print "$myfile does not exist\n";

}

Open in new window

0
 

Author Comment

by:rgbcof
ID: 36815708
It works.  Thanks.
0
 
LVL 23

Assisted Solution

by:nemws1
nemws1 earned 50 total points
ID: 36815710
joremee is correct - you want the '-e' option.  The only change I'd make to his code is to put double-quotes around the file argument in line 2 (in case it has a space or other odd character):
if( -e "$myfile" ) {

Open in new window

0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

Author Closing Comment

by:rgbcof
ID: 36815994
It works.  Thanks
0
 
LVL 10

Expert Comment

by:jeromee
ID: 36815996
Hi nemws1,
This script being a perl script, I don't believe that the double quotes are required.
Please correct me if I'm wrong.
0
 
LVL 84

Expert Comment

by:ozo
ID: 36816974
the quotes are not required
0
 
LVL 10

Expert Comment

by:jeromee
ID: 36817162
Thanks Ozo.
I thought I was going crazy for a sec.
0
 
LVL 23

Expert Comment

by:nemws1
ID: 36817197
I should have been more clear - my apologies, jeromee.  No, quotes are not required for a single scalar argument.  The problem is that people get in the habit of doing it this way and then they write something like this:
 
$p="/home/user"
$f="datafile"
if (-e $p/$f) {
   .....
}

Open in new window

Which is obviously incorrect.  IMO, it is good coding habit (and also helps with readability) to put double quotes around files/paths.  Saves you many a headache.
0
 
LVL 84

Expert Comment

by:ozo
ID: 36817265
on the other hand, if people get in the habit of using unnecessary quotes
they may write something like this:

open $myfile,"/tmp/test.txt" or die;
if( -e "$myfile" )

which is not so obviously incorrect.
0
 
LVL 10

Expert Comment

by:jeromee
ID: 36817298
No apologies req'd nemws1.
Thanks anyway.
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Passing shell scripts to run on the SFTP cmd object 12 182
perl to convert excel to csv 3 208
Awk Question 2 127
Perl Frameworks 1 90
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question