见:Learning Perl Challenge: Remove intermediate directories
#!/usr/bin/perl
use strict;
use warnings;
use File::Path qw(remove_tree);
use File::Copy qw(move);
my ($dir) = @ARGV;
short_dir($dir);
sub short_dir {
my ($start, $now) = @_;
return unless(-d $start);
$now ||= $start;
my @subnodes = glob("$now/*");
my @subdirs = grep { -d $_ } @subnodes;
if($#subdirs>0){
short_dir($_) for @subdirs;
}elsif($#subdirs==0){
short_dir($start, $subdirs[0]);
}else{
return if($start eq $now);
move($_, "$start/") for @subnodes;
my @dirs = grep { -d $_ } glob("$start/*");
remove_tree($dirs[0]);
}
}
没有评论:
发表评论