2014-01-17 60 views
-2
#!/usr/bin/perl -w 
use strict; 
use warnings; 
use diagnostics; 
use feature 'say'; 
[email protected] = qw(a b c d e f g h i j k l m n o p q r s t u v w x y z); 
[email protected]_a = @array[0..5]; 
[email protected]_b = @array[6..8]; 
[email protected]_c = @array[8..15]; 
[email protected]_d = @array[16..16]; 
[email protected]_e = @array[17..20]; 
[email protected]_f = @array[21..24]; 
[email protected]_g = @array[25..16]; 
print "first five string:",@array_a,"\n"; 
print @array_b,"\n"; 
print @array_c,"\n"; 
print @array_d,"\n"; 
print @array_e,"\n"; 
print @array_f,"\n"; 
print @array_g; 

我有一個上面的Perl腳本,它在終端屏幕上打印腳本的結果。我不需要將信息打印在屏幕上,而是需要將它保存到文本文件中。 我試圖像我如何將輸出結果保存在文件中

$filename = "/home/Ram/Desktop/Perl_file.txt"; 
open([email protected]_a = @array[0..5], '>', $filename) or die "Could not open 
+ file '$filename' $!"; 
print @array_a ; 
close @array_a; 

,但它顯示像

第一個五年字符串錯誤:ABCDEF GHI ijklmnop q RSTU vwxy不能使用字符串(「6」)爲標誌裁判而「嚴格裁判」在array.pl第22行使用(#1)(F)「strict refs」只允許硬引用。符號引用是不允許的。請參閱perlref。從用戶代碼中未捕獲的異常:在array.pl第22行使用「strict refs」時,不能使用字符串(「6」)作爲符號ref。

爲什麼它顯示錯誤?請讓我知道

+2

[開放的perldoc(http://perldoc.perl.org/functions/open.html)=>'打開(我$ fh,「>」,「output.txt」)或者死亡$ !;' –

回答

1

那不是你如何打開一個文件句柄嘗試:

open(my $fh,'>',$filename) or die "could not open $filename $!"; 
print $fh @array_a; 
print $fh @array_b; 
print $fh ....... 
相關問題