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

I want to be able to put "pipe" data to clipboard, e.g.

dir | putclipboard.pl
or
putclipboard.pl "some data"

Win32::Clipboard puts text on clipboard. Problem is how do we check if STDIN contains data? I've tried IO::Select, but couldn't get it to work.

  • Comment on Checking if STDIN contains data on win32

Replies are listed 'Best First'.
Re: Checking if STDIN contains data on win32
by ikegami (Patriarch) on Oct 25, 2007 at 14:17 UTC

    It's easier to the opposite: Use STDIN if there are no args. It's better because text can always be added to STDIN, but args cannot be added once a program has started.

    my $text; if (@ARGV) { $text = shift(@ARGV); } else { local $/; $text = <STDIN>; }

    If the argument is a file name whose content you want to place on the clipboard, the ARGV file handle (the default for <>) does just that.

    my $text; { local $/; $text = <>; }

    If you still wanted to head down your original path, your best bet would be to check whether your process's STDIN has been redirected using -t STDIN (tested). select and therefore IO::Select only work on sockets in Windows.

Re: Checking if STDIN contains data on win32
by ldln (Pilgrim) on Oct 25, 2007 at 14:51 UTC
    -t STDIN
    always seem to return 1?!

    So, there is no (non-binding) way to check if STDIN contains data on win32? Is there any Win32 API functions that can come to the resuce?
    If we wanted to ask for console input and move on if none supplied within x secs, is another situation where checking STDIN would be useful (..and no, alarm doesn't work in this situation on win32).

      -t STDIN
      always seem to return 1?!

      Not for me:

      C:\> perl -lwe "printf '<%s>', -t STDIN" <1> C:\> perl -e1 | perl -lwe "printf '<%s>', -t STDIN" <> C:\> perl -lwe "printf '<%s>', -t STDIN" < AUTOEXEC.BAT <>

      - tye