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

hi monks, Can anyone tell me how to copy certain files form one location to another location in the same machine. My cod is:
#!/usr/bin/perl open(FILE,"viewall.pdbid") or die $!; @file=<FILE>; foreach $path(@file) { print $f1="/data/pub/pdb/$path"; print $f2="/tmp/$path"; print `cp $f1 /tmp/.`; }
advance thanks by Tony1 !!!

Replies are listed 'Best First'.
Re: file copy
by tirwhan (Abbot) on Nov 28, 2007 at 10:51 UTC

    File::Copy

    use File::Copy; open (my $fh,"<","viewall.pbdid") or die $!; while (<$fh>) { my $from = "/data/pub/pdb/$path"; my $to = "/tmp/$path"; copy ($from,$to) or die $!; }
    This assumes that "viewall.pbdid" only contains file names, not paths with subdirectories. If it does, you'll need to check/create the parent directory under /tmp before copying the file, File::Copy will not create them for you. Check out File::Copy::Recursive and File::Mirror if that is the solution you need.

    All dogma is stupid.
Re: file copy
by sh1tn (Priest) on Nov 28, 2007 at 14:28 UTC
    If the source file is small you can use File::Slurp

    use File::Slurp; my $file_src = shift; my $file_dst = shift; write_file($file_dst, {'binmode' => ':raw'}, read_file($file_src, {'bi +nmode' => ':raw'}));


      Umm, what would be the point of that? File::Slurp

      • is not in core and will only work with Perl version 5.005+
      • seems to have issues with Unicode files as well as several unadressed bug reports
      • does not help with any platform issues
      • usage is just plain unintuitive for a maintenance programmer in this case

      I'm not trying to knock File::Slurp, it's probably good at what it does (though the numerous, long-running bug reports would give me pause), but what possible reason (other than obfuscation) would you have for using it in this case?


      All dogma is stupid.