[newbie] rename not working

M

Marek Stepanek

Hello Perlers,



I am ashamed : this simple script should work but I don't see the mistake.
With uncommented

# rename ...

the test print is working. But the rename itself is not working with
following error message :

Use of uninitialized value in -e at ./rename_files.pl line 20.
Use of uninitialized value in rename at ./rename_files.pl line 33.
Use of uninitialized value in concatenation (.) or string at
./rename_files.pl line 33.
couldn't rename h2armenia.gif to : No such file or directory at
./rename_files.pl line 33.

thank you for your patience



marek


******************


#!/usr/bin/perl

# rename_files.pl - rename "files.gif" keeping the original name
# and adding at the beginning a h2 to "h2files.gif"


use strict;
use warnings;

my ($file, $new);

foreach $file (@ARGV) {

if ($file =~ m/^page\d+\.gif$/) {
$new = "h2" . $file;
}

if (-e $new) {
warn "$new already exists. Skipping...\n";
next;
}

if ($file =~ m{/}) {
warn "$file contains a slash. Skipping...\n";
next;
}

# test before renaming finally the files :
# print "the old files will be renamed from\t\t$file\t\tto\t\t$new\n\n";

rename $file, $new or die "couldn't rename $file to $new: $!";

print "Perl renamed the old files from\t\t$file\t\tto\t\t$new\n\n";
}







--
______________________________________________________________________
___PODIUM_INTERNATIONAL_//_the_embassy_for_talented_young_musicians___
_______Marek_Stepanek__mstep_[at]_PodiumInternational_[dot]_org_______
__________________http://www.PodiumInternational.org__________________
______________________________________________________________________
 
P

Paul Lalli

Marek Stepanek said:
I am ashamed : this simple script should work but I don't see the mistake.
With uncommented

# rename ...

the test print is working. But the rename itself is not working with
following error message :

Use of uninitialized value in -e at ./rename_files.pl line 20.
Use of uninitialized value in rename at ./rename_files.pl line 33.
Use of uninitialized value in concatenation (.) or string at
./rename_files.pl line 33.
couldn't rename h2armenia.gif to : No such file or directory at
./rename_files.pl line 33.

In the future, it would be helpful to know which lines are lines 20 and
33...
#!/usr/bin/perl

use strict;
use warnings;

my ($file, $new);

