By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,712 Members | 1,958 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,712 IT Pros & Developers. It's quick & easy.

Saving a Form to a file

P: 5
Hi i am trying to write a perl script to save a form to a file. Can anyone help?
Jul 21 '08 #1
Share this Question
Share on Google+
10 Replies


numberwhun
Expert Mod 2.5K+
P: 3,503
Sure, but we have to know things like:
  1. what you have tried thus far
  2. what errors you are seeing
  3. what is not working

If you can post your code (surrounded by code tags please) and provide us the other information, we will do our best to help you.

Regards,

Jeff
Jul 21 '08 #2

P: 5
i was able to save it to a file but now i just want to read in the file and just print out the data on a new line for each element. here is the code: i want the output to look exactly like how it is saved in the file
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use CGI qw(:standard);
  5. use CGI qw(:cgi);
  6.  
  7.  
  8. my $data_file = "/tmp/results.txt";
  9. my $configuration;
  10. my $config_ID;
  11. my $cds_ID ;
  12.  
  13. open (DAT, $data_file) || die ("could not open file");
  14.  
  15. my @raw_data = <DAT>;
  16. close(DAT);
  17.  
  18.  
  19. print "Content-type: text/html \n\n";
  20. print "<html><body>";
  21.  
  22.  
  23.  
  24. print "(@raw_data)";
  25.  
  26.  
  27.  
  28.  
  29. print "</body></html>";
  30.  
********************data from file************************

txtCfgId=test
txtCfguser=test
txtEmailAdr=
txtPager=test
txtSrvOU=test
txtProject=rhga
pnlServerID=eaga
txtSrvrIP=egar
txtSrvrGW=agae
txtSrvrMask=eaga
lstTimeZone=US%2FCentral
txtDNSDom=aega
txtDNSHold1=efar
txtDNSHold2=ergar
txtDNSHold3=
Jul 24 '08 #3

numberwhun
Expert Mod 2.5K+
P: 3,503
nmeliasp,

First, the code tags that I asked you to use to surround your code were not optional. They are required for the forums and your not putting them in requires a moderator to add them for you. Please add them to your code next time, as I requested earlier!

As for your issue, the topic of your post seems to be misleading. It is saying that you want to save a form to a file yet in your latest post, you are wanting to display the contents of a file on the screen, as it appears in the file.

When you run what you have, how does the output look?

Personally, in place of this line:

Expand|Select|Wrap|Line Numbers
  1. print "(@raw_data)";
  2.  
I would write it as so, but this is just me:

Expand|Select|Wrap|Line Numbers
  1. foreach my $line (@raw_data) {
  2.     chomp($line);
  3.     print("$line\n");
  4. }
  5.  
I didn't test it, but that should print out the file, line by line, making it look on the screen as it did in the file.

Regards,

Jeff
Jul 24 '08 #4

P: 5
this is the output of the code in an internet browser which is what i want the output to be in:

VTI-GROUP=0 txtCfgId=test txtCfguser=test1 txtEmailAdr=nmeliasp%40yahoo.com txtPager=test txtSrvOU=test txtProject=rhga pnlServerID=eaga txtSrvrIP=egar txtSrvrGW=agae txtSrvrMask=eaga lstTimeZone=US%2FCentral txtDNSDom=aega txtDNSHold1=efar txtDNSHold2=ergar txtDNSHold3= Submit=Submit =
Jul 24 '08 #5

P: 5
it was the same when i tried that piece of code you suggested
Jul 24 '08 #6

numberwhun
Expert Mod 2.5K+
P: 3,503
it was the same when i tried that piece of code you suggested
He he, I didn't even take that into account, sorry, long day. Try this and see if it works:

Expand|Select|Wrap|Line Numbers
  1. foreach my $line (@raw_data) {
  2.     chomp($line);
  3.     print("<p>$line</p>");
  4. }
  5.  
Let me know if that works.

Regards,

Jeff
Jul 24 '08 #7

KevinADC
Expert 2.5K+
P: 4,059
this is the output of the code in an internet browser which is what i want the output to be in:

VTI-GROUP=0 txtCfgId=test txtCfguser=test1 txtEmailAdr=nmeliasp%40yahoo.com txtPager=test txtSrvOU=test txtProject=rhga pnlServerID=eaga txtSrvrIP=egar txtSrvrGW=agae txtSrvrMask=eaga lstTimeZone=US%2FCentral txtDNSDom=aega txtDNSHold1=efar txtDNSHold2=ergar txtDNSHold3= Submit=Submit =
Output in a browser requires HTML markup to display properly. Jeff posted a simple example but this is not the HTML forum. If you need to learn the basics of HTML find an HTML tutorial or read the CGI.pm modules documentation for generating HTML output.


Jeff,

this is not a good example:

Expand|Select|Wrap|Line Numbers
  1. foreach my $line (@raw_data) {
  2.    chomp($line);
  3.    print("$line\n");
  4. }

FIrst it makes little sense to chomp() the input only to add a newline to print the output. But the bigger issue is the use of parenthesis around the argument to the print command. That is really almost never needed, although there are times, when it is, like this:

Expand|Select|Wrap|Line Numbers
  1. print (split(//))[0],"\n";
but if you do that it will print nothing, it will in fact throw a syntax error . You have to use some odd syntax to make it work:

Expand|Select|Wrap|Line Numbers
  1. print ((split(//))[0]),"\n";
or:

Expand|Select|Wrap|Line Numbers
  1. print +(split(//))[0],"\n";
But in this example the newline will not be printed:

Expand|Select|Wrap|Line Numbers
  1. print ((split(//))[0]),"\n";
because the print function expects a list, and the parenthesis create a list, so after the last parenthesis the list ends and the newline is ignored. You have to put the newline inside the parenthesis to make it part of the list. In this example it will print the newline:

Expand|Select|Wrap|Line Numbers
  1. print +(split(//))[0],"\n";
But that is rather obscure syntax and to the unsuspecting perl coder it will look like it should not even work. Hell, I don't even understand why it works but its documented in the print functions manpage.

The best practice is to avoid using parenthesis around the arguments of the print function unless it is really necessary.

Damn, I drank too much coffee this morning.
Jul 24 '08 #8

eWish
Expert 100+
P: 971
Damn, I drank too much coffee this morning.
Is this becoming a problem for you?

--Kevin
Jul 26 '08 #9

KevinADC
Expert 2.5K+
P: 4,059
Is this becoming a problem for you?

--Kevin
Maybe ..........................
Jul 26 '08 #10

P: 5
yes that worked!!! Thank you
Jul 28 '08 #11

Post your reply

Sign in to post your reply or Sign up for a free account.