2012-01-30 47 views

回答

6

print manpage:在一個數組或哈希,或在一般

如果你存儲句柄每當 任何正在使用的表達比裸詞處理更復雜的或 平原,無下標量變量來檢索它,你將不得不 使用返回文件句柄值的塊代替。

您應該使用:

print { $$self{tmp_db_fh} } "Bob\n"; 

此代碼不會use strict下工作。要修復它只是用一種my變量:

open my $fh, ">", "data.txt" or die $!; 
$$self{tmp_db_fh} = $fh; 
print { $$self{tmp_db_fh} } "Bob\n"; 
+1

這不會使用'use strict'refs';' – dgw 2012-01-30 14:34:54

+0

我搜索了很多地方,但從來沒有得到那個網頁。多謝。 – 2012-01-30 14:35:50

0

文件句柄只能是標量。

$$self{tmp_db_fh}或者是一個開放的文件句柄(至data.txt中),那麼這會工作:

sub _init_tmp_db 
{ 
    my ($self) = @_; 

    my $filehandle = $$self{tmp_db_fh} ; 
    print $filehandle "Bob\n"; 
} 

或打開裏面_init_tmp_db

sub _init_tmp_db 
{ 
    my ($self) = @_; 

    open my $filehandle , ">", "data.txt" or die "Cannot open data.txt" ; 
    print $filehandle "Bob\n"; 
} 

文件句柄但在$$self{tmp_db_fh}提供一個字符串(像'FILEHANDLE')將不起作用。

4

你應該IO ::文件模塊來代替。

use IO::File; 
my $file = IO::File->new; 
$file->open("> data.txt"); 

print_something($file); 

sub print_something { 
    my ($file) = @_; 
    $file->print("hello world\n"); 
} 

或者在你的榜樣作用:

use IO::File; 
# ... 
sub _init_tmp_db 
{ 
    my ($self) = @_; 

    $self{tmp_db_fh} = IO::File->new; 
    $self{tmp_db_fh}->open(">", "data.txt"); 
    $self{tmp_db_fh}->print"Bob\n"; 
} 

(注意,你仍然可以非 - >基於電話太多,但我用上面寫了 更傳統 - > open()的呼叫類型。)

+0

Thx的建議,但現在,我在當前的代碼使用方法中打開的所有其他文件,如在問題中。 – 2012-01-30 14:33:58

+0

沒關係,IO :: File模塊的確可以這樣工作。 IE,開啓($ self {tmp_db_fh},>>「,」data.txt「)'是安全的,一旦你創建了IO :: File句柄,它就可以正常工作。 – 2012-01-30 14:39:54

+0

你不需要**使用[IO :: File](http://perldoc.perl.org/IO/File.html)。像往常一樣使用Perl [TMTOWTDI](http://en.wikipedia.org/wiki/TMTOWTDI)。 – 2012-01-30 19:23:37

0

這是很容易的文件句柄創建變量解決:

sub _init_tmp_db { 
    my $self = shift; 

    my $fh; 
    open $fh, ">", "data.txt" 
    $self->{temp_db_fh} = $fh; 

    # Sometime later... 

    $fh = $self-{temp_db_hf}; 
    print $fh "Bob\n"; 
} 

這是一個問題,因爲解析了print語法的方式以及語法的早期缺乏。 print聲明實際上有兩種不同的格式:格式#1是您只需將它傳遞給打印機。格式#2說第一個項目可能是一個文件句柄,剩下的就是你想要打印到文件句柄的東西。如果print無法輕易確定第一個參數是文件句柄,則失敗。

如果你看看其他語言,他們將使用一個參數來傳遞文件句柄,也可能是要打印的東西。或者在面向對象的語言中,它們將爲文件句柄參數重載>>。他們會是這個樣子:

print "This is my statement", file=file_handle; 

print "This is my statement" >> file_handle; 

您可能能夠Munge時間的語法使用一個變量脫身。但是,它不會使程序更高效或更易讀,並且可能會使程序更難以維護。所以,只需使用文件句柄的變量。


你說類的標題。我假設你有興趣編寫一個完全面向對象的包來做到這一點。這是一個簡單的例子。通知write 子程序 方法我將文件句柄檢索到一個變量中並使用print語句中的變量。

#! /usr/bin/env perl 
# 
use strict; 
use warnings; 


####################################################### 
# MAIN PROGRAM 
# 
my $file = File->new; 

$file->open("OUTPUT") or 
    die "Can't open 'OUTPUT' for writing\n"; 

$file->write("This is a test"); 
# 
####################################################### 

package File; 
use Carp; 


sub new { 
    my $class = shift; 

    my $self = {}; 
    bless $self, $class; 

    return $self; 
} 

sub open { 
    my $self = shift; 
    my $file = shift; 

    my $fh; 
    if (defined $file) { 
     $self->{FILE} = $file; 
     open ($fh, ">", $file) and $self->_fh($fh); 
    } 
    return $self->_fh; 
} 

sub _fh { 
    my $self = shift; 
    my $fh = shift; 

    if (defined $fh) { 
     $self->{FH} = $fh; 
    } 
    return $self->{FH}; 
} 

sub write { 
    my $self = shift; 
    my $note = shift; 

    my $fh = $self->_fh; 
    print $fh $note . "\n"; 
    return 
} 
相關問題