Solved

Problem with variable

Posted on 2000-02-16
10
164 Views
Last Modified: 2010-03-05
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?
0
Comment
Question by:microboard
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 5

Expert Comment

by:PC_User321
ID: 2529363
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

0
 
LVL 84

Expert Comment

by:ozo
ID: 2529710
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>  
0
 

Author Comment

by:microboard
ID: 2530148
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.
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 16

Expert Comment

by:maneshr
ID: 2531469
can you provide the forum with the complete script?
0
 

Author Comment

by:microboard
ID: 2532527
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
0
 
LVL 16

Expert Comment

by:maneshr
ID: 2532960
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";
0
 
LVL 5

Expert Comment

by:PC_User321
ID: 2533633
microboard,

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

0
 

Author Comment

by:microboard
ID: 2533949
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.

0
 
LVL 16

Accepted Solution

by:
maneshr earned 50 total points
ID: 2534995
glad that you could figure out the problem.
0
 

Author Comment

by:microboard
ID: 2539277
thanks
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

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 …
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

777 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