[% setvar title Builtins : Make use of hashref context for garrulous builtins %]
Note: these documents may be out of date. Do not use as reference! |
To see what is currently happening visit http://www.perl6.org/
Builtins : Make use of hashref context for garrulous builtins
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
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.
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";
The standardized keys for these functions would be:
'dev' Device number of filesystem 'ino' Inode number 'mode' File mode (type and permissions) 'nlink' Number of (hard) links to the file 'uid' Numeric user ID of file's owner 'gid' Numeric group ID of file's owner 'rdev' The device identifier (special files only) 'size' Total size of file, in bytes 'atime' Last access time in seconds since the epoch 'mtime' Last modify time in seconds since the epoch 'ctime' Inode change time in seconds since the epoch 'blksize' Preferred block size for file system I/O 'blocks' Actual number of blocks allocated
localtime
/gmtime
'sec' Second 'min' Minute 'hour' Hour 'mon' Month 'year' Year 'mday' Day of the month 'wday' Day of the week 'yday' Day of the year 'isdst' Is daylight savings time in effect (localtime only)
caller
'package' Name of the package from which sub was called 'file' Name of the file from which sub was called 'line' Line in the file from which sub was called 'sub' Name by which sub was called 'args' Was sub called with args? 'want' Hash of values returned by want() 'eval' Text of EXPR within eval EXPR 'req' Was sub called from a C<require> (or C<use>)? 'hints' Pragmatic hints with which sub was compiled 'bitmask' Bitmask with which sub was compiled
getpw*
'name' Username 'passwd' Crypted password 'uid' User ID 'gid' Group ID 'quota' Disk quota 'comment' Administrative comments 'gcos' User information 'dir' Home directory 'shell' Native shell 'expire' Expiry date of account of password
getgr*
'name' Group name 'passwd' Group password 'gid' Group id 'members' Group members
gethost*
'name' Official host name 'aliases' Other host names 'addrtype' Host address type 'length' Length of address 'addrs' Anonymous array of raw addresses in 'C4' format
getnet*
'name' Official name of netwwork 'aliases' Other names for network 'addrtype' Type of network number 'net' Network number
getproto*
'name' Official name of protocol 'aliases' Other names for protocol 'proto' Protocol number
getserv*
'name' Official name of service 'aliases' Other names for service 'port' Port at which service resides 'proto' Protocol to be used
None.
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