Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello,
When trying to rename archive.txt using this variable $the_old_title the filename ends up $old_title not $old_tile.shtml.

Any Ideas Thanks

my $the_old_title = "$old_title.shtml"; rename("archive.txt","$the_old_title");

Replies are listed 'Best First'.
Re: Rename problem
by Abigail-II (Bishop) on Nov 22, 2002 at 17:29 UTC
    Perhaps the rename failed. But you never know if you don't check the return value of rename.

    Abigail

      Thanks Abigail-II, I actually changed the line but no change in filename or error reported.
      Is there another way to check the return value
        What "another way"? You used no way....

        A typical way is:

        rename ($old, $new) or die "Failed to rename $old to $new: $!\n";

        Abigail

Re: Rename problem
by tadman (Prior) on Nov 22, 2002 at 17:20 UTC
    Instead of using a temporary variable, why not just fold the two steps into one?
    rename("archive.txt", "$old_title.shtml") || die "Could not rename for some reason ($!)\n";
      Thats fine but probably in a fit of frustration I created the variable in question. As stupid as that sounds.
        Does the function return an error? Does your rename name have any unusual characters in it? Are you changing directories, for example?
Re: Rename problem
by fruiture (Curate) on Nov 22, 2002 at 17:11 UTC

    What contents does $old_title have? The Code is nearly allright: It renames 'archive.txt' to whatever is in $the_old_title and $the_old title is wahetevr was in $old_title with appended '.shtml'. You can rely on that.

    What is actually happening?

    Please check `perldoc -q "quoting.*vars"`!

    --
    http://fruiture.de
      What contents does $old_title have?
      $old_title is just a string of chars.
      What is actually happening? the resulting name is incorrect. It is missing the file extension ie .shtml
Re: Rename problem
by John M. Dlugosz (Monsignor) on Nov 22, 2002 at 20:48 UTC
    Try coding a rename with the actual name on the right, as opposed to variables and computation. If that works, you know the rename itself is OK and the problem is with the computation of the value. If that still doesn't work, the problem is with the rename, not with Perl. Perhaps file system issues... try it and report back, as this will cut down the problem size.

    —John

Re: Rename problem
by arrow (Friar) on Nov 22, 2002 at 21:49 UTC
    What you might want to try is
    my $the_old_title = "$old_title"."\.shtml";
    Then do rename("archive.txt","$the_old_title");
    Tell us what happens

    Cheers
      That was it..
      Thanks so much for your help. I hope I can return the favor sometime.
Re: Rename problem
by Dr. Mu (Hermit) on Nov 23, 2002 at 00:04 UTC
    Okay this is a long shot. I only bring it up because, in a frustrated and fatigued state, I've done the same thing. After each failed attempt, did you remember to change the errant file name back to "archive.txt", before the next try?