Skip to content


Subversion checkout URL

You can clone with
Download ZIP


Recursive copy #1

wants to merge 2 commits into from

2 participants


I changed File::Copy to use the newish IO.copy method, as well as implemented recursive copying.


I'd rather not suddenly remove the original cp. Maybe it's good to leave a warn() in the body of the old sub?

Also, having IO.copy maybe the better place to implement recursive copying is to augment the IO class, and deprecate File::Copy completely? cp would still be needed for some Shell::Command (which I hope will happen sooner than later).

Anyway, I'm for merging it if we keep the original cp, of course having it use IO.copy

@worr worr Preserved old behavior
- fixed testing cleanup

Things have changed a bit since this pull request was opened, but I integrated the code into Shell::Command without changing a line of code, and it works like a charm now. Thanks!

@tadzik tadzik closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Apr 19, 2011
  1. @worr

    Added recursive copying

    worr authored
  2. @worr

    Preserved old behavior

    worr authored
    - fixed testing cleanup
This page is out of date. Refresh to see the latest.
Showing with 22 additions and 11 deletions.
  1. +13 −10 lib/File/
  2. +9 −1 t/02-file-copy.t
23 lib/File/
@@ -2,12 +2,16 @@ use v6;
module File::Copy;
-sub cp(Str $from, Str $to) is export {
- my $f1 = open $from, :r, :bin;
- my $f2 = open $to, :w, :bin;
- $f2.write($ until $f1.eof;
- $f1.close;
- $f2.close;
+sub cp(Str $from, Str $to, :$r) is export {
+ if ($from.IO ~~ :d and $r) {
+ mkdir("$to") if $to.IO !~~ :d;
+ for dir($from) -> $item {
+ mkdir("$to/$item") if "$from/$item".IO ~~ :d;
+ cp("$from/$item", "$to/$item", :r);
+ }
+ } else {
+ $from.IO.copy($to);
+ }
=begin pod
@@ -20,14 +24,13 @@ File::Copy -- copy files
use File::Copy;
- cp 'source', 'destination';
+ cp 'source', 'destination', :r;
C<File::Copy> exports just one subroutine, cp taking two string
-parameters: source and destination. If something comes wrong, the
-internal open() or write() calls will die, C<copy()> has no special
-error reporting.
+parameters: source and destination and a recursion flag. If something
+goes wrong, IO.cp fails. There is no special error reporting.
=end pod
10 t/02-file-copy.t
@@ -1,10 +1,18 @@
use v6;
use Test;
use File::Copy;
+use File::Find;
-cp 't/test.file', 't/another.file';
+cp 't/test.file', 't/another.file', :r;
is slurp('t/test.file'),
"copied file is identical";
+cp 't/dir1', 't/dir2', :r;
+is dir('t/dir1'),
+ dir('t/dir2'),
+ "recursive copy";
unlink 't/another.file';
+my @filelist := find(dir => 't/dir2');
+for @filelist.reverse -> $file { say $file; unlink $file; }
+unlink 't/dir2';
Something went wrong with that request. Please try again.