in reply to STDIN problemo

#!/usr/bin/perl my @piped = <STDIN>; print "last piped element: ".$piped[-1]; close STDIN; open STDIN, "<", "/dev/tty" or die "Couldn't open console: $!"; # "con" on Windows (IIRC) print "> "; my $user_input = <STDIN>; print "user input: ".$user_input; __END__ $ echo foo |./813419.pl last piped element: foo > bar user input: bar

Replies are listed 'Best First'.
Re^2: STDIN problemo
by ww (Archbishop) on Dec 18, 2009 at 22:13 UTC

    re line 8 for windows: perhaps con: or CON: (either appears to work on the rather dated W32 box available right now):

    open STDIN, "<", "/con:" or die "Couldn't open console: $!";

    To execute a command * found in $user_input simply insert

    system($user_input);

    after line 11 of almut's elegant no-module solution -- which observation is in no way intended to deprecate ikegami's earlier reply using a module which also appears to fit the OP's needs. Both are easily extensible./p>

    Update: * ...but this fails with some commands, notably with cd or chdir, although it does "work" with commands like dir or perl -c script.pl.

      Thanks! that was perfect.

      Just getting to know this language or scripting language.. and i love it! i can do so many things whit so few code.