Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a Perl 6 script exit on Ctrl+D input?

Tags:

raku

loop {
  my $word = prompt '> ' ;
  say $word;
}

What's the right way to make it exit if/when instead of printing a word I press Ctrl+D?

like image 545
Eugene Barsky Avatar asked Dec 10 '22 08:12

Eugene Barsky


1 Answers

I'm less familiar with Perl 6 than with Perl 5, but the Perl 5 method seems to work:

loop {
  my $word = prompt '> ' ; 
  last if not defined $word;
  say $word;
}

This might be more idiomatic:

while (defined my $word = prompt '> ') {
    say $word;
}

(Without the defined operator, the loop will terminate on an empty input.)

like image 183
Keith Thompson Avatar answered Mar 02 '23 01:03

Keith Thompson