My code looks like this:
#!/usr/bin/perl -w
use strict;
use warnings;
my $i = 1;
my @old_names = glob "/root/pics/*.jpg";
my $new_name = "picture$i";
foreach my $old_name (@old_names) {
rename($old_name, $new_name) or
die "Couldn't rename $old_name to $new_name: $!\n";
} continue { $i++ }
when I execute the script it deletes the pictures, what am I doing wrong?
Edit:
This is what worked for me.
#!/usr/bin/perl -w
use strict;
use warnings;
my $i = 1;
my @old_names = glob "/root/pics/*.jpg";
foreach my $old_name (@old_names) {
my $new_name = "picture$i" . ".jpg";
rename($old_name, "/root/pics/$new_name") or die "Couldn't rename $old_name to $new_name: $!\n";
} continue { $i++ }
Thanks for the help everyone.
This line:
Is outside your loop. Therefore it does not get updated. So all the files get renamed into the same name, which effectively deletes them.
Solution is to move the assignment inside the loop, or skip the variable all together and do
For golfing purposes, you might also consider
You might also be interested in checking out File::Copy, which is a core module.