use strict; use warnings; use IO::Select; ... my $text; if ( @ARGV ) { if ( -r $ARGV[ 0 ] ) { $text = do { local $/; <>; }; } else { $text = join q{ }, @ARGV; } } else { my $select = IO::Select->new(); $select->add( \ *STDIN ); print qq{Type text, finish with ctrl-D on its own line\n} unless $select->can_read( 0.05 ); $text = do { local $/; <>; }; } ... # Do something with $text