OK, so this is so simple but for the life of me I can't figure out why the code below doesn't work.
I'm trying to simply write a CGI script that creates sequentially numbered files. I'm using a counter (stored in a separate file) to keep track of the last ordinal used, and then generating a unique filename using sprintf
. The uniquely named file is NOT created. I suspect it's an issue with sprintf(...)
not correctly converting $ordinal to a scalar?
If I assign $ordinal
by say replacing the line $ordinal = <NUMPHOTOS>;
with $ordinal=42;
the code works fine and a file named 00000042.jpg
is created.
What am I doing wrong here?
Help!
my ($filename, $ordinal);
local $| = 1;
print "Content-type: text/plain\n\n";
# NOTE: $ordinal is set to zero if the file doesn't exist
open (NUMPHOTOS, "<numpics.dat");
$ordinal = <NUMPHOTOS>;
print "ordinal = $ordinal";
$filename = sprintf("%08d.jpg", $ordinal );
close (NUMPHOTOS);
open (NUMPHOTOS, ">numpics.dat");
$ordinal += 1;
print NUMPHOTOS $ordinal;
close (NUMPHOTOS);
open ( UPLOADFILE, ">$filename" ) or die "ERROR: can't open $filename: $! \n";
print "writing out file $filename...\n";
print UPLOADFILE 'hello world';
close UPLOADFILE;