Forum Moderators: coopster & phranque

Message Too Old, No Replies

write a variable filename fails

         

parrot

11:19 am on Feb 21, 2005 (gmt 0)

10+ Year Member



Hi all,

I'm working on a shareware software review script. All variables passed by my form are rendered ok. What I would like to do is write a file with the user's registration number as the filename. Perl prints $registration ok but fails to use $registration as the filename.

The value of the label in the form = set to 10100001
I want the script to write the file 10100001.txt

Here's what I came up with so far.

Cheers all,

Ton

#!/usr/bin/perl
use CGI qw(:standard);
#use strict;

print header;

$query = new CGI;
$revdir = review;
$registration = $query->param('registration') ¦¦ '(No input)';
$your_name = $query->param('your_name') ¦¦ '(No input)';
$your_message = param('your_message') ¦¦ '(No input)';

use Cwd;
mkdir( $revdir, 0755 );
chdir review;

$registration = $registration . ".txt";

open NWEFH, ">>$registration";
print NWEFH "Review by: " , $your_name,"\n";
print NWEFH $your_message,"\n\n";

close(NWEFH);

wruppert

1:27 pm on Feb 21, 2005 (gmt 0)

10+ Year Member



Uncomment "use strict;"
Add "use warnings;"

My system did not like your broken bar ¦¦, but that may just be keyboard differences.

my $revdir = review; -> review needs to be quoted.
chdir review; -> review needs to be quoted.

Should use 3 param version of open for safety.
Check return codes.

This worked on my ActivePerl WinXP machine from the command line.

[pre]
#!/usr/bin/perl
use CGI qw(:standard);
use strict;
use warnings;

print header;

my $query = new CGI;
my $revdir = "review";
my $registration = $query->param('registration') ¦¦ '(No input)';
my $your_name = $query->param('your_name') ¦¦ '(No input)';
my $your_message = param('your_message') ¦¦ '(No input)';

use Cwd;
mkdir( $revdir, 0755 )
or die "Unable to create directory $revdir - $!";
chdir $revdir
or die "Unable to chdir $revdir $!";

$registration = $registration . ".txt";

open NWEFH, ">>", $registration;
or die "Unable to open $registration for output - $!";
print NWEFH "Review by: " , $your_name,"\n";
print NWEFH $your_message,"\n\n";

close(NWEFH);
[/pre]

wruppert

2:43 pm on Feb 21, 2005 (gmt 0)

10+ Year Member



Just noted small error above:

chdir review; -> needs to be "chdir $revdir;"

I had fixed this in my code example, but missed it earlier.

rocknbil

5:51 pm on Feb 21, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Does that work for you if $registration = 001?

You may want to also quote registration to force it to a string, or do

$registration = '' . "$registration" . '.txt';

parrot

6:10 pm on Feb 21, 2005 (gmt 0)

10+ Year Member



That's the problem.
I can't seem to make $registration into a string.
When I print $registration it looks fine. 1011234.txt

when I put $registration in the part to open a file it does not work
When I put $registration ="1011234" . ".txt";
just before I try to open the file it works fine.
Thety both look the same to me.

I guess this part is wrong:

open NWEFH, ">>$registration";

Cheers

parrot

4:58 pm on Feb 22, 2005 (gmt 0)

10+ Year Member



Okay, I've found the problem.
The param $registration was tainted and I had to untaint it before I could use it to open and write a file.

Here's a piece of code that helped me out:
use CGI::Carp qw/fatalsToBrowser/; # Put this line somewhere under the shebang

Thanx all.

Ton

coopster

12:19 pm on Feb 23, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Good for you. Got some good helpers there too ;)

Welcome to WebmasterWorld, parrot.