perl的tell函数

发布时间:2020-11-09编辑:脚本学堂
perl的tell函数,注解都是英文,大家慢慢研读吧。函数名 tell 调用语法 tell (filevar); 解说 返回从文件头到当前位置的距离。

perl的tell函数,注解都是英文,大家慢慢研读吧。

函数名 tell
调用语法 tell (filevar);
解说 返回从文件头到当前位置的距离。

Syntax
tell FILEHANDLE
tell
Definition and Usage
Returns the current position of read pointer (in bytes) within the specified FILEHANDLE. If FILEHANDLE is omitted, then it returns the position within the last file accessed.
Return Value
Current file position in bytes

Example
To check this function do the followings
    Create a text file with "this is test" as content and store it into /tmp directory.
    Read 2 charcters from this file.
    Now check the position of read pointer in the file.
 

复制代码 代码如下:
#!/usr/bin/perl -w
open( FILE, "</tmp/test.txt" ) || die "Enable to open test file";
$char = getc( FILE );
print "First Charctaer is $charn";
$char = getc( FILE );
print "Second Charctaer is $charn";
# Now check the poistion of read poiter.
$position = tell( FILE );
print "Position with in file $positionn";
close(FILE);

It will produce following results:

First Charctaer is T
Second Charctaer is h
Position with in file 2