develooper Front page | perl.perl6.language | Postings from October 2005

Re: Sane (less insane) pair semantics

Thread Previous | Thread Next
From:
Stuart Cook
Date:
October 9, 2005 16:27
Subject:
Re: Sane (less insane) pair semantics
Message ID:
49b351060510091627n309183fcu@mail.gmail.com
The think I don't like about `foo( *$bar )` is that it's not clear
whether you're splatting a pair, or a hash, or an array, or a complete
argument-list object.  This is probably fine for quick-'n'-dirty code,
but I'd like to encourage a more explicit style:

  my %hash = (a=>'b', c=>'d');
  foo( *%hash );  # splat a hash as named arguments
  # => foo( a=>'b', c=>'d' );

  my $pair = a=>'b';
  foo( *%$pair );  # view the pair as a 1-elem hash, and splat that

  my $href = \%hash;  # or just %hash
  foo( *%$href );  # view the hashref as a hash, and splat that

  sub returns_a_hash { ... }
  foo( *%{returns_a_hash} );  # call the sub, view the result as a
hash, and splat that

  my @array = (1, 2, 3);
  foo( *@array );  # splat an array as positional arguments
  # => foo( 1, 2, 3 );

  my $aref = \@array;  # or just @array
  foo( *@$aref );  # view the arrayref as an array, and splat that

  sub returns_an_array { ... }
  foo( *@{returns_an_array} );  # call the sub, view the result as a
hash, and splat that

In this style, the splat character (*) is always next to a sigil (% or
@) telling you exactly how the arguments are being substituted (named
or positional).

I'm also a little wary of giving parens the power to change pair
behaviour, but the rules are simple enough that I can probably get
over it.  The other proposal, IIRC, was to have `named()` and `pair()`
special-forms for forcing the desired behaviour; these would only be
valid in argument lists.


Stuart

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About