in reply to Re: Multi-Line Input
in thread Multi-Line Input

The problem is finding something to terminate your input that will not legitimately occur in that input. You can use BrowserUk's suggestion of ^Z/^D in an endless version of your while loop, breaking out with a test for eof.

use strict; use warnings; my $prompt = q{Add input (CTRL-D to finish) : }; my @input = (); print $prompt; while( 1 ) { last if eof STDIN; push @input, scalar <STDIN>; print $prompt; } print qq{\n}; chomp @input; print qq{Your input:\n}, do { local $" = qq{\n -->}; qq{ -->@input}; }, qq{\n};

Running it (the terminating ^D does not show up)

knoppix@Knoppix:~/perl/Monks$ ./spw796843 Add input (CTRL-D to finish) : This is Add input (CTRL-D to finish) : a few words Add input (CTRL-D to finish) : of nonsense Add input (CTRL-D to finish) : Add input (CTRL-D to finish) : including a Add input (CTRL-D to finish) : blank line Add input (CTRL-D to finish) : Your input: -->This is -->a few words -->of nonsense --> -->including a -->blank line knoppix@Knoppix:~/perl/Monks$

I usually tidy up the last prompt and the ^D (which, IIRC, does appear on Solaris) with the following change to the code.

use strict; use warnings; my $prompt = q{Add input (CTRL-D to finish) : }; my $maskPrompt = qq{\r} . ( q{ } x ( length( $prompt ) + 2 ) ) . qq{\r}; my @input = (); print $prompt; while( 1 ) { last if eof STDIN; push @input, scalar <STDIN>; print $prompt; } print $maskPrompt; chomp @input; print qq{Your input:\n}, do { local $" = qq{\n -->}; qq{ -->@input}; }, qq{\n};

I hope this is of interest.

Cheers,

JohnGG