Solved

perl, how to find out if a file exist

Posted on 2011-09-29
10
278 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
Comment Utility
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
Comment Utility
It works.  Thanks.
0
 
LVL 23

Assisted Solution

by:nemws1
nemws1 earned 50 total points
Comment Utility
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
 

Author Closing Comment

by:rgbcof
Comment Utility
It works.  Thanks
0
 
LVL 10

Expert Comment

by:jeromee
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 84

Expert Comment

by:ozo
Comment Utility
the quotes are not required
0
 
LVL 10

Expert Comment

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

Expert Comment

by:nemws1
Comment Utility
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
Comment Utility
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
Comment Utility
No apologies req'd nemws1.
Thanks anyway.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

763 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now