Re: can't write a variable to a file
by ysth (Canon) on May 11, 2004 at 09:03 UTC
|
Can you show the code that sets $savestr and calls Savetofile()? I suspect that $savestr is not a global variable and the Savetofile function isn't in it's scope. | [reply] |
|
|
as per docs...you never unlock a file..just close it and it becomes unlocked and is flushed. unlocking before close (where it's flushed) is causing the problem....especially that you don't write "\n", which forces a buffer flush also.
| [reply] |
|
|
I've written it with out the lock, and it still causes the problem.
| [reply] |
|
|
The variable is not contained in any subs. I've tried just holding: $savestr="hello!" and it still doesn't work.
but if I pass "hello!" directly, it works.
$savestr="$optionstr\n$total\n$countstr";
print $savestr;
#save data back to file
#save edited data back to file
####################save file
sub Savetofile{
#open and read each line
open(OUTFILE,">../kicks.txt")or
die("Cannot open file");
flock(OUTFILE, LOCK_EX);
print (OUTFILE "$savestr");
flock(OUTFILE, LOCK_UN)or die("Cannot unlock
file");
close (OUTFILE) or die("Cannot close file");
}
Edited by Chady -- added code tags.
| [reply] [d/l] |
|
|
a subroutine doesn't get executed unless you call it; this:
print "a";
sub doit { print "zed" }
print "b";
&doit();
prints "a", then "b", then "zed". After the print "a", perl jumps right over the sub definition to the next plain statement. | [reply] [d/l] |
|
|
|
|
|
|
You don't show where you are actually making the call to Savetofile. It needs to be somewhere after assigning to $savestr. Can you paste all the code, and explain (with examples) what you mean by 'if I pass "hello!" directly'.
A better solution might be to pass the string as a parameter to Savetofile:
$savestr = "whatever\n";
&Savetofile($savestr);
sub Savetofile
{
my $string = shift;
if (defined($string)) {
# all the open/lock/print/close stuff goes here
}
}
Also can you please put your code in <code> tags, it makes it a lot easier to read! Thanks...
| [reply] [d/l] [select] |
Re: can't write a variable to a file
by nimdokk (Vicar) on May 11, 2004 at 10:24 UTC
|
Try this sample code. I tested it and it runs on Win32 just fine (should run just fine on any platform with Perl).
use strict;
use warnings;
my $string="Hello, world!";
print "$string\n";
open OUTPUT, ">sample.txt" or die "Cannot open sample.txt for writing.
+ $!";
print OUTPUT $string;
close OUTPUT;
exit;
| [reply] [d/l] |
|
|
I haven't tried your code. I am sure it works..The thing is.. My code seems to be correct..there's no reason why it shouldn't work. And I've written similar instructions b4
| [reply] |
|
|
| [reply] |
|
|
If you could display all of your code it would be very helpful. Sounds like a simple problem, but need to see all the code to debug.
| [reply] |
|
|
OK, my suggestion would be to try to isolate out the part that isn't working. Perhaps take the code out of the subroutine and see if that might be causing the problem, and so on.
| [reply] |
Re: can't write a variable to a file
by Anonymous Monk on May 11, 2004 at 08:40 UTC
|
print (OUTFILE "$savestr");
do you want print the content of $savestr or the word '$savestr' ?
print OUTFILE '$savestr'; # print out $savestr
print OUTFILE "\$savestr"; # print out $savestr
| [reply] [d/l] [select] |
|
|
I want to print what is contained in $savestr. I've tried putting the $savestr without the double quotes..it doesn't work either.
| [reply] |