[% setvar title Builtins : Make use of hashref context for garrulous builtins %]

This file is part of the Perl 6 Archive

Note: these documents may be out of date. Do not use as reference!

To see what is currently happening visit http://www.perl6.org/

TITLE

Builtins : Make use of hashref context for garrulous builtins

VERSION

  Maintainer: Damian Conway <damian@conway.org>
  Date: 19 Sep 2000
  Last Modified: 29 Sep 2000
  Mailing List: perl6-language@perl.org
  Number: 259
  Version: 4
  Status: Frozen
  Frozen since: v3

ABSTRACT

This RFC proposes the builtin functions that return a large number of values in an array context should also detect hashref contexts (see RFC 21) and return their data in a kinder, gentler format.

DESCRIPTION

It's hard to remember the sequence of values that the following builtins return:

        stat/lstat
        caller
        localtime/gmtime
        get*

and though it's easy to look them up, it's a pain to look them up Every Single Time.

Moreover, code like this is far from self-documenting:

        if ((stat $filename)[7] > 1000) {...}

        if ((lstat $filename)[10] > time()-1000) {...}

        if ((localtime(time))[3] > 5) {...}

        if ($usage > (getpwent)[4]) {...}

        @host{qw(name aliases addrtype length addrs)} = gethostbyname $name;

        warn "Problem at " . join(":", @{[caller(0)]}[3,1,2]) . "\n";

It is proposed that, when one of these subroutines is called in the new HASHREF context (RFC 21), it should return a reference to a hash of values, with standardized keys. For example:

        if (stat($filename)->{size} > 1000) {...}

        if (lstat($filename)->{ctime} > time()-1000) {...}

        if (localtime(time)->{mday} > 5) {...}

        if ($usage > getpwent()->{quota}) {...}

        %host = %{gethostbyname($name)};

        warn "Problem at " . join(":", @{caller(0)}{qw(sub file line)} . "\n";

Standardized keys

The standardized keys for these functions would be:

MIGRATION ISSUES

None.

REFERENCES

This RFC explains the mechanism by which HASHREF context would be detected:

RFC 21: Subroutines: Replace wantarray with a generic want function

These RFCs propose (partial) alternative solutions to this problem:

RFC 37: Positional Return Lists Considered Harmful

RFC 127: Sane resolution to large function returns

RFC 48: Replace localtime() and gmtime() with date() and utcdate()

These existing standard modules provide alternatives to various bits of the proposed mechanism. The hash keys suggested in this proposal are identical to the corresponding attribute names in these modules, except that the unnecessary prefixes have been eliminated. The length and diversity of this list might be seen as an argument for a single integrated mechanism.

        File::stat
        Net::hostent
        Net::netent
        Net::protoent
        Net::servent
        Time::gmtime
        Time::localtime
        User::grent
        User::pwent