On Wed, Dec 20, 2006 at 03:14:18PM -0700, Tom Smith wrote: > > Thank Chad (and John) for your input on this. I thought I'd post the > portion of the script that I was trying to work out to see if there's > room for improvement. This should work on any *nix system. The format of > the command is simple: `test.pl username`, where username is a real > username on the system in question. Here's the script:
You're welcome. > > test.pl: > #!/usr/bin/env perl > > use strict; > use warnings; > > # Determine which Linux groups the user belongs to. > open(FILE,'</etc/group') or die "Can't open /etc/group."; You probably want to use the $! variable to provide more information when your script dies on an error. Thus, the above code might be modified to look like this: open(FILE, '</etc/group') or die "Can't open /etc/group: $!"; > > my @memberof; > while ($_ = <FILE>) { > if($_ =~ /$ARGV[0]/) { > my @groups = split(/:/,$_); > push(@memberof,$groups[0]); > } > } > > close FILE; > > print "[EMAIL PROTECTED]"; > > > So is there a better way to do this, or perhaps a cleaner way? The way you did it seems to work just fine. There are other ways to do it, of course (TIMTOWTDI and all that), but which you choose might depend more on context than one being necessarily better than others. If for some reason a functional approach (no side-effects) is desirable, for instance, you might iterate through the file's contents inside a subroutine, and use perhaps a foreach loop to push() return values into the array when you call the subroutine. Depending on what else your program is going to do, this might yield greater reusability of code, but if that's not a concern for this script it really doesn't matter. For something as quick as looking up a single user's group memberships, resource footprint and processor time aren't really a concern. I guess this is a long-winded way of saying "It depends, but what you did looks okay to me." -- CCD CopyWrite Chad Perrin [ http://ccd.apotheon.org ] "It's just incredible that a trillion-synapse computer could actually spend Saturday afternoon watching a football game." - Marvin Minsky -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>