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

what is line of code means?
unlink $path if -f $path;
thanks

Replies are listed 'Best First'.
Re: code explanation
by johngg (Canon) on May 21, 2009 at 10:07 UTC

    Be aware that on *nix, the -f file test will be true for a symbolic link that points to a plain file. Apply the -l test as well if this is a concern.

    $ cat > rubbish a load of rubbish $ ln -s rubbish linkToRubbish $ cat rubbish a load of rubbish $ cat linkToRubbish a load of rubbish $ $ ls -l rubbish linkToRubbish lrwxrwxrwx 1 johngg None 7 May 21 10:51 linkToRubbish -> rubbish -rw-r--r-- 1 johngg None 18 May 21 10:51 rubbish $ $ perl -le ' > $file = q{linkToRubbish}; > unlink $file if -f $file;' $ ls -l rubbish linkToRubbish ls: cannot access linkToRubbish: No such file or directory -rw-r--r-- 1 johngg None 18 May 21 10:51 rubbish $ $ ln -s rubbish linkToRubbish $ cat linkToRubbish a load of rubbish $ $ perl -le ' > $file = q{linkToRubbish}; > unlink $file if -f $file and not -l $file;' $ ls -l rubbish linkToRubbish lrwxrwxrwx 1 johngg None 7 May 21 11:01 linkToRubbish -> rubbish -rw-r--r-- 1 johngg None 18 May 21 10:51 rubbish $

    I hope this is of interest.

    Cheers,

    JohnGG

      thanks..
Re: code explanation
by akho (Hermit) on May 21, 2009 at 09:43 UTC
    What part you don't understand? It says "delete file $path if it is a plain file (i.e. not a directory, special file or somesuch)".
      thanks for the reply it helped me
Re: code explanation
by Anonymous Monk on May 21, 2009 at 09:45 UTC
    It is only 5 words, 2 of which are $path
    if( -f $path ){ unlink $path } perldoc -f unlink unlink LIST unlink Deletes a list of files. ... perldoc -f -f ... -f File is a plain file.
      thanks for the answer
Re: code explanation
by tokpela (Chaplain) on May 21, 2009 at 12:53 UTC
Re: code explanation
by toolic (Bishop) on May 21, 2009 at 13:33 UTC
    Specific doc links:

    unlink

    -X

    and from the command-line

    perldoc -f unlink perldoc -f -X
Re: code explanation
by Apocrypha (Initiate) on May 21, 2009 at 13:29 UTC
    That will remove the file $path if it is a file (-f) and not a directory or symlink.
Re: code explanation
by Anonymous Monk on May 21, 2009 at 13:52 UTC
    thanks for the replies monks, can you explain what is this expression do?
    for (readdir DIR) { next if /^\.{1,2}$/; my $path = "$dir/$_";
    thanks
      Virtually every reply to your first question showed you how to try to answer basic questions yourself... maybe you should give that a try, then let everyone know what parts you didn't understand.
        i read from help docs which says that the opendir is used to open up a specified directory and the next is like a continue statement in C and i did not understand the part
        if /^\.{1,2}$/;