Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Copying a file to more than one separate directory and/or file

by blacksmith (Hermit)
on Jun 13, 2001 at 19:50 UTC ( #88098=perlquestion: print w/replies, xml ) Need Help??
blacksmith has asked for the wisdom of the Perl Monks concerning the following question:

What would be the most efficient and easiest way to copy a file from one location to 3 different locations. I noticed in the FAQ section, a question that asked about printing more than one file at once. Am I on the right track, and if so could someone elaborate on the methods included there. I appreciate the advice. Blacksmith.
  • Comment on Copying a file to more than one separate directory and/or file

Replies are listed 'Best First'.
Re: Copying a file to more than one separate directory and/or file
by stephen (Priest) on Jun 13, 2001 at 19:58 UTC
    Best way is to use File::Copy. The FAQ section you're referring to is dealing with tee-ing filehandles, so that anything one prints goes into three files (if I'm understanding which section you're talking about correctly). If you're simply copying files, it's easiest to simply copy the files. Using File::Copy, you could do something like:
    use strict; use File::Copy; my $source = 'src1'; foreach my $dest ( 'dest_1', 'dest_2', 'dest_3' ) { copy($source, $dest); }


      Depending on the size of the file, you may exhaust the machine's memory allocated to disk buffering, which means that if you use File::Copy you will physically reread the same disk sectors over and over again. If this is true, the most effecient method of writing to multiple files would be to read a line, and write it out to all the output files. Something like:

      my %fd = ( 'file1' => 'fd1', '/tmp/foo => 'fd2', '/home/me/file' => 'fd3', ); my $in = shift || die "No file specified.\n"; open IN. $in or die "Cannot open $in for input: $!\n"; foreach( keys %fd ) { open $fd{$_}, ">$_" or die "Cannot open $_ for output: $!\n"; } while( <IN> ) { foreach my $fd( keys %fd ) { print $fd $_; } } foreach( keys %fd ) { close $fd{$_}; }

      g r i n d e r
        This line:
        foreach my $fd( keys %fd ) {
        should be:
        foreach my $fd( values %fd ) {
        and instead of reading it in line by line, it would be more efficient just to read a chunk of it at a time so instead of
        while( <IN> ) {
        while( read(IN, $_, 4096) ) {
Re: Copying a file to more than one separate directory and/or file
by lemming (Priest) on Jun 13, 2001 at 19:53 UTC
    You will probably want to take a look at the module File::Copy which is included in current distributions.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://88098]
Approved by root
[Eily]: Hi!
[Happy-the-monk]: Discipulus: si!
[Eily]: Discipulus 2018 has to be better than 2017. The latter was kinda odd, but not we got even :D
[Corion]: Eily++ # basic number theory

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (9)
As of 2018-01-23 08:54 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (242 votes). Check out past polls.