Leser: 17
Note that passing in files as handles instead of names may lead to loss of information on some operating systems; it is recommended that you use file names whenever possible. Files are opened in binary mode where applicable. To get a consistent behaviour when copying from a filehandle to a file, use binmode on the filehandle.
1
2
3
$for i in $(seq 1 1050); do echo "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n" >> test; done
$cat test | wc -l
1050
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
#!/usr/bin/perl use strict; use warnings; use File::Temp qw/tempdir tempfile/; use File::Copy; open my $f,'<','./test' or die "$!"; my $content = do { local($/); <$f>; }; my $temp_dir = '/home/mkurz/tmp'; my ($fh,$filename) = create_temp_file($temp_dir); print $fh $content; move_files($filename, $temp_dir); sub move_files { my ($file1, $temp_dir) = @_; my $dir = create_temp_dir($temp_dir); print $dir; copy("$file1","$dir"); } sub create_temp_dir { my($temp_dir) = @_; my $dir = tempdir( CLEANUP => '0', DIR => "$temp_dir", ); return $dir; } sub create_temp_file { my ($temp_dir) = @_; my ($dir, $fh, $filename); $dir = tempdir( CLEANUP => '1', DIR => "$temp_dir", ); ($fh, $filename) = tempfile( DIR => $dir, SUFFIX => '.idoc', ); # binmode( $fh, ":utf8" ); return $fh, $filename; }
1 2 3 4
my ($fh,$filename) = create_temp_file($temp_dir); print $fh $content; close($fh); move_files($filename, $temp_dir);
$fh->autoflush(1);