Solved

perl, how to find out if a file exist

Posted on 2011-09-29
10
285 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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…

739 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