Perl last 函式



說明

這不是一個函式。last 關鍵字是一個迴圈控制語句,它立即使當前迴圈的迭代變為最後一個迭代。沒有進一步的語句被執行,並且迴圈結束。如果指定了 LABEL,那麼它將退出由 LABEL 標識的迴圈,而不是當前包含的迴圈。

語法

以下是此函式的簡單語法 -

last LABEL

last

返回值

這不返回任何值。

示例

以下是顯示基本用法示例程式碼 -

#!/usr/bin/perl

$count = 0;

while( 1 ) {
   $count = $count + 1;
   if( $count > 4 ) {
      print "Going to exist out of the loop\n";
      last;
   } else {
      print "Count is $count\n";
   }
}
print "Out of the loop\n";

當執行以上程式碼時,會產生以下結果 -

Count is 1
Count is 2
Count is 3
Count is 4
Going to exist out of the loop
Out of the loop
perl_function_references.htm
廣告
© . All rights reserved.