On Wed, Mar 10, 2004 at 10:46:05PM -0500, matt wrote:
: I was thinking along the lines of...
: 
: String $foo = "hello";
: $foo.scramble!

That would be $foo.=scramble in the current scheme of things.

: print "$foo\n";
: $foo = "hello"
: print $foo.scramble ~ "\n";
: print $foo;
: 
: OUTPUT (or close):
: elhlo
: hloel
: hello
: 
: Also, along these same things.. is there a way to apply a method to all
: variables/objects of a certain type (e.g. String, Num, etc)?  Taking the
: above example.. being able to write a method called "Scramble" that can be
: called as a method from any String type.

Two ways, actually.  You can 'reopen" the String class and add the method:

    class String is extended {
        method scramble () returns String {...}
    }

or if you consider that underhanded, you can define a multi-sub:

    multi sub *scramble (String $s) returns String {...}

If you call that as a method, and there is no ordinary scramble method,
it will "fail soft" to looking for a scramble multimethod, and end up
calling your definition.  Or you can just call it directly as a function:

    scramble("hello")

Larry

Reply via email to