in reply to copy to different directory

This has been a well discussed topic, but here is a short snippet to copy a file from a known directory to another:

use strict; use File::Copy; use File::Spec; my $source_directory = 'foo'; my $target_directory = 'bar'; sub copyfile { for my $file (@_) { my $sourcename = File::Spec->catfile( $source_directory,$file); my $targetname = File::Spec->catfile( $target_directory,$file); copy( $sourcename,$targetname) or warn "Couldn't copy $sourcename to $targetname : $!"; }; }; copyfile('README');

Replies are listed 'Best First'.
Re: Re: copy to different directory
by matija (Priest) on Mar 21, 2004 at 12:10 UTC
    Just for fun, I thought I'd modify that for IO::All:
    use strict; use IO::All; use File::Spec; my $source_directory = 'foo'; my $target_directory = 'bar'; sub copyfile { for my $file (@_) { my $sourcename = File::Spec->catfile( $source_directory,$file); my $targetname = File::Spec->catfile( $target_directory,$file); io($targetname)->print(io($sourcename)->slurp); }; copyfile('README');
    Note as memory efficient as File::Copy, of course...

      Also, IO::All->slurp does not respect binmode and so will clobber any binary files copied that way, at least under Win32 and also under newer versions of Perl that have that weird Unicode stuff (see also RT ticket #5686, thanks to PodMaster):

      #!/usr/bin/perl -w use strict; use File::Temp qw(tempfile); use Test::More tests => 2; use_ok( 'IO::All' ); my ($fh,$filename) = tempfile(); my $binary = "foo\r\nbar"; binmode $fh; print $fh $binary; close $fh; my $content = io($filename)->slurp; is($binary, $content);