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

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

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

 



lost
Deleted

Jan 25, 2001, 3:53 PM

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

Hi,

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.




modified
Deleted

Jan 25, 2001, 3:55 PM

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

"But it does work"

So what's the problem?



lost
Deleted

Jan 25, 2001, 4:08 PM

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

Sorry....

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



rGeoffrey
User / Moderator

Jan 25, 2001, 7:18 PM

Post #4 of 5 (393 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...


Code
$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?


pu_nk
journeyman

Jan 25, 2001, 7:27 PM

Post #5 of 5 (392 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") ;

etc...
Hope this helps,
Drew





(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