Path: utzoo!censor!geac!torsqnt!news-server.csri.toronto.edu!cs.utexas.edu!sun-barr!newstop!texsun!convex!convex.com!tchrist From: tchrist@convex.com (Tom Christiansen) Newsgroups: comp.lang.perl Subject: fun with scoping Message-ID: <111557@convex.convex.com> Date: 17 Dec 90 04:28:35 GMT Sender: news@convex.com Reply-To: tchrist@convex.com (Tom Christiansen) Organization: Convex Computer Corp, Richardson, TX Lines: 33 Once upon a time I had an annoying problem: I diff the run of a nightly find to discover changes to setid files and devices. Now, I happen to have several accounts, like uucp and notes, that have files of this sort and with more than one login to each uid, so every time the passwd and group files hash (YP), I pull out a different id and so get reams of nightly output. This annoyed me. So I wrote the following filter to convert everything out of 'find -ls' back into numbers so it would stop changing: #!/usr/bin/perl $fmt = 'a22 A9 A8 a*'; while (<>) { ($pre, $login, $group, $post) = unpack($fmt, $_); print pack($fmt, $pre, &get(*login), &get(*group), $post); } sub get { local(*id) = @_; $id{$id} = $id =~ /^\d+$/ ? $id : &id unless defined $id{$id}; $id{$id}; } sub login { (getpwnam($id))[2]; } sub group { (getgrnam($id))[2]; } I would say that this is an interesting program, eh? :-) If this isn't a great demo of Perl's um, unique, scoping behavior, I don't know what is. If you think that's scary, note that if we s/get/id/g the program still works! --tom -- Tom Christiansen tchrist@convex.com convex!tchrist "With a kernel dive, all things are possible, but it sure makes it hard to look at yourself in the mirror the next morning." -me