in reply to Re: symbolic links
in thread symbolic links
Also, if you're trying to create the link on the parent directory, the destination string should read "/home/test/", not "/home/folder" (you should probably not hardcode it like that either, as any typo can bring unexpected bugs like this one. Use you $dir variable! :-)
But what I would really warn you about is on putting the returned value of symlink() back on $file. Not only it destroys your filename for further use, the variable name differing from what it actually does (or any multi-use variable at any rate) will most likely confuse you if your program escalates.
Even worse, if a user tries to run your program on a filesystem that does not support symbolic links, it will cause a fatal error unless you trap it in an eval block. You could do something like:
Of course, if this is just a simple PoC code where you were not particularly concerned with the pointed issues, feel free to ignore them :-)#!/usr/bin/perl use strict; use warnings; my $dir = "/home/test/folder"; opendir(BIN, $dir) or die "Can't open $dir: $!\n"; my @array = grep { -f "$dir/$_" } readdir BIN; foreach my $file (@array) { eval { symlink ("$dir/$file","$dir/../$file") }; # links to parent if ($@) { print "could not create symlink for $file\n"; } else { print "success!\n"; } }
Hope this helps!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: symbolic links
by muizelaar (Sexton) on Jul 04, 2007 at 19:24 UTC |