* Ovid <[EMAIL PROTECTED]> [2007-04-16 12:55]:
> Ugh.  I hated my double call to stat().
> 
>     $ENV{PATH} = do {
>         my $sep = $Config{path_sep};
> 
>         join $sep, ( 
>             map { /(.*)/ }
>             grep {
>               local $_ = stat $_;
>               defined && !( $_->mode & S_IWOTH )
>             }
>             grep { file_name_is_absolute($_) }
>             split( /\Q$sep/, $ENV{PATH}, -1 ),
>         );
>     };

Note that `local $_` has issues when tieing or other magic are
involved. If you want to topicalise something, use `for` instead.
That’s not viable in this case, though, since `for` doesn’t
return a well-defined value and you need that for `grep`. Better
just use a lexical variable.

Also note that if `stat` succeeds it will return an object, which
can never be false. Testing definedness is therefore unnecessary,
you can just test truth.

Finally, if you switch to the low-precedence boolops you can get
rid of those parens. At that point you have the version I posted.

What I learned from this excercise is that Perl has no good API
to encapsulate stat(2). What I *wanted* to write is this:

    $s and $s->perm( 'o-w' )

where the `o-w` is a string that follows symbolic chmod mode
syntax. (So `o-w` means “others may not write”, `o+w` means
“others may write”, and `o=w` means “others may write, but not
read or execute,” and you can equally use variations on the theme
like `ug+rx,o=x`.)

That is much easier to decipher than all that C-ish bitfiddle.

Regards,
-- 
Aristotle Pagaltzis // <http://plasmasturm.org/>

Reply via email to