Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Can't copy file

by novice2015 (Acolyte)
on Mar 30, 2015 at 19:37 UTC ( #1121892=perlquestion: print w/replies, xml ) Need Help??

novice2015 has asked for the wisdom of the Perl Monks concerning the following question:

I am a total beginner and have a script like this: It runs fine the only problem is it does not copy the data in output1.txt to output3.txt. It creates output3.txt but does not copy the data. Why?

#!/usr/bin/perl my $server = `uname -n`; print $server; chomp $server; # Display all the files in /tmp directory. $dir = "/usr/ent/newdir/db/sign/*"; my @files = </usr/ent/newdir/db/sign/*files*>; open(my $outfh, '>', '/tmp/output1.txt') or die "cannot open file"; foreach $LINE (@files ){ print $outfh $LINE; close FH; } use File::Copy; {$oldlocation = "/tmp/output1.txt"; $newlocation = "/tmp/output3.txt"; copy($oldlocation, $newlocation); }

Replies are listed 'Best First'.
Re: Can't copy file
by toolic (Bishop) on Mar 30, 2015 at 20:17 UTC
    Check if copy succeeded (File::Copy):
    copy( $oldlocation, $newlocation ) or die "Copy failed: $!";

    Tip #1 from the Basic debugging checklist: warnings. You try to close FH, but you opened $outfh.

    Do not close the file in the foreach loop. It works as expected for me when I close it outside the loop.

Re: Can't copy file
by Laurent_R (Canon) on Mar 30, 2015 at 20:56 UTC
    As I already told you on your cross-post on the Perl Guru forum (http://perlguru.com/gforum.cgi?post=81230;#81230), you need to close the file handle ($outfh, not FH) after the end of the for loop and before proceeding to copy the file.

    The reason for it is that Perl is buffering output to files, so that the "output1.txt" file is probably still empty when you are copying it. By closing the file handle, you force the buffer to be copied to the file. If you don't do that, the file is probably actually populated only when the program ends.

    Je suis Charlie.
Re: Can't copy file
by redgreen (Priest) on Mar 30, 2015 at 20:22 UTC

    use strict;
    use warnings;

    close FH; This file handle hasn't been opened.

    $outfh is never closed. Make sure you close after you are done writing.

    Which is probably why the copy fails, the file is still open...

    Also, check to make sure the copy was successful:

    if ( ! copy($oldlocation, $newlocation) ) { print "Copy failed: $!\n"; }
Re: Can't copy file
by marinersk (Priest) on Mar 30, 2015 at 21:10 UTC

    novice2015 has asked for the wisdom of the Perl Monks concerning the following question:

         I am a total beginner

    Excellent -- and welcome to the Monastary. Please add the following to your script; I strongly urge you to never remove them:

         #!/usr/bin/perl
         use strict;
         use warnings;
         my $server = `uname -n`;
         print $server;
         chomp $server;
         # Display all the files in /tmp directory.
         
    my $dir = "/usr/ent/newdir/db/sign/*";
         my @files = </usr/ent/newdir/db/sign/*files*>;
         open(my $outfh, '>', '/tmp/output1.txt') or die "cannot open file";
         foreach
    my $LINE (@files ){
             print $outfh $LINE;
             close FH;
         }
         use File::Copy;
         {
             
    my $oldlocation = "/tmp/output1.txt";
             
    my $newlocation = "/tmp/output3.txt";
             copy($oldlocation, $newlocation);
         }
         exit;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1121892]
Approved by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2021-11-26 23:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?