ЭЛЕКТРОННАЯ БИБЛИОТЕКА КОАПП |
Сборники Художественной, Технической, Справочной, Английской, Нормативной, Исторической, и др. литературы. |
9.3. Copying or Moving a FileProblemYou need to copy a file, but Perl has no built-in copy command. SolutionUse the use File::Copy; copy($oldfile, $newfile); You can do it by hand: open(IN, "< $oldfile") or die "can't open $oldfile: $!"; open(OUT, "> $newfile") or die "can't open $newfile: $!"; $blksize = (stat IN)[11] || 16384; # preferred block size? while ($len = sysread IN, $buf, $blksize) { if (!defined $len) { next if $! =~ /^Interrupted/; # ^Z and fg die "System read error: $!\n"; } $offset = 0; while ($len) { # Handle partial writes. defined($written = syswrite OUT, $buf, $len, $offset) or die "System write error: $!\n"; $len -= $written; $offset += $written; }; } close(IN); close(OUT); Or you can call your system's copy program: system("cp $oldfile $newfile"); # unix system("copy $oldfile $newfile"); # dos, vms DiscussionThe File::Copy module provides use File::Copy; copy("datafile.dat", "datafile.bak") or die "copy failed: $!"; move("datafile.new", "datafile.dat") or die "move failed: $!"; Because these functions return only a simple success status, you can't easily tell which file prevented the copy or move from being done. Copying the files manually lets you pinpoint which files didn't copy, but it fills your program with complex See AlsoDocumentation for the standard File::Copy module (also in Chapter 7 of Programming Perl); the |