sabotodd has asked for the wisdom of the Perl Monks concerning the following question:

Hello monks!

This problem is really perplexing. I've successfully output to files for countless projects. However, this one baffles me: I can write a string to the file "Hello!", but I can't write a saved variable to that same file e.g. $savestr ???


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.

Replies are listed 'Best First'.
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.
      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.
        I've written it with out the lock, and it still causes the problem.
      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.

        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.

        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...

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;
      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
        My code seems to be correct..there's no reason why it shouldn't work. And I've written similar instructions b4
        FLAW! (Famous LAst Words) That sort of "affirmation" is the first sign that there is some bone-headed typo, forgotten detail, residual code snippet from a prior attempt, logical inconsistency, or whatever, that you can't see because you don't believe it's really there. I run into such cases -- and those same words go through my head -- at least four times a year (used to do it more often, before I got the hang of "use strict"...)

        When you get to the point of believing that your code must be right even though it doesn't work, it's time to step through it with the debugger: inspect the values of the variable(s) in question and see how the conditionals behave as you go. That will usually pay off.

        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.
        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.
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
      I want to print what is contained in $savestr. I've tried putting the $savestr without the double quotes..it doesn't work either.