in reply to Re: More efficient dereferencing of a subroutine return
in thread More efficient dereferencing of a subroutine return

If the dereferencing of an array returned by a function is my $arrayref = \@{ function() } then how does that work with the keys() function?

I tried my $arrayref = \@{ keys(%$someHashRef) || [] } but something makes it break.

What makes the keys() function a special case?

Replies are listed 'Best First'.
Re^3: More efficient dereferencing of a subroutine return
by choroba (Cardinal) on Apr 12, 2021 at 13:27 UTC
    You say "dereferencing", but at the same time "$arrayref". That's confusing.

    To dereference an array reference returned from a subroutine, you can use:

    my @array = @{ sub_returning_aref() };

    To get the actual reference, no dereferencing is needed:

    my $aref = sub_returning_aref();

    On the other hand, if a subroutine returns a list, you can assign it directly to an array:

    my @array = sub_returning_list();

    To create an array reference, you need to create an anonymous array, as the subroutine returns a list, not an array.

    my $aref = [ sub_returning_list() ];

    There's nothing special about keys.

    my @array = keys %hash; my $aref = [ keys %hash ];

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
      Ah! Indeed... I must still be asleep... :) I meant referencing, not DEreferencing, so [ keys %hash ] is what I was looking for. Thank you!