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

I'm trying to File::Copy::move a glob of files
I tried testing for $! and get a failure message for each file when it actually succeeded!
What am i missing??

Here's a code snippet (assume the glob array is ok)
(perl 5.6.1/linux)

# ------------------------ foreach my $f (@myGlob) { $!=''; # STILL complaining if (-l $f) { next; } move ($f,$RecBak) if (-f $f); # failed to skip symbolic links # till I said if -l $f next (why!?) if ($!) { print "\$!:[$!] $f FAILED! } else { print "$f moved" ;} } # ----------------------

This succeeds in moving all and only regular files as intended, but claims to fail!!
$! is always "no such file or directory".
I'm stuck..

thanks in advance,

- manchot

Replies are listed 'Best First'.
Re: File::Copy move $! is "no such file" even on success
by Abigail-II (Bishop) on Sep 12, 2003 at 15:03 UTC
    $! is only set to a useful value if the function that you call fails. If it succeeds, you cannot draw conclusions from the value of $!. If you look at the source code of File::Copy, you see that the move command first tries a rename(). If that fails, it tries copying and unlinking. But if the rename() fails, rename() sets $! Even if the copy and unlink() succeeds.

    The right way to test whether move() succeeds is by checking its return value.

    Abigail

Re: File::Copy move $! is "no such file" even on success
by jeffa (Bishop) on Sep 12, 2003 at 15:07 UTC
    How about something like this instead:
    for my $f (@myGlob) { next unless -f $f; warn "can't move $f\n" unless move ($f,$RecBak); }

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
Re: File::Copy move $! is "no such file" even on success
by hardburn (Abbot) on Sep 12, 2003 at 14:56 UTC

    $! isn't guarenteed to be reset after a successful call. I suspect you're seeing an error message from a previous call.

    Update: Ignore the above. I didn't read the code closely enough.

    ----
    I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
    -- Schemer

    Note: All code is untested, unless otherwise stated

      $! isn't guarenteed to be reset after a successful call. I suspect you're seeing an error message from a previous call.

      If you look at the code, you see that $! is set before the call to move (it would generate a warning if they are turned on, but $! is still set to 0). $! will be set by move() (or by one of the function that it calls). You just shouldn't draw conclusions from it.

      Abigail

Re: File::Copy move $! is "no such file" even on success
by Anonymous Monk on Sep 12, 2003 at 15:15 UTC

    Thanks a bunch, folks!

    I can hardly believe the instant high quality answers here! WOW!

    Thanks especially to Abigail-II for the full explanations, and to jeffa for the elegant alternative ;)

    manchot

Re: File::Copy move $! is "no such file" even on success
by Anonymous Monk on Sep 12, 2003 at 15:07 UTC
    even tho I'm explicitly setting $!='' on each iteration?
      You are explicitly setting $! before you call File::Copy::move(), so you only blow away the previous error.

      Also, 'blanking out' $! is kind of like removing the batteries from your smoke alarm - you are only going to regret it in the end.

      Happy coding,

      jeffa