If you don't post code for something like this it means you think there is something wrong with perl, and not with what you are doing :).
Strip down your script to just the open statement and look at it again. If you still can't see what's wrong, then post the code so we can all see it. In the meantime the following works for me (no added whitespace):
#!/usr/bin/perl -w
use strict;
use CGI ':standard';
open FH, '> data.txt' or die $!;
print FH 'testing123';
close FH;
print header(), start_html(-title => "test");
print "finished";
print end_html();
Hope that helps... | [reply] [d/l] |
open(FILE, '>', $file); truncates $file and opens it for writing.
open(FILE, '>>', $file); opens $file for appending.
open(FILE, '<', $file); opens $file for reading. See open in perlfunc for more information.
| [reply] [d/l] [select] |
Wow, I was just in the process in posting my code, which happens to be
open(DATA, ">/host/n/b/p/p/o/r/nbpdexplorers.port5.com/cgi-bin/data2.t
+xt") or die "Cannot open database file: $!";
flock(DATA, 2);
seek(DATA, 1, 0);
foreach my $item (@currentdata) {
print DATA "$item\n";
}
flock(DATA, 8);
close(DATA);
and I realized the only thing which was different from the other examples you gave me. I had been using the seek(DATA, 1, 0); because another monk (who I don't remember, sorry) had advised to use when writing to a file and using flock. I tried it without that line and it worked fine. Thanks so much for your examples and assistance.
Just Another Perl Wannabe | [reply] [d/l] [select] |
Aha! That's the bit of code you should have included in the first place :)
Reading up on seek, which I hadn't used before, what you seem to be doing is correctly opening your file, and then searching in the file for the point where you wish to begin.
seek(DATA, 1, 0);
is actually telling perl to offset the file by 1 byte and since it's a newly created file it simply creates the offset before it does anything else, hence the initial white space.
Normally you would use seek for fixed length records where you can be sure that the beginning of a particular record can be found at an exact number of bytes into the file, so you were either told this for a specific purpose which you took to be standard procedure, or you were just told wrong! :)
As I mentioned this is from my reading and interpretation and I rather imagine I will be corrected by other more experienced monks so please read for yourself and draw your own conclusions rather than rely on mine!
I hope this has been helpful to you... | [reply] [d/l] |