• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 190
  • Last Modified:

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!
0
maabu
Asked:
maabu
  • 4
  • 2
1 Solution
 
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
 
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
[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

 
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

Featured Post

[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now