打印出GLOB()而不是变量名

时间:2015-08-18 12:59:16

标签: perl

我已经定义并打开了一个文件名:

open my $file1, '<', 'test_dll.txt';

现在,当我调用它并想要说明例如打印文件名时,它打印出GLOB()而不是test_dll.txt

print "The name of file is: $file1\n";

输出:

The name of file is: GLOB(0x389e74)

有人请告诉我为什么会这样吗?我怎么能得到变量的名称?

2 个答案:

答案 0 :(得分:5)

open的第一个参数是文件句柄,而不是文件名。如果要将文件名保留在变量中,请使用单独的文件名:

my $filename = 'test_dll.txt';
open my $FH, '<', $filename or die $!;
print "The name of file is: $filename\n";

答案 1 :(得分:0)

因为你做了

# Create a file handle and assign it to $file1.
open my $file1, '<', 'test_dll.txt';

而不是

# Assign a string to $file1.
my $file1 = 'test_dll.txt';
相关问题