CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Beginner:
Saving data to a unique filename



Jan 25, 2001, 3:53 PM

Post #1 of 5 (411 views)
Saving data to a unique filename Can't Post


I am learning perl and trying to save user input data to a unique filename. I need help!
I have a form that allow user to enter data (date,name,email, etc.) and I want to have this info save in file using date or name as a filename. This in effect create a unique filename for each user.
Can you help?

I have something like this:

$file = "/home/httpd/cgi-bin/LTS/data/";
sub Add {
open(DATA, ">$file/$Date") || die "Couldn't open $file_name.";
print DATA "$FORM{'Date'}|$FORM{'LMP'}|$FORM{'SMP'}|$FORM{'OMP'}|$FORM{'TMP'}|$FORM{'CMP'}|$FORM{'Comments'}\n";
close DATA;

But it does work.


Jan 25, 2001, 3:55 PM

Post #2 of 5 (409 views)
Re: Saving data to a unique filename [In reply to] Can't Post

"But it does work"

So what's the problem?


Jan 25, 2001, 4:08 PM

Post #3 of 5 (408 views)
Re: Saving data to a unique filename [In reply to] Can't Post


I meant to say it doesn't work!!!

User / Moderator

Jan 25, 2001, 7:18 PM

Post #4 of 5 (402 views)
Re: Saving data to a unique filename [In reply to] Can't Post

Here are some things that may or may not have anything to do with your problem...

Are you sure that "/home/httpd/cgi-bin/LTS/data" exists and the script has permission to write to it?

Do $Date, $file_name, and %FORM exist and have values?

No variables were passed to the function. Are you sure you want to rely on global variables here? And do they still exist for the function?

You are picking a filename based on $Date. But if two people come along on the same day, will you have them using the same file name? And if they will share should you have the open do an append using ">>$file"?

On the left side of the || in the open line you have $file but on the right side you have $file_name. Is that what you wanted?

And now some bonus thoughts that won't fix anything now, but I can't help myself...

You did an "|| die" but usually it is written as "or die". In this case you have parentheses on the left side so it does what you want, but if you forget them and say...

open DATA, ">$file/$Date" || die "Couldn't open $file_name.";

you will find that || binds stronger than open so perl will behave as if you said...

open DATA, (">$file/$Date" || die "Couldn't open $file_name.");

When things die it is usually considered good form to print $! in the die statement.

And just to be annoying, do you really have to use the "|" as the separator? It is a character that makes splitting harder because it needs to be done as split /\|/ not just split /|/.

So I would write it as...

$file = "/home/httpd/cgi-bin/LTS/data/"; 
$Date = '0125'; #or something else you did not show us

&Add ($file, $Date, %FORM);

sub Add
my ($file, $Date, %FORM) = @_;

$file .= "$Date";
open(DATA, ">$file") or die "Couldn't open $file, $!\n";
print DATA join ('|', map { $FORM{$_} } qw (Date LMP SMP OMP TMP CMP Comments)), "\n";
close DATA;

Sun Sep 9, 2001 - 1:46:40 GMT, a very special second in the epoch. How will you celebrate?


Jan 25, 2001, 7:27 PM

Post #5 of 5 (401 views)
Re: Saving data to a unique filename [In reply to] Can't Post

-- OPPS.. looks like someone posted a reply just before me and i did not reload my page.... Here is some code anyway...

Try taking the die off.

I think that is not allowing it to creat the file...

So it should look like so..
open(DATA, ">$file/$Date") ;

Hope this helps,

(This post was edited by pu_nk on Jan 25, 2001, 6:44 PM)


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives