Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing a value from an array into a file reference

Status
Not open for further replies.

ddmama

IS-IT--Management
Jun 3, 2003
5
US
Hello,
I have a fairly complicated perl cgi script that I inherited that I am trying to tweak. Specifically, I need to be able to designate a different file name to be opened depending on what value the script passes back. The filename is stored in one column in a two-dimensional array that is stored in a .csv file.


I am trying to refence that filename in order to designate the html filepath name that should be opened. I am using the following script:

$FILE_PATH = "../htdocs/@fields[12]";

I know that this populates correctly because I can print the value and it returns "../htdocs/document.html" But when I try to actually use it to open ../htdocs/document.html, I get a "page cannot be displayed error"

Any ideas? I've tried any number of variations on this theme...I'm really at a loss.
 
I rarely walk back the tree using "..". It's prone to some errors. It's better with the full path IMHO. What is the status of your open statement if you check thr $! variable?
 
Can you use an absolute path, rather than a relative path?
Paul

It's important in life to always strike a happy medium, so if you see someone with a crystal ball, and a smile on their face ...
 
Before I started tweaking the script, it had a static address defined as $FILE_PATH = '../htdocs/document.html' and it worked. However, I'll try using the absolute path and see how it behaves.
 
OK, tried it with an absolute path, same problem. I'm not clear as to what the question about "What is the status of your open statement if you check thr $! variable?" Perhaps a bit more code would help. Here's how I open the csv file, load it to the array, then grab the info I need.

open CENTERS, $DATA_PATH;
@centers = <CENTERS>;
close CENTERS;

$cCode = $formData->param('cCode');

foreach $line1 (@centers) {
@fields = split(/,/, $line1);


$FILE_PATH = &quot;/usr/local/apache/htdocs/@fields[12]&quot;;

if(@fields[0] eq $cCode) {

open HTML, $FILE_PATH;
@html = <HTML>;
close HTML;
foreach $line2 (@html) {
$line2 =~ s/\[NAME\]/$fields[2]/;
$line2 =~ s/\[ADDRESS1\]/$fields[3]/;
etc.

Note: I've tried puting the $FILE_PATH definition both where it is and inside the if (@fields[0] eq $cCode) condition. Both fail.
 
When you open a file (or do any external operation) it's wise to check if it succeeded. What if for some reason the file your script tries to open suddenly disappears? You just can't have your script continue executing as if nothing was wrong. So in other words always check the status and take appropriate action if the statement returns a failure. so your open's should look like;

open FH, &quot;file.txt&quot; or die &quot;Can't open file.txt: $!&quot;;

that will cause your script to die if for some reason it could not open file.txt and will print the reason (contained in $!) why it failed.
 
for brevity, I left out the error checking in the script. Specifically, we check to make sure that the file is there:

if(!(-f $DATA_PATH)) {
&error(&quot;Could not open the CSV data file containing center information, because of an error: $!&quot;);
}

We also check to see if the parameter was passed from the prior page:

if(cCode eq '') {
&error(&quot;The cCode parameter was not passed to the script, so a coupon could not be generated.&quot;);
}

It's is clearly not an error in retrieving the data from the file. I know this because I've populated the html address with a static address and substituted the $FILE_PATH variable for the NAME $fields[2] reference above. When the next page loads, the NAME reference correctly is listed as /usr/local/apache/htdocs/document.html.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top