CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Perl File Exists Issue

 



tbone587
User

Jun 9, 2011, 8:14 PM

Post #1 of 4 (312 views)
Perl File Exists Issue Can't Post

I have been trying to get the file exist checker to work within my program, but I cant get it to read my file path. I have tested it with a test file and it works, but it will not read the file name I have with a path. For the example I am using it should be opening up the file "/home/tsmith/Scripts/DNS/etc/namedb/rev/204.89.60". For some reason it will not recognize the file and output the else portion of the if/else statement. I have tried using the file test.cfg and it will then read the file and work file. It seems to have an issue with the path or recognizing the variable values. Here is the code:


Code
 
($Match_Rev_IP) = $CleanupLine =~ m/(\d\d?\d?)\.(\d\d?\d?)\.(\d\d?\d?)\.(\d\d?\d?)/;

my $FirstOctet = $1;
my $SecondOctet = $2;
my $ThirdOctet = $3;
my $LastOctet = $4;

$RevFile = '/home/tsmith/Scripts/DNS/etc/namedb/rev/${FirstOctet}.${SecondOctet}.${ThirdOctet}';

if (-e $RevFile)
{

open REVIN, "</home/tsmith/Scripts/DNS/etc/namedb/rev/$FirstOctet.$SecondOctet.$ThirdOctet" or die $!;

my @REV_ENTRY = <REVIN>;
close (REVIN);

open REVOUT, ">/home/tsmith/Scripts/DNS/etc/namedb/rev/$FirstOctet.$SecondOctet.$ThirdOctet" or die $!;
print "DEBUG: File existed\n";
}

else
{
print "The $FirstOctet.$SecondOctet.$ThirdOctet zone file does not exist! No cleanup is necessary...\n";
}



miller
User

Jun 9, 2011, 8:44 PM

Post #2 of 4 (308 views)
Re: [tbone587] Perl File Exists Issue [In reply to] Can't Post

Use double quotes. You aren't interpolating those variables.

If you had printed out your file variable, you would've seen this:


Code
$RevFile = "/home/tsmith/Scripts/DNS/etc/namedb/rev/${FirstOctet}.${SecondOctet}.${ThirdOctet}";


Also, you should use strict; and use warnings; in all your programs.

- Miller


BillKSmith
Veteran

Jun 9, 2011, 8:47 PM

Post #3 of 4 (307 views)
Re: [tbone587] Perl File Exists Issue [In reply to] Can't Post


Quote

Code
$RevFile = '/home/tsmith/Scripts/DNS/etc/namedb/rev/${FirstOctet}.${SecondOctet}.${ThirdOctet}';




Double quotes are required for variable interpolation.
Good Luck,
Bill


tbone587
User

Jun 9, 2011, 9:24 PM

Post #4 of 4 (305 views)
Re: [BillKSmith] Perl File Exists Issue [In reply to] Can't Post

thank you. I figured that out right before you guys replied:( This forum is truly great though, people always willing to help.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives