On 4/25/05, [EMAIL PROTECTED]
<[EMAIL PROTECTED]> wrote:
> I wish to read input from a user that has multiple lines with carriage
> returns from <stdin> but not actually stop reading until a single # on a
> line by itself. Is there a module / package / function that will aid in
> this? I would like to do something like below:
> 
> Until(<STDIN> = "#") {
> 
>             $incoming_lines = <STDIN>;
> 
> }
> 
> $storage_var = $incoming_lines;
> 
> This is most like NOT working code, it is a logic example so that you
> might understand what I am trying to accomplish, so please no comments on
> the actual code shown.
> 
> Chris Hood
> 
> 

Chris,

It really depends on what you want to do and how you want to get your input.

  my @input = <STDIN>;
  chomp @input;

is probably the simplest.  It relys on your OS to supply EOF and
terminate stdin.  On unix systems, that usually means pressinf Ctrl-D.

   my @input;
   while (<STDIN>) {
        chomp;
        push @input, $_;
   }

Is another option.  This lets you parse the text as it's entered, so
you can easily do things like create a custom EOF:

   while (<STDIN>) {
        chomp;
        last if /^#$/;
        push @input, $_;
   }

You could also replace that push with:

   $input .= " $_";

or something similar if you desperately wanted a scalar for some reason.

And finally, you could change the value of $/.  Just make sure you've
properly localized it, and no other files will be read in the same
scope.  Something like:

   sub get_input {
        local $/ = "\n#\n";
        my $input = <STDIN>;
        return $input
   }

will work fine, but you can get into a lot of trouble very quickly by
doing something like:

   $/ = "\n#\n";
   my $input = <STDIN>;
   open(FH, "<", "my/foo.txt");
   while (<FH>) {
        do something if ((/something/) and ($input =~ /something else/));
   }

HTH,

--jay

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to