SPUG: Minimal Perl talk at UW on Thursday

John W. Krahn krahnj at telus.net
Tue Nov 14 14:37:12 PST 2006


Tim Maher wrote:
> On Tue, Nov 07, 2006 at 10:13:48PM -0800, Tim Maher wrote:
>>The next Seattle Area System Administrator's Guild meeting is
>>Thursday, November 9, 2006 at 7pm.
>  
> I've posted the slides of my talk at http://MinimalPerl.com; look
> near the bottom of the home page.

I hope you don't mind,  :-),  some comments on the listfiles script:

The file http://minimalperl.com/listfiles.plx appears to be HTML but because
of the .plx extention it displays as plain text.


>         # Doesn't handle ls's -a, etc., so discard
>         # hyphen-prefixed options, if any
>         while ( defined $ARGV[0] and $ARGV[0] =~ /^-/ ) {
>             warn "$0: Ignoring option: '$ARGV[0]'\n";
>             shift;
>         }

What if you have file names that begin with a '-' character?


>         -e $filename or
>             warn "$0: '$filename'; $!\n" and next;
> 
>         $target="";
>         # symlinks need to be read using "lstat"
>         if (-l $filename) {  # file is a symlink
>             (undef, undef, $mode, $nlink, $uid, $gid,
>                 undef, $size, undef, $mtime)=lstat $filename;
>             $target=(readlink $filename or '(nowhere)');
>         }
>         else { # not a symlink
>             (undef, undef, $mode, $nlink, $uid, $gid,
>                 undef, $size, undef, $mtime)=stat $filename;
>         }

You are stat()ing the same file three times, why not just once:

        my ( $mode, $nlink, $uid, $gid, $size, $mtime ) = ( lstat $filename )[
2 .. 5, 7, 9 ];
        -e _ or warn "$0: '$filename'; $!\n" and next;
        my $target = -l _ ? readlink( $filename ) || '(nowhere)' : '';


>         # convert UID-number to string
>         $uid_name=$uid; # keep numeric ID if can't convert to user-name
>         my $x=getpwuid $uid;
>         defined $x and $x =~ /[a-zA-Z]/ and $uid_name=$x;
> 
>         # convert GID-number to string
>         $gid_name=$gid; # keep numeric ID if can't convert to user-name
>         $x=getgrgid $gid;
>         defined $x and $x =~ /[a-zA-Z]/ and $gid_name=$x;

Is there some Unix or Posix specification that requires the /[a-zA-Z]/ test?

Or to put it another way, if the test failed would that be proof that the name
was invalid?

Or to put it another way, if you got a valid name from getpwuid/getgrgid and
it didn't match /[a-zA-Z]/ how did it get into the file in the first place?



John
-- 
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order.       -- Larry Wall


More information about the spug-list mailing list