[求助]Bareword strict subs

[求助]Bareword strict subs

[求助]Bareword strict subs
我在学习文件操作的时候练习了下面一个程序,把srcfile的内容复制到dstfile去,但是在使用use strict的时候出错了,在google上搜索的结果都是讨论在use xxx ;的时候出这个错误信息的,并且没找到可以解决我的问题的办法,还请各位帮忙看看。谢谢了。
难道文件句柄不能是变量吗?

程序如下:

[quote]use strict;
use warnings;

my $srcfile = "srcfile" ;
my $dstfile = "dstfile" ;
my $src = INFILE ;
my $dst = OUTFILE;

open ($src,"$srcfile") or die "$!" ;
open ($dst,">$dstfile") or die "$!";
my @data = <$src> ;
foreach (@data) {
print $dst $_ ;
}
close $src;
close $dst;[/quote]

上面的INFILE和 OUTFILE都没引住的,执行的时候错误信息如下:
perlish@laptop:~/perl$ ./io.pl
Bareword "INFILE" not allowed while "strict subs" in use at ./io.pl line 26.
Bareword "OUTFILE" not allowed while "strict subs" in use at ./io.pl line 27.
Execution of ./io.pl aborted due to compilation errors.


在我引住的时候,错误信息如下:
my $src = "INFILE" ;
my $dst = "OUTFILE";

perlish@laptop:~/perl$ ./io.pl
Can't use string ("INFILE") as a symbol ref while "strict refs" in use at ./io.pl line 29.

use strict;--use warni.
use strict;
use warnings;

my $srcfile = "srcfile" ;
my $dstfile = "dstfile" ;
open (my $src,"$srcfile") or die "$!" ;
open (my $dst,">$dstfile") or die "$!";
my @data = <$src> ;
foreach (@data) {
print $dst $_ ;
}
close $src;
close $dst;
use strict;--use warni.
use strict;
use warnings;
use FileHandle;

my $srcfile = "srcfile" ;
my $dstfile = "dstfile" ;
my $src = new FileHandle "< $srcfile" or die "$!";
my $dst = new FileHandle "> $dstfile" or die "$!";

my @data = <$src>;
foreach (@data) {
print $dst $_;
}

undef $src;
undef $dst;
[quote]回复给 anthony : .
[quote]回复给 anthony : use strict;--use warni....[/quote]

是的,不初始化$src $dst就没问题的,我一个朋友说是可能是作用域的原因

以后我文件句柄不用变量就好了 :)


裸字要用引号引起来,否则perl会把他当子程序 from tnt :)

use strict;
use warnings;
no strict 'refs';
my $srcfile = "srcfile" ;
my $dstfile = "dstfile" ;
my $src = 'INFILE' ;
my $dst = 'OUTFILE';

open ($src,"$srcfile") or die "$!" ;
open ($dst,">$dstfile") or die "$!";
my @data = <$src> ;
foreach (@data) {
print $dst $_ ;
}
close $src;
close $dst;




   

my $src = 'INFILE' ;--.
my $src = 'INFILE' ;
my $dst = 'OUTFILE';
^^^^^ 多此一举。直接用就可以了。
[quote]open my $src, 'myfile' or die $! [/quote]
我认为是use strict;--u.
我认为是use strict;
use warnings;搞的!把这两行去掉后就没有问题,perl会解释清楚了!