develooper Front page | perl.perl5.porters | Postings from July 2013

Re: postfix dereference syntax

Thread Previous | Thread Next
Brad Gilbert
July 4, 2013 02:54
Re: postfix dereference syntax
Message ID:
On Wed, Jul 3, 2013 at 5:17 PM, David Golden <> wrote:
> On Wed, Jul 3, 2013 at 5:51 PM, D Perrett <> wrote:
>> object methods. Would we be better with a syntax for builtin methods?
>>   $arrayRef->>flatten;
>>   $hashRef->>flatten;
> Rik said "no autoboxing" anyway, but just to complete the thought, I
> think that "methods on references" and "methods on objects" could
> coexist. If a programmer doesn't know whether they have a reference or
> an object, it's up to them to sort it out.  Objects should be opaque.
> Rather than ->> I'd prefer to just see it done with CORE, if that
> could be made to work, so it's similar to ordinary superclass method
> calling.
>    $maybe_object->CORE::flatten;
> If we had core classes for containers and if references were objects
> this would all be a lot easier, but that ship has probably sailed.

Actually how about ->^

    my %h = ( map{ $_, uc $_ } 'a'..'z' );
    my $h = \%h;
    my @a = ( 'a'..'z' );
    my $a = \@a;

    say for $h->^keys->^sort;
    say for $a->^keys;

    say for %h->^keys->^sort;
    say for %a->^keys;

    package Simple { sub new{bless {}} }

    my $obj = Simple->new;
    say for $obj->keys; # defers to ->^keys

    package Complex {
      our @ISA = 'Simple';
      sub keys{
        my ($self) = @_;
        sort keys %$self;

    $obj = Complex->new;
    say for $obj->keys; # sorted
    say for $obj->^keys; # unsorted


    push $a->^@, 1..9;
    $a->^push( 1..9 );

    my @letters = $a->map(sub{ /\A\w\z/ });

We could even add to ^sort to make it effectively
do a Schwartzian transform for us.

    @sorted = @unsorted->^sort(
      sub{ $a <=> $b }, # use numeric comparison
      sub{ length($_) } # but sort by the length of the string

I would like to point out that P6 uses .^ to call methods on
an objects meta object.
Which means that these return the same thing in Perl6:

    say @a.^sort;
    say @a.HOW;
    say @a.HOW.sort;

So this may interfere with using ->^ for meta object
I think it may still be possible to use it for both
purposes though.

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About