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

Hi Monks
Can someone explain this code to me, I am getting an error on the "die".
Thanks a lot!
my @dir = $ftp->dir($file); if ($dir[0] =~ /$size/) { unlink($file) or die $@; } else { warn "File size differs for $filename ($size): @dir"; }

Replies are listed 'Best First'.
Re: Code Error Question
by Joost (Canon) on May 17, 2007 at 19:55 UTC
    It should very likely be:
    unlink($file) or die $!;
    In other words, unlink ("delete") the file specified in $file or die giving the system's error message.

    If you want to have a clearer error message, use:

    unlink($file) or die "Can't unlink '$file': $!";
    update: I have no idea what the /$size/ match is supposed to do, but it's probably wrong. Also, having a variable named $file and another named $filename in a related code block is suspicious - especially if you're not using strict and warnings.

Re: Code Error Question
by kyle (Abbot) on May 17, 2007 at 19:59 UTC

    Is $file a directory or a file? If you want to delete a directory, use rmdir (if it's empty) or rmtree in File::Path (if it's occupied). If you unlink a file, the error will be in $! (not $@). See perlvar. Finally, your warn refers to a $filename variable that isn't used anywhere else. Maybe what you're trying to do is:

    my @dir = $ftp->($file); if ( $dir[0] =~ /$size/ ) { unlink $file or die "Can't unlink '$file': $!"; } else { warn "File size differs for $file ($size): $dir[0]\n"; }