Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

How to chgrp in perl?

by neniro (Priest)
on Apr 04, 2004 at 10:46 UTC ( #342448=perlquestion: print w/replies, xml ) Need Help??

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

I'm searching a way to chgrp in perl. I found chown, but it needs numerical IDs. I'm developing using Windows (yes thats odd because the target is linux) - so I tried the following code:
#!/usr/bin/perl use strict; use warnings; my $group = 'users'; my @folder = qw ( worksheets correspondence ); &chgrp($group, \@folder); sub chgrp { my ($group, $folder) = @_; eval { system( "chgrp $group @$folder" ) }; warn $@ if $@; }
It works as expected - on a Windows-system it just warns that there is no chgrp-command. Is there a better way to do this? I searched on CPAN for 'chgrp' but I found nothing.

Replies are listed 'Best First'.
Re: How to chgrp in perl?
by PodMaster (Abbot) on Apr 04, 2004 at 10:56 UTC
      The solutions i saw, searching Perlmonks, use chown and numerical-IDs. They resolve these by using getpwnam. I'd like to enhance my script, described in Using perl for creating project folders, in a way that it is possible to declare group rights in the xml-schema. It is much more flexible if i can use groupnames instead of numerical IDs.
        getgrnam, in scalar context, returns the group ID of the named group:
        my $gid = getgrnam("users");
        Now you can use the chown function.

        perldoc -f getgrnam gives more detailed information about getgrnam (and its sibling functions).


Re: How to chgrp in perl?
by borisz (Canon) on Apr 04, 2004 at 13:54 UTC
    You can use perl's chown to change the group.
Re: How to chgrp in perl?
by Vautrin (Hermit) on Apr 05, 2004 at 05:00 UTC
    If the target is a *nix system, why not just save yourself the trouble and use a chgrp command in backticks, or via a system() call. If you want to get fancy you could check the OS type or for the existence of a chgrp command in your path, and only chgrp if you know you can.

    Want to support the EFF and FSF by buying cool stuff? Click here.
Re: How to chgrp in perl?
by Abigail-II (Bishop) on Apr 05, 2004 at 09:20 UTC
    Nothing wrong with using system. There will always be purists who insist of writing complicated back bending C style code to avoid using system (and you can in this case, using chown and getgrnam), but they haven't seen the light yet, and don't appreciate Perls root: being a glue language.

    You could avoid the warning by checking for the OS, or the existance either /bin/chgrp or /usr/bin/chgrp (where you would find them on a UNIX system). One thing I have to ask though, what's with the eval?


      I use the eval block to try and not die. It isn't necessary in this case.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://342448]
Approved by grinder
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2022-05-26 02:56 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (92 votes). Check out past polls.