2015-08-18 76 views
-1

我已經定義並打開一個文件名:打印出GLOB(),而不是變量名

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) 

總會有請讓我知道爲什麼會這樣?我怎麼能有變量的名字?

+0

注:文件句柄不一定指向文件;他們可以指向外部命令或套接字等......據我所知,沒有直接的方式來做你所要求的,但是有幾個迂迴的方法可以做到這一點:1)保持你的文件清單按名稱在散列中創建2)使用['stat'](http://perldoc.perl.org/functions/stat.html)查找文件的索引節點,並使用其他工具查找關於該索引節點的元數據 –

回答

5

open的第一個參數是文件句柄,而不是文件名。如果你想保留的文件名中的一個變量,使用單獨的一個:

my $filename = 'test_dll.txt'; 
open my $FH, '<', $filename or die $!; 
print "The name of file is: $filename\n"; 
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';