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

Hi Monks, Got a very basic doubt, my code is below--
use warnings; print "Enter the name of the file:\n"; $filename = <STDIN>; chomp($filename); open MYFILE, "$filename" or die "Error in opening: $!"; print "Enter your text:\n"; $new_text = <STDIN>; chomp($new_text); print "\nYour new file contains: $new_text\n"; open MYFILE, ">>$filename" or die "Error in opening: $!"; print MYFILE "$new_text\n"; while(my $line = <MYFILE>) { print "$line\n"; } close(MYFILE);
The contents of the file are not appended to the STDOUT. Please guide how to do that.

Replies are listed 'Best First'.
Re: Contents of file not printed to STDOUT
by ikegami (Patriarch) on Jan 31, 2011 at 17:51 UTC
    You are reading from a file opened for writing. You can solve this by reopening for the file for reading.
      Thanks, i got it working. @ananymous monk - I tried the suggestion already but did not work for me. I needed to reopen the file in read mode and that worked. Thanks
Re: Contents of file not printed to STDOUT
by Anonymous Monk on Jan 31, 2011 at 17:40 UTC
      ... you [nt2282] have a question, not a doubt ...

      Actually, nt2282 ought to have neither, as 'Basic doubt/question' is a very poor choice for a question title. Better, perhaps, might be something along the lines of 'Contents of file not printed/written/appended to STDOUT'.

      As nt2282 posted the OP as a registered user, it is possible for him or her to go back and change the title to a better one. (Update: But see GrandFather's Re^3: Contents of file not printed to STDOUT for even better.)

      Otherwise, I have no problem with the use of either 'question' or 'doubt' in such a context. I value clarity most, and I accept 'doubt' without question.

        The OP can do that, but can't rename replies that have inherited the original title. A better solution is for the node to be considered for retitling and have a janitor apply magic which does rename replies as appropriate.

        True laziness is hard work
      how can i do that. i am clueless now as the code looks correct to me.
      No, the OP has a doubt. More, a basic doubt, nay, a very basic doubt. You are fighting a battle that has been lost. You and I may not like it but that doesn't come into it.

      It's a friendly, polite post with code that shows what the problem is. nt2282++, (although I do agree that the title could have been better).

      You succinctly identified the root of the problem, good for you. Just leave out the lectures. :-)

Re: Contents of file not printed to STDOUT
by GrandFather (Saint) on Jan 31, 2011 at 20:24 UTC

    You've solved the immediate problem, but there are a few things that can be done to improve the code:

    Always use strictures (use strict; use warnings;).

    Use the three parameter version of open and lexical file handles:

    open my $inFile, '<', $filename or die "Failed to open $filename: $!";

    Note that $filename doesn't need to be interpolated into a double quoted string - it's fine just as it is.

    If you don't chomp the line you just read you don't need to append \n in the following print statements.

    True laziness is hard work