[newbie] rename not working

Discussion in 'Perl Misc' started by Marek Stepanek, Jan 25, 2005.

  1. 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__________________
    ______________________________________________________________________
    Marek Stepanek, Jan 25, 2005
    #1
    1. Advertising

  2. Marek Stepanek

    Paul Lalli Guest

    "Marek Stepanek" <> wrote in message
    news:BE1C6720.ED0F%...
    >
    > 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
    Paul Lalli, Jan 25, 2005
    #2
    1. Advertising

  3. Marek Stepanek <> wrote in
    news:BE1C6720.ED0F%:

    > 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.
    A. Sinan Unur, Jan 25, 2005
    #3
  4. Marek Stepanek

    Matija Papec Guest

    X-Ftn-To: Marek Stepanek

    Marek Stepanek <> wrote:
    ># 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 ..



    --
    Matija
    Matija Papec, Jan 25, 2005
    #4
  5. On Tue, 25 Jan 2005, Marek Stepanek wrote:

    >
    >
    > 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";
    }
    }


    --
    terry l. ridder ><>
    terry l. ridder, Jan 25, 2005
    #5
  6. On 25.01.2005 21:58, in article
    , "terry l. ridder"
    <> wrote:

    > snip


    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__________________
    ______________________________________________________________________
    Marek Stepanek, Jan 26, 2005
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    126
    Views:
    1,760
    Jan Claeys
    Dec 8, 2007
  2. Snoopy Dog

    Newbie - File Rename

    Snoopy Dog, Oct 13, 2006, in forum: Ruby
    Replies:
    5
    Views:
    124
    Snoopy Dog
    Oct 16, 2006
  3. Peter Bailey
    Replies:
    20
    Views:
    230
    Bill Kelly
    Oct 9, 2007
  4. Tom Bates
    Replies:
    5
    Views:
    182
    Chris Mattern
    Apr 25, 2005
  5. Replies:
    3
    Views:
    88
    Tad McClellan
    Sep 9, 2006
Loading...

Share This Page