Gabor Szabo wrote:

Hmm, I don't really know how to do this but Gaal told me to
write up some proposal here.

So we had chmod in Perl5, I would like to make sure it works in
Perl6 as well with slight modifications.

LIST = chmod MODE, LIST

1) In list context it would return the names of the files successfully
     changed .
     In scalar context it should be the number of files successfully changed
     as it is in Perl5

One might argue that it would be more useful to return a result object whose boolean value is the success or failure, whose numeric and string values are the number of files *un*changed, and whose list value is the list of those *un*changed files.

Then you could write:

    unless chmod 0o664, @files  ->  $failed {
        warn "Couldn't change the following $failed file(s): $failed[]";
    }

See below for a partial Perl 5 implementation.

BTW, I think that *many* of Perl 6's list-taking builtins could conform to the same (or to a very similar) general interface.


2) In addition to the octet representation it would also be ready to receive
    the unix style mode representation which is string
     one or more of the letters ugoa,
     one of the symbols +-=
     one or more of the letters rwxXstugo

I like this for documentation readability.


3) While some of the modes are UNIX specific, it would be nice to find similar
    modes in other operating system and do the right thing there too.
    e.g. "all" in UNIX would map to "Everyone" in Windows/NTFS

I like this for portability.


4) "filename".chmod(MODE)  should also work and I guess
    <file1 file2 file3>.chmod(MODE) should also work on those 3 files

I think this is not a good idea (as per Nicholas's previous comment), especially given that you can already write those:

     "filename" ==> chmod(MODE)
     <file1 file2 file3> ==> chmod(MODE)

if you want the filenames out front.

Damian

-----cut----------cut----------cut----------cut----------cut-----

Partial Perl 5 implementation:

    sub chmod {
        my ($mode, @files) = @_;

        $mode = _normalize_mode($mode);

        my @failed;
        FILE:
        for my $file (@files) {
            next FILE if chmod $mode, $file;
            push @failed, $file;
        }

        use Contextual::Return;
        return
           BOOL   { [EMAIL PROTECTED]  }
           SCALAR { [EMAIL PROTECTED] }
           LIST   { @failed   }
    }

    sub _normalize_mode {
        return shift @_;     # Extra smarts needed here ;-)
    }

    unless (my $failed = &chmod(0664, qw(test1 test2 test5))) {
        warn "Failed to chmod $failed file(s): @$failed";
    }

Reply via email to