珀尔 |有用的文件处理功能
Perl 最初是为文本处理而开发的,例如从指定的文本文件中提取所需的信息并将文本文件转换为不同的形式。这些操作可以通过使用各种内置文件函数来执行。
例子:
#!/usr/bin/perl
# Opening a File in Read-only mode
open(fh, "<", "File_to_be_read.txt");
# Reading next character from the file
$ch = getc(fh)
# Printing the read character
print"Character read from the file is $ch";
# Closing the File
close(fh);
输出:
以下是 Perl 中用于处理文件的一些有用函数:
Function | Description |
---|---|
glob() | Used to print the files present in a directory passed to it as an argument |
tell() | Used to get the position of the read pointer in a File with the use of its FileHandle |
getc() | Used to read the next character from the file whose File Handle is passed to it as argument |
reverse() | Returns the List in reverse order when used in List context and returns a concatenated string of the values of the List, with each character of the string in the opposite order when used in scalar context |
rename() | Renames the old name of a file to a new name as given by the user |