Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

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

by grinder (Bishop)
on Jun 13, 2001 at 20:51 UTC ( #88118=note: print w/ replies, xml ) Need Help??


in reply to Re: Copying a file to more than one separate directory and/or file
in thread Copying a file to more than one separate directory and/or file

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


Comment on Re: Re: Copying a file to more than one separate directory and/or file
Download Code
Re: Re: Re: Copying a file to more than one separate directory and/or file
by nardo (Friar) on Jun 13, 2001 at 21:48 UTC
    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> ) {
    use
    while( read(IN, $_, 4096) ) {

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://88118]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (6)
As of 2014-09-17 06:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (61 votes), past polls