Raju Raju - 1 month ago 15
Perl Question

Can directory names and file names be variables in cgi perl scripting

I know that file names can be variable in perl scripting.Does the same apply to cgi-perl scripting.Because when I used variables inside open statement I get the error No such file or directory.But when I directly mention the path the file is opened for reading.These variables are passed from a form. The values are passed correctly they are not empty(Checked by printing the varaiables).

Example:

$dir=abc;
$file=file1;

open (FILE, '/var/www/cgi-bin/$dir/$file')
or print "file cannot be opened $!\n";


Error:

file cannot be opened no such file or directory
.

Answer

Use double quotes to interpolate variables:

open (FILE, "/var/www/cgi-bin/$dir/$file")
#    here __^                and here __^
    or print "file cannot be opened $!\n";

Also, ALWAYS

use strict;
use warnings;

By using single quotes, the varibles aren'tt interpolated, so you're trying to open literally /var/www/cgi-bin/$dir/$file and it doesn't exist.