foreach $file (@ARGV) {

if ($file =~ m/^page\d+\.gif$/) {
$new = "h2" . $file;
}

What if $file doesn't match that pattern? Then what is $new? At the
moment, it's nothing - that is, it's undefined.
if (-e $new) {

And here you check to see if a possibly undefined value is an existing
filename.

You need either figure out what $new should be in this case, or -
depending on what this program is supposed to do - skip the remainder of
the loop if $file doesn't match.

perldoc -f next will help you with that.

<remainder snipped>

Paul Lalli
 
A

A. Sinan Unur

Use of uninitialized value in -e at ./rename_files.pl line 20.
Use of uninitialized value in rename at ./rename_files.pl line 33.
Use of uninitialized value in concatenation (.) or string at
./rename_files.pl line 33.
couldn't rename h2armenia.gif to : No such file or directory at
./rename_files.pl line 33.

This is what you get for not declaring your variable sin the smallest
applicable scope. See below.
#!/usr/bin/perl

use strict;
use warnings;
Good.

my ($file, $new);
Bad.

foreach $file (@ARGV) {

if ($file =~ m/^page\d+\.gif$/) {
$new = "h2" . $file;
}

A-ha! If the $file does not match the pattern, $new is not initialized.
if (-e $new) {
warn "$new already exists. Skipping...\n";
next;
}

But you go ahead and use it anyway.
if ($file =~ m{/}) {
warn "$file contains a slash. Skipping...\n";
next;
}

But that's impossible if $file matches the pattern above.

Here's a revised (but untested) version:

#! /usr/bin/perl

use strict;
use warnings;

for my $src (@ARGV) {
next unless $src =~ /^page\d+\.gif$/;
my $dest = "h2$src";

if(-e $dest) {
warn "$dest already exists. Skipping ...\n";
next;
}
print "Renaming $src => $dest\n";

rename $src, $dest
or warn "Failed to rename $src => $dest: $!";
}

__END__

Sinan.
 
M

Matija Papec

X-Ftn-To: Marek Stepanek

Marek Stepanek said:
# rename_files.pl - rename "files.gif" keeping the original name
# and adding at the beginning a h2 to "h2files.gif"


use strict;
use warnings;

absolutely no flaw here. :)
my ($file, $new);

foreach $file (@ARGV) {

if ($file =~ m/^page\d+\.gif$/) {
$new = "h2" . $file;
}

if (-e $new) {
warn "$new already exists. Skipping...\n";
next;
}

This conditions should be nested inside of the first one as $new isn't set
if matching fails.

if ($file =~ /../) {
..
if (-e $new) { warn .. }
else { rename .. print .. }
}
elsif ..
 
T

terry l. ridder

Hello Perlers,



I am ashamed : this simple script should work but I don't see the mistake.
With uncommented

# rename ...

the test print is working. But the rename itself is not working with
following error message :

Use of uninitialized value in -e at ./rename_files.pl line 20.
Use of uninitialized value in rename at ./rename_files.pl line 33.
Use of uninitialized value in concatenation (.) or string at
./rename_files.pl line 33.
couldn't rename h2armenia.gif to : No such file or directory at
./rename_files.pl line 33.

the above error msgs clearly tell you want to look for.
thank you for your patience



marek


******************


#!/usr/bin/perl

# rename_files.pl - rename "files.gif" keeping the original name
# and adding at the beginning a h2 to "h2files.gif"


use strict;
use warnings;

my ($file, $new);

foreach $file (@ARGV) {

if ($file =~ m/^page\d+\.gif$/) {
$new = "h2" . $file;
}

the variable $new may or may not be defined at this point in the code.
Use of uninitialized value in -e at ./rename_files.pl line 20.
if (-e $new) {

clearly at some point $new is uninitialized. that is what the above
error msg is telling you.
warn "$new already exists. Skipping...\n";
next;
}

if ($file =~ m{/}) {
warn "$file contains a slash. Skipping...\n";
next;
}

# test before renaming finally the files :
# print "the old files will be renamed from\t\t$file\t\tto\t\t$new\n\n";

Use of uninitialized value in rename at ./rename_files.pl line 33.
rename $file, $new or die "couldn't rename $file to $new: $!";

since $new is uninitialized above it will be uninitialized here also.
you are attempting to rename a file with a valid filename to a file with
a filename that is uninitialized.
Use of uninitialized value in concatenation (.) or string at
print "Perl renamed the old files from\t\t$file\t\tto\t\t$new\n\n";

since $new is uninitialized above it will be uninitialized here also.
you are attempting to concatenate using $new which is uninitialized.

one way of doing it.

use strict;
use warnings;

my ($file, $new);

foreach $file (@ARGV)
{
if ($file =~ m{/})
{
warn "$file contains a slash. Skipping...\n";
next;
}
elsif ($file =~ m/^page\d+\.gif$/)
{
$new = "h2" . $file;

if (-e $new)
{
warn "$new already exists. Skipping...\n";
next;
}

#
# test before renaming finally the files :
#
# print "the old files will be renamed from\t\t$file\t\tto\t\t$new\n\n";

rename $file, $new or die "couldn't rename $file to $new: $!";

print "Perl renamed the old files from\t\t$file\t\tto\t\t$new\n\n";
}
}
 
M

Marek Stepanek


Thanx for your great response. You gave me great stuff to digest. I am
feeling bad, because this were obvious logical errors; but also to think
logic and perlish you have to learn and train it.


greetings from Munich



marek



--
______________________________________________________________________
___PODIUM_INTERNATIONAL_//_the_embassy_for_talented_young_musicians___
_______Marek_Stepanek__mstep_[at]_PodiumInternational_[dot]_org_______
__________________http://www.PodiumInternational.org__________________
______________________________________________________________________
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top