+ -
当前位置:首页 → 问答吧 → 文件句柄怎么全局化?

文件句柄怎么全局化?

时间:2011-07-06

来源:互联网

我有个脚本需要按条件输出记录到不同文件,例如if($a==1){open FH1……}
但是我调用FH1是在花括号外层调用,这样就提示错误了。该怎么解决?

作者: forward_ding   发布时间: 2011-07-06

#!/usr/bin/perl -w
use strict;
my $a = 2;
open FILE_OUT ,">1.txt" or die "cannot open file!" if $a == 1;
open FILE_OUT ,">2.txt" or die "cannot open file!" if $a == 2;
print FILE_OUT 'hello world!';

我这样后成功把hello world 写入到 2.txt中了!

作者: 64878641   发布时间: 2011-07-06

先在括号外声明一标量,然后FH1的写法用这个标量替换,或者用IO::File模块生成句柄也可以。

作者: x9x9   发布时间: 2011-07-06