编写了一个简单的Perl Tk脚本,用于制作单个文件的多个副本。 但正如标题所示我的脚本中出现“无法打开文件”错误。 这个脚本可以在终端(Linux)上运行,但是当我尝试使用Tk时就不行了。
use Tk;
use Tk::FileSelect;
use File::Spec;
use File::Basename;
use Cwd;
my $mw = new MainWindow; # Main Window
my $frm_name = $mw -> Frame() -> pack();
my $lab = $frm_name -> Label(-text=>"File Name:") -> pack();
my $ent = $frm_name -> Entry() -> pack();
my $but = $mw -> Button(-text=>"Get File", -command =>\&push_button) -> pack();
my $copyBtn =$mw -> Button(-text=>"Copy", -command =>\&makeCopies) -> pack();
my $dd = getcwd;
my $count;
#Text Area
my $txt = $mw -> Text(-width=>50, -height=>10) -> pack();
MainLoop;
#This function will be executed when the button is pushed
sub push_button {
my $fs = $mw->FileSelect();
my $file = $fs->Show;
#my $fn = basename($file);
my $fn = File::Spec->splitpath( $file );
print "$fn\n";
$ent -> insert('end',$fn);
}
sub makeCopies{
while ($count < 5){
chdir getcwd;
open (FN, $fn) or warn "Unable to open file";
binmode (FN);
open (FN,">($count)$fn") or warn "Unable to copy file";
print (<FN>);
$count++;
}
}
close(FN);
我已经尝试了Basename和File :: Spec思考可能是一个或另一个没有正确解析。 但我无法弄清楚为什么它不会打开$ fn ??