Link to home
Start Free TrialLog in
Avatar of microboard
microboard

asked on

Problem with variable

Knowing the following:

$tFile = "greeting"
$Particular = "invitation"
$FileToUse = "dat.csn"

Why then does the following line not work correctly?

$NewFilename = "cgi_user_dir/${tFile}${Particular}${FileToUse}";

The result makes NewFilename = "cgi_user_dir//invitation/dat.csn"

What is happening to the "tFile" variable??

Does any of this make sense to anybody?
Avatar of PC_User321
PC_User321

Do you have semicolons at the end of these:-

$tFile = "greeting"
$Particular = "invitation"
$FileToUse = "dat.csn"
Must have them.

The example does not seem to be correct - where do the slashes come from?

Perhaps you really did :-
$NewFilename = "cgi_user_dir/$tFile/$Particular/$FileToUse";

With the changes described, it should work.  It works for me

Avatar of ozo
perl -de 1

Enter h or `h h' for help.

main::(-e:1):   1
  DB<1> $tFile = "greeting"

  DB<2> $Particular = "invitation"

  DB<3> $FileToUse = "dat.csn"

  DB<4> x $NewFilename = "cgi_user_dir/${tFile}${Particular}${FileToUse}";
0  'cgi_user_dir/greetinginvitationdat.csn'
  DB<5>  
Avatar of microboard

ASKER

I can run the script on my own computer and it does fine but when running it on the web server I am having the trouble.

Thanks guys but I guess my method is ok; it must be something else messing things up.
can you provide the forum with the complete script?
What I'm trying to do is check whether a file already exists on my server and if so, then I simply make a new filename. What I don't understnad is that it was working fine then suddenly it's not working anymore.


##--------BEGIN SCRIPT-----------------

#just know that $upname is a filename that includes the directory: "directory/sub/file.csn"

#set filename
$filename = $upname;

#Here I am separating the directory name
#from the filename
$DirPosCheck = rindex($upname, "\/");
$DirCheck = substr($upname,0,                     $DirPosCheck);

#this gives me the filename alone
$FileCheck= substr($upname,$DirPosCheck + 1);

# check directory existence - this works fine
if (!(-e "${FULLPATH}cgi_user_dir/${DirCheck}"))        {
      print "Invalid Directory Names -\n";
      print "*${DirCheck}*\n";
      exit;
       }

# find free file name
#What I'm doing here is simply separating the
#known file extension from the filename - it works
$NewFilename = $FileCheck;
$NewFilename =~ s/.que.csn//i;
$extension = ".que.csn";
$nCount = 0;

#this is the trouble area
while (-e      "$/cgi_user_dir/${DirCheck}/${FileCheck}") {

     ++$nCount;
     $FileCheck =                      "${NewFilename}${nCount}${extension}";
   };             

For some reason the $FileCheck part of the string is not being registered by the script so that even if a file exists, it is not being found. Now, I can remove the $FileCheck part and then it returns true because it will register the $DirCheck value. But if I put the $FileCheck value back in, then it skips the entire statement as if it couldnt' find the file. The file I'm testing does exist so don't think I'm getting the value wrong. I can manually type in the filename and it works fine, but if I leave it up to the variable, then it doesn't seem to register. Is there any way this could be a server problem? Is there some known incident where a server's perl script executable was to blame? The think I don't understand is that the above script was working just fine. Now, it gives problems.

Perhaps someone could simply show me how you would check for the existence of a file on your server and then adjust the name of the file so that you do not overwrite the existing one. Hope someone can help
if i am not wrong you are first trying to seperate the directory and the filename and then creating a new file with a running number.

if so you can use this. i have cut down the program size a lot without removing any validation checks

===file_ext.pl

#!/usr/local/bin/perl

$upname="/tmp/UserAccounts.pm";

#set filename
$filename = $upname;

$filename=~ /(.*)\/(.*)$/;
$dir=$1;
$FileCheck=$2;

# check directory existence - this works fine
if (!(-e "$dir")){
   print "Invalid Directory Names -\n";
   print "*$dir*\n";
   exit;
}

# find free file name
#What I'm doing here is simply separating the
#known file extension from the filename - it works
$NewFilename = $FileCheck;
$extension = ".pm";
$NewFilename =~ s/$extension//i;

$nCount = 0;


#this is the trouble area
while(){    ##Infinite loop. Be Careful!!
   $FileCheck = $NewFilename.$nCount.$extension;
   if (-e "$dir/$FileCheck"){
      ++$nCount;
   }else{
      last;
   }
}

print $FileCheck,"\n";
microboard,

Surely the line
   while (-e      "$/cgi_user_dir/${DirCheck}/${FileCheck}") {
in your script should be
   while (-e      "${FULLPATH}cgi_user_dir/${DirCheck}/${FileCheck}") {

maneshr, I like your code although this line $filename=~ /(.*)\/(.*)$/;  appears to only parse a specific number of times. But doesn't matter, your code is very good. So, send an answer instead of a comment.
But just to let you know, I figured out what was wrong with the script. And it turns out no one but me could have solved it because it was being created within the calling application. The application was not sending the proper POST. It was leaving off a vital part that I was using when parsing the values.

ASKER CERTIFIED SOLUTION
Avatar of maneshr
maneshr

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thanks