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

Helpful Monks, I have a very simple problem that I just don't understand and cannot resolve. I have the following code,
open FILE, ">file.txt"; $record = <STDIN>; print FILE "The is the first line\n"; print FILE "$record\n"; close FILE;
This creates the file, but nothing is written into it? I'm working on XP Pro with Activestate PERL 5.8.6 build 811. Help! Alex

Replies are listed 'Best First'.
Re: Getting STDIN and writing to a file
by sk (Curate) on Jun 08, 2005 at 21:02 UTC
    works just fine on my system. XP Pro, AS 5.6.1.

    I would check for error on open.  open FILE, ">file.txt" or die $!;

    cheers

    SK

Re: Getting STDIN and writing to a file
by ruoso (Curate) on Jun 08, 2005 at 21:09 UTC

    Are you pressing Ctrl+C instead of typing something and pressing ENTER?

    The first line is executed, as the file is created (I'd include a "|| die $!" after the open just to be sure...

    I can't tell from what you say if the second is executed, but i doubt it isn't.

    And as it seems to hang before the third line, I could tell that eighter you're pressing Ctrl+C in the prompt and stopping the program, or it is stopping when trying to read STDIN. Or... you don't have enough disk space to write the string in the third line to the file (which would be almost impossible, as I doubt that the block size is smaller than that string).

    But there is no apparent reason to prevent this code from working... Unfortunally I don't have a way to test in the same environment you are. Anyway, I doubt it's a bug in activestate perl. daniel

Re: Getting STDIN and writing to a file
by PerlingTheUK (Hermit) on Jun 08, 2005 at 21:10 UTC
    This works just fine on my system (XP Home), same perl.
    I'd rather change
    print FILE "$record\n";
    to
    print FILE "$record";
    unless that is on purpose, as $record will normally be terminated with a newline.

    Cheers,
    PerlingTheUK
Re: Getting STDIN and writing to a file
by Anonymous Monk on Jun 08, 2005 at 21:33 UTC
    Thanks, I must have had fat fingers or something because this now works for me also. While I'm asking, is there a better way of getting keyboard input than stdin? Regards, Alex.
      Well, I don't know if this applies to Win32, but in *nix Term::Readkey and Term::Readline are the best options I can think of... but it'll depends mainly on what you're wanting to do exactly...