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

Dear Monks,

I need to find the perl equivalent to this unix/linux (yes we are supporting both) command:

chmod g+s directory

Would I just be able to add the same thing to my script without using a system command or is there something else I need? I can't find any perl examples using g+s.

Thanks, Michele

Replies are listed 'Best First'.
Re: chmod in perl script
by bellaire (Hermit) on Feb 04, 2011 at 14:37 UTC
    You do use chmod, but you have to keep in mind that unlike the command-line chmod, which can simply flip bits on or off, perl's chmod sets the entire permission set as a single unit. Basically, you have to read the current permissions off the file using stat, add group sticky to those permissions bitwise, and then apply the new permissions using chmod. I think the group sticky bit is 02000, so that'd be:
    my $filename = 'filename'; my $perms = ((stat($filename))[2] & 07777); # Just permissions, not + type my $newperms = $perms ^ 02000; # Add the group sticky +bit chmod $newperms, $filename or die "Could not change permissions: $!";

      Thanks a lot Monks!

      I wound up using bellaire's suggestion and it worked perfectly.

      Take care, Michele

Re: chmod in perl script
by marto (Cardinal) on Feb 04, 2011 at 13:38 UTC
Re: chmod in perl script
by Corion (Patriarch) on Feb 04, 2011 at 13:38 UTC

    Have you looked at chmod? Finding out the numeric values for g+s is likely easiest done by using ls on such a directory and then looking at the numeric value for the permissions. Likely it's 04000 for setting the group-sticky-bit, but I don't know.

Re: chmod in perl script
by ambrus (Abbot) on Feb 05, 2011 at 08:40 UTC

    I think we've answered this in the chatterbox. My suggestion was

    my @s = lstat $dirname or die "stat $!"; -l _ and die "islink $!"; chmod+($s[2] | 02000), $dirname or die "chmod";

    If you prefer symbolic constants, you can use Fcntl "S_ISGID"; and then write S_ISGID() instead of 02000.

    You could also try the File::chmod module to use symbolic mode letters.