A few quick Perl questions

1a  Can I create a symlink from Perl or do I have to spawn to the os?  

1b What's the most efficient way to do this?  

1c If I have to shell to do this, can you give me an example, including a method of determining whether the command was successful (from within the Perl parent)?

1d If I wanted to create multiple symlinks, what would be the most efficient way to do this in one pass? (as opposed to doing x number of shells, maybe create a script with the commands and then execute it all at once, but I'd need to know if any of the commands in the script failed).

2.  The same thing but with deleting a file.

Thanks!
LVL 1
maabuAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

adam923Commented:
1a symlink(targetfile,linkfile) is a perl function (built in) that creates linkfile as a new symbolic link, pointing to targetfile

1b use perl built in function, see above

1c see 1b

1d use a foreach loop with the symlink function on the loop body... ask more specific question if needed

2 the unlink(filename) perl built in funciton can do this. this function can also take a list of filenames (comma separated) as its argument, making the multiple-delete problem much easier
0
adam923Commented:
also,
for each symlink call the return value will be 1 (aka true) on success and 0 (aka false) on failure so an example would be
(using parallel arrays @targets and @lniks)
for($i=0;$i<$#targets;$i++){
     unless( symlink($targets[$i],$links[$i]) ){
          warn "couldn't link $links[$i] to $targets[$i]";
     }
}

for the unlink calls, the return value is the number of files successfully deletd for example:
unless( unlink(@files) == $#files ){
   warn "not all files deleted";
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
maabuAuthor Commented:
Excellent - a few more quick follow ups...

Is there a way to do a hard link in Perl as well?  

I assume a symlink won't work in a CHROOT environment if the target is above the chroot dir tree, but a hard link would - and if you know - does the hard link consume as much space as the actual file?  If you create a hard link - can you delete the other reference and as long as there is at least one hard link to the file, it will remain?  Is that how it works?  I just want to cover all the bases - thanks very much!!
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

adam923Commented:
the link function works the same way as symlink but creates "hard" links... you are correct in your description of hard links, they point to the contents of a file in the same way that a normal directory entry points to the contents of a file (well, a hard link actually is a normail directory entry so this makes sense)  sorry if that was confusing but the point is, you're right. once you make a link to a file you can remove the original and the contents would be available via the link
the hard link takes up as much space as the file's directory entry, not its contents
about the chroot -- i'm not really sure how you'd specify a target for a hard link that isn't in your current directory tree but maybe i'm interpreting this question incorrectly
0
maabuAuthor Commented:
Thanks very much - your responses were extremely helpful, prompt and right on the money!
0
adam923Commented:
no problem... if you use perl -- a free program -- enough to warrent a $37 investment in learning it you should get a copy of "Programming Perl" from O'Reilly & Associates, Inc. (make sure it's the 2nd edition, copyright 1996)  it's the "bible" of perl and has answers to all of your questions above and so much more
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.