views:

98

answers:

2

In the summary of differences between Perl 5 and Perl 6, it is noted that the wantarray function is gone:

wantarray() is gone

wantarray is gone. In Perl 6, context flows outwards, which means that a routine does not know which context it is in.

Instead you should return objects that do the right thing in every context.

Could someone provide an example of how such an object is created?

+3  A: 

I think 2 examples might be:


http://perlcabal.org/syn/S13.html#Type_Casting

A class may define methods that allow it to respond as if it were a routine, array, or hash. The long forms are as follows:

method postcircumfix:<( )> ($capture) {...}
method postcircumfix:<[ ]> (**@slice) {...}
method postcircumfix:<{ }> (**@slice) {...}

Those are a bit unwieldy, so you may also use these short forms:

method &.( $capture ) {...}
method @.[ **@slice ] {...}
method %.{ **@slice } {...}

Also, I think this might be relevant though less so: http://perlcabal.org/syn/S12.html

Search for:

You may write your own accessors to override any or all of the autogenerated ones.

So you return an object which has several context-specific accessors.


Interestingly enough, it started out with Perl6 replacing "wantarray" with a generic "want": RFC 98 (v1) context-based method overloading, circa 2000, also at http://dev.perl.org/perl6/rfc/21.html . I'm not sure why/when the change was made.

DVK
+2  A: 

This comment on Reddit about the blog post Immutable Sigils and Context gives the following examples:

class GeoLocation is Array {
    method Str { 'middle of nowhere' }
}

sub remote_location {
    return GeoLocation.new(1e6 xx 3);
}

# or even easier:

sub remote_location {
    return (1e6 xx 3) but 'middle of nowhere';
}

/I3az/

draegtun