Uses File::Copy::Recursive, but wedges another 'copy' sub so that a progress bar, or some other hook, can be displayed or run.
update:
The real trick to this particular snippet is determining that File::Copy::Recursive uses File::Copy::copy, but the copy sub is imported into the File::Copy::Recursive namespace rather than its own namespace. If you try to hook File::Copy::copy, it will not work.
For completeness, thank you jdporter, here is what it would look like if Hook::LexWrap was used:
use Hook::LexWrap; use File::Copy::Recursive qw(dircopy); use strict; use vars qw($dir_from $dir_to); $dir_from = "/tmp/from"; $dir_to = "/tmp/to"; $|=1; # Using Hook::LexWrap my @dirs; wrap *File::Copy::Recursive::copy, pre => sub { @dirs = @_ }, post => sub { printf "copying %s to %s. \r", @dirs }; dircopy($dir_from, $dir_to); print "\n";
use File::Copy::Recursive qw(dircopy); use strict; use vars qw($dir_from $dir_to *mycopy); $dir_from = "/tmp/from"; $dir_to = "/tmp/to"; sub mycopy_func { # call the original &mycopy(@_); # call my sub after mycopy_showprogress(@_); } sub mycopy_showprogress { # this could call anything to show progress or even # to operate on the file being copied printf "copying %s to %s. \r",@_; } $|=1; # Add the hook *mycopy = *File::Copy::Recursive::copy; *File::Copy::Recursive::copy = *mycopy_func; dircopy($dir_from, $dir_to); print "\n";
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Copying a directory and its contents wihile displaying a progress bar
by jdporter (Paladin) on Aug 05, 2008 at 11:35 UTC | |
Re: Copying a directory and its contents while displaying a progress bar
by ambrus (Abbot) on Aug 06, 2008 at 10:38 UTC | |
by hiseldl (Priest) on Aug 08, 2008 at 02:14 UTC |
Back to
Cool Uses for Perl