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

Hello wise ones,

I need to know if there's any possible way to override a file test operator. Specifically, I need to override the -l file test. This is an example:

my $isSymlink = -l "path/to/file";

Now, before you answer "why in earth would you even want to do that?", I explain. Perl implementation on Win32 doesn't implement the CORE functions symlink, readlink and file-test "-l", based on the wrong legacy idea of Windows not supporting symlinks. NTFS implements complete symlink functionality although it wasn't very well documented until Windows Vista was released.

I'm trying to run some modules that rely a lot on the symlink Perl functionality and cannot work on Win32 only because of these functions/operator not being implemented.

I already managed to override the CORE functions symlink and readlink in any namespace by the use of a overriding module I created and the use of the special file site/lib/sitecustomize.pl. Both functions work great and return the same result you would expect on unix. But I cannot find a way to do the same to the "-l" file test.

Is there someone here with that depth of knowledge of Perl tricks/secrets? I hope so!

Thanks in advance for any advice. :-)

Francisco

Replies are listed 'Best First'.
Re: How to override a "file test" operator
by eyepopslikeamosquito (Archbishop) on Mar 10, 2012 at 09:06 UTC

    I already managed to override the CORE functions symlink and readlink in any namespace by the use of a overriding module I created
    Why write your own? Did you experience problems with Win32::Symlink?

Re: How to override a "file test" operator
by Marshall (Canon) on Mar 10, 2012 at 05:52 UTC
    I am still on 5.10.x, but from reading the 5.14 documentation overload, it appears that it is possible to overload the file test operators, although its not easy to do.
    File tests The key '-X' is used to specify a subroutine to handle all the filetest operators (-f , -x , and so on: see -X for the full list); it is not possible to overload any filetest operator individually. To distinguish them, the letter following the '-' is passed as the second argument (that is, in the slot that for binary operators is used to pass the second operand). Calling an overloaded filetest operator does not affect the stat value associated with the special filehandle _ . It still refers to the result of the last stat, lstat or unoverloaded filetest. This overload was introduced in Perl 5.12.
    I sometimes use the _ filehandle in Perl 5.10. I take this 5.14 description to mean that this feature won't work if you overload the file descriptors to follow Win32 sym links. I think that what you want to do is possible although not easy.
Re: How to override a "file test" operator
by Anonymous Monk on Mar 10, 2012 at 03:16 UTC

    File test operators are one of those things you cannot override

    However, if you make an object of the thing you're using file test operators on, then you can use overload

    update: actually, it might be possible to accomplish using B::Generate, but that is PerlAssembly, yuck :)