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

Hello Great Monks,
I'm sure this is very easy, but is there a way to capture multi-line input? I have this script. When I press <ENTER> key, it accepts the input. I'm thinking that if I want a multi-line input, then I should change the input terminator into something else... Any ideas?
#!/usr/bin/perl use strict; use warnings; print "Enter your guess: "; my $input = <STDIN>; chomp $input; print "$input\n";

Replies are listed 'Best First'.
Re: Multi-Line Input
by ssandv (Hermit) on Sep 23, 2009 at 00:25 UTC
    One way (certainly not the only way) to do it is to put your input routine in a loop: (warning, drylabbed code follows)
    my $answer; print "Enter your text: (type END to exit)\n"; while (<STDIN>) { last if /^END$/; $answer .= $_; }

      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.

      I hope this is of interest.

      Cheers,

      JohnGG

Re: Multi-Line Input
by BrowserUk (Patriarch) on Sep 23, 2009 at 01:17 UTC

    If you use readline in a list context, you can use ^Z/^D to terminate multiline input:

    C:\test>perl -E"my $in = join'', <STDIN>; say $in" the quick brown fox jumps over the lazy dog ^Z the quick brown fox jumps over the lazy dog

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Multi-Line Input
by Anonymous Monk on Sep 23, 2009 at 00:25 UTC
    You loop until input equals 'q' or empty line
Re: Multi-Line Input
by Anonymous Monk on Sep 23, 2009 at 00:26 UTC
Re: Multi-Line Input
by Anonymous Monk on Sep 23, 2009 at 11:00 UTC
    Try this code.
    #!/usr/bin/perl use strict; use warnings; print "Enter your guess: "; my @input = <STDIN>; print "@input\n";
    Use Ctrl+D to terminate the input.
Re: Multi-Line Input
by bichonfrise74 (Vicar) on Sep 23, 2009 at 22:11 UTC
    Just curious, why will CTRL-D terminate the input?