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

I'm stumped, have tried a few different things (such as changing path names, creating dirs first, using "rename" instead of "move"), but I simply CANNOT get this script to work.
It does not die.

As is fairly obvious from the script, I am trying to move a bunch of mp3s (all in their own subdirs) to a new subdir. Each list is contained in a file named mp3s1-9.

For example "/home/ftp/pub/mp3/nin/nine_inch_nails_fixed_06_screaming_slave.mp3 to /home/ftp/pub/mp3/mp3s7_dir/nin/nine_inch_nails_fixed_06_screaming_slave.mp3"

This is in preparation for a whole whack of backup-burning. I can't see the problem
#!/usr/bin/perl -w use strict; use File::Copy; #rather than renaming my $path = "/home/ftp/pub/mp3s/"; #just in case move needs a full path chdir $path; my @lists = <mp3s?>; foreach (@lists){ open (FH, $_) || die "Could not read file :$!"; my $dir = $_ . "_dir"; mkdir $dir; #dir that we want to move the files into while (<FH>){ chomp; my $orig = $_; s!/.*/(.*/.*)!$1!; #keep last dir + file name in new dir my $dest = $path . $dir . "/" . $_; move $orig,$dest || die "Could not move $orig to $dest: $!"; } }
If I add a print right before the move, $orig and $dest print out correctly.
The dirs do get created properly, and the script does not die. This was a chunk of a larger script that was going to move a chunk once size limit was reached, but because of the problems I had, I moved this portion to this script.

Replies are listed 'Best First'.
Re: Moving?
by Fastolfe (Vicar) on Apr 14, 2001 at 04:34 UTC
    I don't see anything obvious, but this may be tripping you up:
    move $orig,$dest || die "Could not move $orig to $dest: $!"
    You should either be using 'or' instead of || here or wrap your arguments to move in parens. || is of a high precedence, so your statement is interpreted like:
    move($orig, $dest || die "...");
    Which means if the move failed, you get no explanation why and your script continues blindly forward. You could be in some kind of loop and just don't know it.

    In addition, you indicated that you had debugging code in there before, but the best you can do to explain the problem is "it doesn't die". Can you determine where in your script it's hanging? Is it in an infinite loop somewhere? See if you can isolate the problem spot.

      Hmm, Ok, it after wrapping it in parens the script is dieing at the move point. At that point it gave me an error stating the the file could not be found. hmm..

      Problem fixed! - The regex was capturing the last directory + file name.. (nin/...) whoops, when I attempted to move it to that directory it did not exist :-)

      Sorry about the brevity of the error description previously, but there was no "hang", nor error messages. The script ran, created the dirs, and stopped..

      Thank you for the assistance Fastolife, you got me on the right track.. damned precedence ;-)