How to create a symbolic link using Perl?

I am trying to run the below code in Perl:

symlink($oldname,$newname) or die print "$!\n"; 

but I get an error:

 The symlink function is unimplemented at C:\...\CreateSymlink.pl line 14. 

If I changed the code to:

 link($oldname,$newname) or die print "$!\n"; 

then hard links are created without errors.

Using Perl activation and Windows 7 32-bit.

Any ideas why this is happening? I am trying to create symbolic links for a file.

PS: I am new to Perl.

+4
source share
2 answers

What is the "appearance" of the "symbolic link" ?; -)

  • The abbreviated .LNK ? (Win95 +, not transparent)
  • A connection point ? (Win2K +, only local file systems / directories)
  • True NTFS symlink ? (Vista / 2008 / Win7 +)

Oh, and read the symlink in the Wikipedia article above for the mklink ;-) Back-ticks (or system) may be a good friend, but Note:

The default security settings in Windows Vista / Windows 7 prevent non-administrators and non-administrators from creating symbolic links. This behavior can be changed [using the security policy setting] ....

Happy coding.


The WinAPI CreateSymbolicLink function can be used directly; I am not sure if it is โ€œsufferingโ€ from the same restriction as the mklink above. However, this flow indicates that it is still operational.


FWIW, this "works" in Strawberry Perl 5.12. YMMV, I just typed this and never used it otherwise :-)

 use Win32::API; $fn = Win32::API->new( # Note "A" function, IDK how to use Unicdoe "kernel32", "BOOLEAN CreateSymbolicLinkA(LPTSTR lpSymlinkFileName, LPTSTR lpTargetFileName, DWORD flags)" ); unlink("src.txt"); unlink("lnk.txt"); open(FH,">src.txt") or die $!; close(FH); print "src.txt exists? " , (-f "src.txt"), "\n"; print "lnk.txt exists? " , (-f "lnk.txt"), "\n"; $hr = $fn->Call("lnk.txt", "src.txt", 0); print "Result: ", $hr, "\n"; print "lnk.txt exists? ", (-f "lnk.txt"), "\n"; open(FH,">>src.txt") or die $!; print FH "hello world!\n"; close(FH); open(FH,"<lnk.txt") or die $!; print "linked data: ", scalar(<FH>), "\n"; close(FH); 

My results (run as "Administrator" - may not work for "other users" - I donโ€™t know why, but my cmd.exe always opens with elevated privileges):

  src.txt exists?  1
 lnk.txt exists?
 Result:
 lnk.txt exists?  1
 linked data: hello world! 

Directory List:

  10/22/2011 02:53 PM <DIR>.
 10/22/2011 02:53 PM <DIR> ..
 10/22/2011 02:54 PM 636 foo.pl
 10/22/2011 02:53 PM <SYMLINK> lnk.txt [src.txt]
 10/22/2011 02:53 PM 14 src.txt 

I have no idea what the [subtle] differences can be, if any, between NTFS symbolic links and "UNIX" symbolic links. In addition, pre-Vista / 2008 will not work above - previous versions of NTFS do not support symbolic links (and previous versions of windows do not have the CreateSymbolicLink function).

+9
source

Bypass done:

 my $oldfilename = File::Spec->catfile($oldname); my $newfilename = File::Spec->catfile($newname); if(-f $newfilename){ } else { @args = ("mklink", $newfilename, $oldfilename); system(@args) == 0; } 
+1
source

All Articles