Recently, whilst trying to save a program I had written as con.pl under Win XP, I got an error message telling me that con was a "reserved device name". I added a v, saving the program as "conv.pl".

Running the program later at the command line, I mistakenly typed "perl con.pl" instead of "perl conv.pl". To my surprise the perl interpreter did not stop in its tracks with a "No such file or directory" error. Instead it presented me with a prompt, as if I had given it a filename of "-" (which will cause the interpreter to read program code from STDIN, and execute it after you have entered EOF, usually ^D).

I keyed in print 'Hi', entered ^D, and pressed <ENTER>. The interpreter ran the print command, then exited without event. After some thought, I reasoned that it was behaving in this way because I had given it the Windows device "con", and it had treated it as any other filehandle. It seems windows will treat con.* as con. I tested this by running "perl con.bat" , "perl con.exe", and even "perl con.perlmonks". They all worked out as before, reading from STDIN, and executing it.

I then used the nul device, which is similar to Unix's /dev/null. The interpreter exited without a word, as I had expected, since the nul device leads nowhere.

I then took the next logical step, opening both the con and nul devices as filehandles in a test program I created.

#!perl use strict; use warnings; open(IN,"con") || die $!; print "Enter a line:"; my $line=<IN>; print "Right back at ya: $line"; open(OUT,">nul") || die $!; print OUT "Garbage Out";
And it worked. It read from the keyboard in line 7, and wrote to the nul device in line 11.

I am not really sure what use this could have but I posted it here mainly as further proof of perl's TMTOWTDI nature.

And of course, to win a wager or two with( you know, "I bet ya I can read from the keyboard without using the STDIN filehandle").

update (broquaint): title changed (was Reading from STDIN...without using STDIN)

Replies are listed 'Best First'.
Re: Reading from the keyboard in Win32 without using STDIN
by Juerd (Abbot) on Jun 02, 2003 at 13:33 UTC

    In unix: /dev/tty and /dev/null. I bet Windows/DOS has no cool random device like /dev/urandom.

    By the way, if you use an MS-DOS CD driver, a similar device is created. That means that if you used /D:MSCD000, you can no longer use MSCD000.anything as a filename. Big deal. But if you were lazy and used /D:CDROM, you'll find that programs can no longer install or use cdrom.dll. which can be quite annoying.

    Juerd # { site => 'juerd.nl', plp_site => 'plp.juerd.nl', do_not_use => 'spamtrap' }

Re: Reading from the keyboard in Win32 without using STDIN
by hardburn (Abbot) on Jun 02, 2003 at 14:07 UTC

    "con" on DOS machines (remember: DOS isn't dead, it just smells that way), is the "console", roughly equivilent to *nix /dev/tty devices. Not to burst your bubble, but it doesn't really have anything to do with Perl.

    Despite Microsoft's efforts to cover it up, Windows really does have devices setup as pseduo-files, just as *nix people expect. I've noticed this truth surface more often in NT-based systems than the old DOS-based ones.

    There is also an old bug that causes Win9* machines to crash when you try to open C:\con\con (see this Microsoft security post). Actually, it crashes on many (all?) DOS device names. C:\con\con is just the most popular one to use.

    ----
    I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
    -- Schemer

    Note: All code is untested, unless otherwise stated

Re: Reading from the keyboard in Win32 without using STDIN (File::Spec)
by tye (Sage) on Jun 02, 2003 at 15:15 UTC

    You might want to use File::Spec, which knows about special device files for different operating systems:

    use File::Spec::Functions qw( devnull ); open( OUT, ">".devnull() ) || die $!;
    and someone needs to submit a patch to provide a devtty() function. (:

                    - tye
Re: Reading from the keyboard in Win32 without using STDIN
by jand (Friar) on Jun 02, 2003 at 20:36 UTC
    This is an artifact of the CP/M heritage of DOS/Windows. Under CP/M many programs didn't have a "print" function; you would just save to "prn". Since some programs always appended an extension (e.g. ".txt") when saving to disk(ette), the OS would ignore file extensions when writing to device driver names.

    When PC-DOS started to support subdirectories in version 2.0, they ignored the subdirectory part while checking for device names. So you could still just save to "prn.txt" in any subdirectory and have your data printed. In the name of "backward compatibility" this behavior is still present in Windows today.

Re: Reading from the keyboard in Win32 without using STDIN
by John M. Dlugosz (Monsignor) on Jun 02, 2003 at 19:43 UTC
    Could be worse. One time I tried to save some commentary as "COM1.TXT" on a DOS machine, and crashed it.