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

I've written a script that, at one point, creates a temporary directory, creates a symlink of a file in that directory, then does some other stuff. After performing some unrelated stuff, it uses unlink to delete the symlink. Or, that's what I was hoping would happen. Instead, unlink reports in $! that the file does not exist. Here's a snippet from my code:
# This part works beautifully... $pathToFiles = '/home/pencilneckgeek/files'; $tempDir = '/home/pencilneckgeek/tmp/' + random(10000); mkdir($tempDir) or die "mkdir: $!\n"; $origFile = "$pathToFiles/theFile.txt"; $theLink = "$tempDir/theFile.txt"; symlink($origFile, $theLink) or die "symlink: $!\n"; # do some stuff... ... # now, here's where the problem lies... unlink($theLink) or die "unlink: $!";
which dies at the unlink, reporting "unlink: No such file or directory" $theLink doesn't change at all at any point in the code - the file's path/name is simply sent off to another program (which currently does nothing)... Any ideas?

Replies are listed 'Best First'.
Re: unlink unworking
by chromatic (Archbishop) on May 17, 2000 at 06:02 UTC
    I had to make a few changes to get this to compile, but when I did it worked correctly:
    #!/usr/bin/perl -w use strict; my $pathToFiles = '/home/pencilneckgeek/files'; my $tempDir = '/home/pencilneckgeek/tmp' . rand(10000); mkdir($tempDir, 0777) or die "mkdir: $!\n"; $origFile = "$pathToFiles/theFile.txt"; $theLink = "$tempDir/theFile.txt"; symlink($origFile, $theLink) or die "symlink: $!\n"; # do some stuff... ... # now, here's where the problem lies... unlink($theLink) or die "unlink: $!";
    First, I used the concatenate . operator (see perlop) instead of addition to create $tempd. Then, I gave mkdir a mode for the new directory. It fails otherwise.

    Things worked fine after that.

    One troubleshooting tip. I always like to print variables (or look at them in a debugger) when things fail suddenly. This should help: print "theLink is set to ->$theLink<-\n";

Re: unlink unworking
by Maqs (Deacon) on May 17, 2000 at 13:16 UTC
    To avoid errors in creating links and directories you can use readlink function
    i.e. readlink($theLink); to chech if the link exists.
    Then you'd better use time() function to create a uniq name of your directory
    i.e.
    $dirname = $path.time();

    /Maqs.
Re: unlink unworking
by btrott (Parson) on May 17, 2000 at 05:53 UTC
    Why don't you change your unlink line to
    unlink($theLink) or die "unlink of $theLink failed: $!";
    That'll tell you what it's trying to unlink, at least.
Re: unlink unworking
by perlmonkey (Hermit) on May 17, 2000 at 12:12 UTC
    I am pretty sure chromatic is correct with the concantenation thing. That and random was not defined for me.
    #!/usr/bin/perl -w use strict; my $tempDir = '/home/pencilneckgeek/tmp/' + rand(10000); print $tempDir, "\n";
    Results:
    Argument "/home/pencilneckgeek/tmp/" isn't numeric in add at - line 2.
    2723.31736981869
    Where the big number is supposed to be $tempDir, which is obviously not what you wanted.
RE: unlink unworking
by turnstep (Parson) on May 17, 2000 at 06:17 UTC

    > $theLink doesn't change at all at
    > any point in the code

    Either it does, or you may have mis-typed the variable name (e.g. $TheLink). The use strict should help ferret the latter out, and btrott's suggestion should show the former.