Re: writing a file
by Fastolfe (Vicar) on Oct 03, 2000 at 08:00 UTC
|
Please provide additional information. An error message would be most helpful. I can't see anything immediately obvious with your code (except close does not use a 2nd filename argument like you're providing). You should also be checking the return values from your open statement, and ideally even your close statement:
open F, ">client.csv" or die "Couldn't open client.csv: $!";
...
close F or die "Error writing to client.csv: $!";
| [reply] [d/l] |
|
|
oops. sorry, didn't align my preformatted text. anyways, here's an easier-to-read reply Ü
basically, the system retrieves data from the database
depending on the value of the fields (analyst, account
type, region, etc...). after generating a report (which is
displayed on the webpage), the fields are stored in the csv
file (client.csv and analyst.csv) in order to generate
their equivalent graph values (Excel graph).
here's the code for generating client.csv....
open F, ">client.csv" or die "couldnt create file $!";
print F "CLIENT NAME,DC,FF,OTHERS\n";
while ((@xgdat) = $dbh->dbnextrow(1))
{
$xgdat[0] =~ s/\,/ /g;
print F "$xgdat[0],$xgdat[1],$xgdat[2],$xgdat[3]\n";
}
close F, "client.csv";
apparently, the data IS written to the file provided the
command (i.e. print F "$xgdat0,$xgdat1,$xgdat2,$xgdat3\n";)
is NOT in a loop. we tried writing it to the console (w/
the command still in the loop) and there were no problems
encountered. we still don't know why this happens.
btw, we also encountered an error with the die statement
after opening the file.
thanks again!
| [reply] [d/l] |
|
|
So let me get this straight:
- You got an error message while attempting to open your file (via die)
- If you print to the console instead of to your file, everything succeeds
- If you try to print to your file (which failed to open per #1), it fails
Hmm...
Did you try looking at the error message? What did the error message say? If you got an error message while attempting to open your file, I would probably have to say that the file was not opened, which explains why your print statement to the file failed to work.
Note that "die" isn't giving you an error message. It simply relays the error message to you and stops execution of your script. If the open call failed, you would not know about it unless you checked the return value of open, and did something with it (in this case, we use 'or die ...' to print out an error message if something went wrong and exit the script).
Clearly, your open is failing, for the reasons explained by the error message you received.
| [reply] |
|
|
ok Ü
basically, the system retrieves data from the database depending on the value of the fields (analyst, account type, region, etc...). after generating a report (which is displayed on the webpage), the fields are stored in the csv file (client.csv and analyst.csv) in order to generate their equivalent graph values (Excel graph).
here's the code for writing to client.csv....
open F, ">client.csv" or die "couldnt create file $!";
print F "CLIENT NAME,DC,FF,OTHERS\n";
while ((@xgdat) = $dbh->dbnextrow(1))
{
$xgdat[0] =~ s/\,/ /g;
print F "$xgdat[0],$xgdat[1],$xgdat[2],$xgdat[3]\n";
}
close F, "client.csv";
apparently, the data IS written to the file provided the command (i.e. print F "$xgdat[0],$xgdat1,$xgdat2,$xgdat3\n";) is NOT in a loop. we tried writing it to the console (w/ the command still in the loop) and there were no problems encountered. we still don't know why this happens.
btw, we also encountered an error with the die statement after opening the file.
thanks again!
| [reply] [d/l] |
Re: writing a file
by arturo (Vicar) on Oct 03, 2000 at 17:31 UTC
|
What error did you encounter with the die?
I don't have a detailed diagnosis -- the list of symptoms you describe is very odd (it doesn't seem to be a permissions problem, unless that's what your die is telling you).
What is the use FileHandle doing here? You don't need it to write to a filehandle, the
module is just there to let you treat a filehandle like a scalar. As far as this bit of code is concerned, you
never use it. Consider eliminating it (although I don't know that that's the problem).
Other questions:
You don't mention specifically whether the print statement *outside* the loop writes to the file,
or whether the line of data you've fetched from the DB prints *if* you put it outside the loop.
If the header line prints but not the line within the loop, that would suggest to me that your DB call is failing (why wouldn't it fail when you're printing to the screen? Beats me.)
It's odd that it would write to STDOUT and not to the file if there isn't some problem opening the file.
Update: try adding an "or die" to the $dbh->execute call; that might help here.
Try replacing everything from open on down with something like this:
my $file = "path/to/client.csv"; # make sure this location is writable
+!
open F, ">$file" or die "Couldn't create $file: $!\n";
print F "CLIENT NAME,DC,FF,OTHERS\n";
while(@row = $dbh->dbnextrow()) {
print F join(/,/, @row), "\n";
}
close F;
Philosophy can be made out of anything -- or less | [reply] [d/l] [select] |
Re: writing a file
by jreades (Friar) on Oct 03, 2000 at 17:40 UTC
|
btw, we also encountered an error with the die statement after opening the file. thanks again!
I have to second Falstofe's request for the error messages because there's nothing obviously wrong with what you're doing -- there might be a few ways to clean things up and make them more readily readable but nothing leaps out...
I think the key here is the quote I highlighted above -- you shouldn't be having problems with die unless you're having problems writing to the file... what kind of message was die throwing your way?
And, as an aside, here's one way to clean things up code-wise (of course, it's always a matter of preference in style)...
use Sybase::DBlib;
use FileHandle;
my $dbh = new Sybase::DBlib 'lthomas', 'lthomas', 'HKT_ERCC';
######################
# I'm not familiar with Sybase, but can you prepare the dbh query?
# That would clean up the following section
######################
$dbh->dbuse('er_cc');
$dbh->dbcmd("exec sp_client_csv \"$month1\",\"$FORM{'cboDay1'}\",\"$FO
+RM{'cboYear1'}\",\"$month2\",\"$FORM{'cboDay2'}\",\"$FORM{'cboYear2'}
+\"");
$dbh->dbsqlexec;
$dbh->dbresults;
open (FILE, ">client.csv") or die ("Couldn't open output file to write
+: $!");
#####################
# FYI your original post has a semi-colon after the while statement an
+d before the '{'.
# Is that in your real code?
#####################
while (@xgdat = $dbh->dbnextrow(1)) {
print FILE join (',', @xgdat), "\n";
}
close FILE or die ("Couldn't close output file: $!");
| [reply] [d/l] |
|
|
| [reply] |