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

Part of the difficulty of my question is from not knowing the proper terminology for what I would call "command channels" (I/O pipes, maybe?). For instance STDIN/OUT is what I would call command channel "1", and STDERR would be "2". Commonly used in UNIX to redirect various outputs wherever we want them.

Ok, so this is simple. We've all done this. No big deal. Well, I need to be able to read input into my script from channel "3". That is it, and that is all. :)

An example:
# printf "this is my text on input 3" | ./myscript.cgi 3<&0

That last bit (for those not familiar with UNIX redirects), "3<&0" will redirect the piped input on channel "1" to channel "3". A very easy method to test for success. :)

Whelp, that's it! Does anybody have any idea on how to do this? My only knowledge of how to read in command line options is with "Getopt" and I am unaware of any way to specify input other than from STDIN (the default).

Thanks,
-the donco

Replies are listed 'Best First'.
Re: Reading in from CLI (UNIX)
by chip (Curate) on Dec 19, 2001 at 11:57 UTC
    Piece of cake:    open STDIN, "<&3" or die;

    I suggest you read "perldoc -f open"; it's quite instructive. Oh, and try "perldoc -f sysopen" too, just for kicks.

    UPDATE: Removed superfluous parens per my own advice to learn() or die;

        -- Chip Salzenberg, Free-Floating Agent of Chaos

      Why not take your advice at Two-arg open() considered dangerous and switch to the 3-arg version of the same?

      Sure, it doesn't make any real difference here, but a good habit is to be encouraged at all turns. :-)

        Using three-arg open for '<&3' would be a good idea, if only it were possible. But my docs say it's not, at least in 5.6. Quoting perldoc -f open:

        Duping file handles is not yet supported for 3-argument open().

        <getsmart>Missed it by this much.</getsmart>

            -- Chip Salzenberg, Free-Floating Agent of Chaos

Re: Reading in from CLI (UNIX)
by ariels (Curate) on Dec 19, 2001 at 13:12 UTC
    Beyond recommending everybody do as chip says, I'd like to correct some UN*X terminology. Your "channels" are what UN*X calls "file descriptors". It is not correct that Perl's STDIN is UN*X's file descriptor 1. STDIN is file descriptor 0:
    <diana 248 [10:08] /tmp >perl -le 'print fileno STDIN; print fileno ST +DOUT; print fileno STDERR' 0 1 2
    Many systems will behave as you expect if you write file descriptor 0 or read file descriptor 1. At least if they're connected to your terminal, /dev/tty. As soon as you redirect, you'll get something else. So "don't do that".