-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfiletest.pl
51 lines (46 loc) · 1.08 KB
/
filetest.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/local/bin/perl
use strict;
use warnings;
my $target;
while(1){
print"What file should I write on?" ;
$target = <STDIN>;
chomp $target;
if(-d $target){
print"No, $target is a directory!\n";
next;
}
if(-e $target ){
print"File already exists. What should I do?\n";
print"(Enter 'r' to write to a different name, ";
print "'o' to overwrite or\n";
print "'b' to back up to $target.old)\n";
my $choice = <STDIN>;
chomp $choice;
if($choice eq 'r'){
next;
}elsif($choice eq 'o'){
unless(-o $target){
print"Can't overwrite $target, it's not yours!\n";
next;
}
unless(-w $target){
print"Can't overwrite $target:$!\n";
}
}elsif($choice eq 'b'){
if(rename($target, $target." .old")){
print"OK, moved $target to $target.old\n";
}else{
print"Couldn't rename file: $!\n";
next;
}
}else{
print"I didn't understand that answer.\n";
next;
}
}
last if open OUTPUT, ">$target";
print"I couldn't write on $target: $!\n";
}
print OUTPUT "Congratulations.\n";
print "Wrote to file $target\n";