- 论坛徽章:
- 0
|
下面这段代码查不出来啥问题,如果我从参数中获得path的值,那么后面两次open $labe都会报错。
如果我在代码中直接给出path的值,那么就没问题。感觉很奇怪!!!!!11
#!/usr/bin/perl
##########################################################
## this script is used to add new label to *.idt ##
## ./add-idt-file.pl label_file ##
##########################################################
use File::Find;
our $label = $ARGV[0];
my $path = $ARGV[1];
open( LABEL, "<$label" ) or die "it can not open file $!"; <-此处不报错
close LABEL;
find( \&wanted, "$path" ); ####如果把此处的$path变量换成当前目录表示".",就没有问题。
sub wanted
{
my $flag = 0;
my $file = $File::Find::name;
if( $file =~ /.*\.idt/ )
{
my $filebak = $file.".cp";
system( " touch $filebak ");
open( FILE, "<$file" ) or die "it can not open file $!";
open( FILEBAK, ">$filebak" ) or die "it can not open file $!";
my $line;
while( <FILE> )
{
$file_text = $_;
print ( FILEBAK $line);
if( $file_text =~ /笆惯のラベルは蝗脱しないでください/ )
{
if( $flag == 0 )
{
my $label_text = "";
open( LABEL, "<$label" ) or die "it can not open file $!"; <- 出错
while( <LABEL> )
{
$label_text = $_;
print( FILEBAK $label_text );
}
close LABEL;
print( FILEBAK "\n" );
$flag = 1;
}
}
$line = $file_text;
}
print ( FILEBAK $line );
close FILE;
if( $flag == 0 )
{
my $label_text = "";
open( LABEL, "<$label" ) or die "it can not open file $!"; <- 出错
while( <LABEL> )
{
$label_text = $_;
print( FILEBAK $label_text );
}
close LABEL;
print( FILEBAK "\n" );
}
close FILEBAK;
}
} |
|