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

I'm on a Win98 system (please, no heckling) and run the following code:
while (<>) { print; } print "hello";
Now, the theory is that CTRL-Z generates the EOF character and when pressed, "hello" should be spit out. But you probably guessed by now it ain't workin' for me. The program just terminates. Anyone have an explanation/solution?

$PM = "Perl Monk's";
$MCF = "Most Clueless Friar Abbot Bishop";
$nysus = $PM . $MCF;
Click here if you love Perl Monks

Replies are listed 'Best First'.
Re: Generating EOF character on Win98
by nysus (Parson) on Jul 23, 2001 at 22:30 UTC

    Here is the answer...

    Per Albannach's wise suggestion, I hunted through ActiveState's bug tracker. In a nutshell, the answer is to put a "\n" before "hello", like so: "\nhello"; Apparently, the command.com drops the first line of the output to STDOUT. See bug tracker report. This is not a Perl bug, but a command.com bug.

    $PM = "Perl Monk's";
    $MCF = "Most Clueless Friar Abbot Bishop";
    $nysus = $PM . $MCF;
    Click here if you love Perl Monks

      I scrapped COMMAND.COM more than ten years ago. Download 4DOS.

      Edit by tye to close " of HREF

Re: Generating EOF character on Win98
by virtualsue (Vicar) on Jul 23, 2001 at 21:46 UTC
    I have a Win98 system running Perl v5.6 that I booted up especially for you. :) Your code works fine, if, as I suggested in the Chatterbox, you add a newline to your last print statement (i.e. print "hello\n"). STDOUT isn't getting flushed on exit.
      Well, I did give your recommendation a shot but it just didn't work. This is weird.

      $PM = "Perl Monk's";
      $MCF = "Most Clueless Friar Abbot Bishop";
      $nysus = $PM . $MCF;
      Click here if you love Perl Monks

      It works for me even without the final newline.

      Another interesting point is that nysus mentioned that pressing CTRL-Z causes the program to terminate immediately where my experience (and current testing) shows that you have to press ENTER after the CTRL-Z for the EOF to be noticed by the program.

      I'm not sure where to look next but I'd probably look at the "DOS properties" of the window that the script is running in. Perhaps the CTRL-Z key sequence has been found to some other function? (Though I can't see how even that would explain the stated behavior.)

              - tye (but my friends call me "Tye")
        Oh brother. Now I know why I feel a need to keep a crucifix handy when I work on a PC. On my setup it works the first time I run it (defined as printing 'hello' after ^Z is pressed). Forever after that (well, several times, until I get bored) the 'hello' is not displayed. I have to reboot the PC in order to see 'hello' again. Très weird.

        I have only investigated far enough to note that, as you say, the newline doesn't appear to figure in this at all. It works the first time, and then not again until I restart. Killing the MS-DOS window and starting another one doesn't help.

      You don't need a final newline under Win32, either to make it flush or to prevent the prompt from overwriting your line.

Re: Generating EOF character on Win98
by Anonymous Monk on Jul 23, 2001 at 22:24 UTC
    Well, I get the same thing happening to me whenever I run any scripts on Windows. What do you mean by "terminates"?

    If you mean that the program exits and returns you to the C:\ prompt (or wherever) then I have no idea.

    However, what always happened to me is that the DOS window I was running the thing in would simply vanish. If this is the problem, do not run the perl script by double-clicking on the icon. In my case would run fine, but when the last bit of output is reached (In this case, your "hello") it would print it and then IMMEDIATELY kill the window.

    The solution was to open the DOS window first (Start --> Programs --> MS-DOS Prompt if my memory is holding out) and then run your script using perl "location" where "location" is the path to your script.

    It's also worth noting that if Perl isn't in your PATH, that won't work unless you're in your Perl directory.

      It's also worth noting that if Perl isn't in your PATH, that won't work unless you're in your Perl directory

      It works if you have an alias set up, so Perl expands into the fully-qualified name.

      It works if your shell looks in the "App Paths" registry settings.

Re: Generating EOF character on Win98
by John M. Dlugosz (Monsignor) on Jul 24, 2001 at 01:19 UTC
    Did you hit Enter after the print line, and then press ^Z? IME, it only works at the beginning of a line. Something to do with the way the shell buffers it, I guess.