meer perlerig, ofwel korter, sneller, minder leesbaar ;)
This commit is contained in:
parent
99453e294c
commit
00e82e9bb6
1 changed files with 17 additions and 45 deletions
|
|
@ -13,18 +13,13 @@ use IO::File;
|
||||||
use File::Copy;
|
use File::Copy;
|
||||||
use POSIX qw(tmpnam);
|
use POSIX qw(tmpnam);
|
||||||
|
|
||||||
$DEBUG = 0;
|
|
||||||
|
|
||||||
#Read current dir
|
#Read current dir
|
||||||
opendir(DIRHANDLE, ".") or die "couldn't open .: $!";
|
opendir(DIRHANDLE, ".") or die "couldn't open .: $!";
|
||||||
while ( defined ($filename = readdir(DIRHANDLE)) ) {
|
while ( defined ($filename = readdir(DIRHANDLE)) ) {
|
||||||
if ($DEBUG) { print "Inside . is something called $filename\n"; }
|
|
||||||
push @dir, "$filename\n";
|
push @dir, "$filename\n";
|
||||||
}
|
}
|
||||||
closedir(DIRHANDLE);
|
closedir(DIRHANDLE);
|
||||||
@source = sort @dir;
|
@source = sort @dir;
|
||||||
if ($DEBUG) { print @source; }
|
|
||||||
|
|
||||||
|
|
||||||
# make tempfiles and install handler to remove them
|
# make tempfiles and install handler to remove them
|
||||||
do { $target_name = tmpnam() }
|
do { $target_name = tmpnam() }
|
||||||
|
|
@ -42,8 +37,6 @@ open $target, $target_name or die "Couldn't open tempfile: $target_name: $!";
|
||||||
@target = <$target>;
|
@target = <$target>;
|
||||||
close ($target);
|
close ($target);
|
||||||
|
|
||||||
if ($DEBUG) { print @target; }
|
|
||||||
|
|
||||||
unless ( scalar(@source) == scalar(@target) ) {
|
unless ( scalar(@source) == scalar(@target) ) {
|
||||||
die "Aborting. Source and target list don't have the same number of lines.\n";
|
die "Aborting. Source and target list don't have the same number of lines.\n";
|
||||||
}
|
}
|
||||||
|
|
@ -62,10 +55,9 @@ if (@unsafe = &check_safety(\@source, \@target)) {
|
||||||
&move_files(\@source, \@target);
|
&move_files(\@source, \@target);
|
||||||
|
|
||||||
# call EDITOR or vi with filename
|
# call EDITOR or vi with filename
|
||||||
|
# edit($filename);
|
||||||
sub edit {
|
sub edit {
|
||||||
my $filename = shift;
|
@editor = (defined $ENV{EDITOR} ? $ENV{EDITOR} : "vi", shift);
|
||||||
$editor = defined $ENV{EDITOR} ? $ENV{EDITOR} : "vi";
|
|
||||||
@editor = ("$editor", "$filename");
|
|
||||||
system(@editor) == 0
|
system(@editor) == 0
|
||||||
or die "System @editor failed: $?";
|
or die "System @editor failed: $?";
|
||||||
}
|
}
|
||||||
|
|
@ -75,33 +67,26 @@ sub edit {
|
||||||
# move_files(\@source, \@target)
|
# move_files(\@source, \@target)
|
||||||
sub move_files {
|
sub move_files {
|
||||||
my ($from, $to) = @_;
|
my ($from, $to) = @_;
|
||||||
my($i, $source, $target);
|
my ($i, $source, $target);
|
||||||
$i=0;
|
for ( $i=0; $i < scalar(@$from); $i++ ) {
|
||||||
while ( $i < scalar(@$from) ) {
|
|
||||||
$source=$from->[$i];
|
$source=$from->[$i];
|
||||||
chomp($source);
|
|
||||||
$target=$to->[$i];
|
$target=$to->[$i];
|
||||||
chomp($target);
|
|
||||||
unless ( $source eq $target ) {
|
unless ( $source eq $target ) {
|
||||||
if ($DEBUG) { print "mv $source $target\n"; }
|
chomp($source);
|
||||||
|
chomp($target);
|
||||||
move("$source", "$target")
|
move("$source", "$target")
|
||||||
or die "move failed: $!";
|
or die "move failed: $!";
|
||||||
}
|
}
|
||||||
$i++;
|
|
||||||
}
|
}
|
||||||
return 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# returns 0 if all entries in @target_list are unique. 1 if not.
|
# returns 0 if all entries in @target_list are unique. 1 if not.
|
||||||
sub check_unique (@target_list){
|
sub check_unique (@target_list){
|
||||||
my(@target_list, %seen, @uniqu);
|
my @uniqu;
|
||||||
@target_list = @_;
|
my @target_list = @_;
|
||||||
%seen = ();
|
my %seen = ();
|
||||||
@uniqu = grep { ! $seen{$_} ++ } @target_list;
|
@uniqu = grep { ! $seen{$_} ++ } @target_list;
|
||||||
unless (scalar(@uniqu) == scalar(@target) ) {
|
return (scalar(@uniqu) != scalar(@target))
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# compares one array of file names to another, making sure files
|
# compares one array of file names to another, making sure files
|
||||||
|
|
@ -111,35 +96,22 @@ sub check_unique (@target_list){
|
||||||
# returns an array of unsafe line numbers
|
# returns an array of unsafe line numbers
|
||||||
sub check_safety {
|
sub check_safety {
|
||||||
my ($from, $to) = @_;
|
my ($from, $to) = @_;
|
||||||
my($i, $j, $source, $target, @danger);
|
my ($i, $j, @danger);
|
||||||
$i=0;
|
for ( $i=0 ; $i < scalar(@$from) ; $i++ ) {
|
||||||
while ( $i < scalar(@$from) ) {
|
for ( $j=0; $j < scalar(@$to); $j++ ) {
|
||||||
$source=$from->[$i];
|
if (($from->[$i] eq $to->[$j]) && ($i != $j)) {
|
||||||
chomp($source);
|
|
||||||
$j=0;
|
|
||||||
while ( $j < scalar(@$to) ) {
|
|
||||||
$target=$to->[$j];
|
|
||||||
chomp($target);
|
|
||||||
if (($source eq $target) && ($i != $j)) {
|
|
||||||
push @danger, $i;
|
push @danger, $i;
|
||||||
}
|
}
|
||||||
$j++;
|
|
||||||
}
|
}
|
||||||
$i++;
|
|
||||||
}
|
}
|
||||||
return @danger;
|
return @danger;
|
||||||
}
|
}
|
||||||
|
|
||||||
# generate random filename, which does not exist
|
# generate random filename, which does not exist
|
||||||
sub rand_file {
|
sub rand_file {
|
||||||
my (@chars, $filename, $exists);
|
my $filename;
|
||||||
@chars=( "A" .. "Z", "a" .. "z", 0 .. 9);
|
my @chars=( "A" .. "Z", "a" .. "z", 0 .. 9);
|
||||||
$exists = 1;
|
while ( -e ($filename = join("", @chars[ map { rand @chars } ( 1 .. 8 ) ]))) {}
|
||||||
while ($exists) {
|
|
||||||
$filename = join("", @chars[ map { rand @chars } ( 1 .. 8 ) ]);
|
|
||||||
$exists = -e $filename;
|
|
||||||
}
|
|
||||||
|
|
||||||
return $filename;
|
return $filename;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue