In order for the user to be typing interactively and have multiple lines assigned to a single scalar string, you have to tell the user to use some special string to indicate that the multi-line input is complete and it's time for the program to move on to the next task. If I wanted to do something like this, I would probably handle it as follows:
my %email; my @parts = qw/From To Subject cc bcc body/; for my $part ( @parts ) { my $prompt = ( $part eq 'body' ) ? " (type EOT alone on a line to finish):\n" : ": "; print "Enter value for $part$prompt"; $email{$part} = <STDIN>; if ( $part ne 'body' ) { chomp $email{$part} } else { while ( $email{$part} !~ /EOT\s+$/ ) { $email{$part} .= <STDIN>; } s/EOT\s*$//; } } # add more parts to %email as you see fit (priority, port, etc) # (updated to fix logic so that chomp is applied where needed, # also fixed bad quotation mark and confusion in variable names, # and added deletion of "EOT" from $email{body})
In other words, you have to implement the equivalent of a  << HERE block for user input from STDIN.

Having said that, I have to admit that I really dislike this sort of interactive input to a perl script. It's so easy to make a mistake while typing to a script's STDIN, and so hard to allow for a mistake to be fixed once it has been made. Most of the time, the user has to interrupt the script and just start over, and unless there is a fairly good facility for line editing, copy/paste, etc (see Term::ReadLine and related modules), it may take several tries to get all the inputs right.

It is much beetter to use the features already provided by the shell: pipes or files for multi-line input to a script, command-line args for essential parameters, and with the better shells (korn, bash) a very thorough handling of command line editing and ability to recall, modify and re-execute previous commands.

This way, the user composes their multi-line email body with a text editor of their own choosing and stores this to a plain text file; then they run your script with args on the command line to set the "from", "to", "cc", "bcc" and "subject" fields as needed, and feed the message body via a pipeline (or just have the file name as a last command line arg), to be read inside the script with  while (<>). Problems with the message body will be worked out in a standard text editor; if the user makes any mistakes with the other parameters, those are all on the command line, and the shell makes them easy to fix.


In reply to Re: <STDIN> input by graff
in thread <STDIN> input by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.