Perl tell 函式



描述

此函式返回指定的 FILEHANDLE 中讀取指標的當前位置(以位元組為單位)。如果省略 FILEHANDLE,則返回上次訪問的檔案中的位置。

語法

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

tell FILEHANDLE

tell

返回值

此函式返回當前檔案的位置(以位元組為單位)。

示例

以下示例程式碼展示了其基本用法,要檢查此函式,請執行以下操作 -

  • 建立一個文字檔案,內容為“this is test”,並將其儲存在 /tmp 目錄中。

  • 從此檔案中讀取 2 個字元。

  • 現在檢查檔案中讀取指標的位置。

#!/usr/bin/perl -w

open( FILE, "</tmp/test.txt" ) || die "Enable to open test file";
$char = getc( FILE );
print "First Character is $char\n";
$char = getc( FILE );
print "Second Character is $char\n";
# Now check the position of read pointer.
$position = tell( FILE );
print "Position with in file $position\n";
close(FILE);

執行以上程式碼後,將產生以下結果 -

First Character is E
Second Character is O
Position with in file 2
perl_function_references.htm
廣告