(It seems you're confused about my position because I was sloppy
presenting it.  My apologies; hopefully this will clear a few things
up.)

On 10/10/05, Uri Guttman <[EMAIL PROTECTED]> wrote:
> Stuart Cook <[EMAIL PROTECTED]> writes:
>
> > 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:
>
> but perl will know the type of the value in $bar and expand/splat it
> accordingly.

I realise that perl can cope with it--my concern was more for the
person reading the code.  Just like perl can cope without explicit
type annotations, but some of us will choose to write them anyway.

The idea is that when you (the human) see *%, you _know_ it's a named
splat, and when you see *@ you _know_ it's a positional splat.  With
*$, it could be either, depending on what's in the variable.  I agree
that sensible variable names and type annotations should make this a
minor issue, it's just that I'd sometimes like to add an extra sigil
for additional clarity.

And to clarify, as far as I'm aware, nothing I suggested here is new
syntax--just ways of combining existing explicit-deref syntax with (*)
to produce more self-descriptive code.

> >   my $pair = a=>'b';
> >   foo( *%$pair );  # view the pair as a 1-elem hash, and splat that
>
> the % there isn't a hash. perl can see the single pair and deal with
> that. the way to make this more readable is to use better variable
> names. $pair here is fine and *$pair would mean to splat it into the
> named arguments.

My understanding (possibly incorrect) was that the % 'derefs' the pair
into a one-element hash, in much the same way that %$href derefs a
hashref into a hash.  From there, *%$pair is exactly the same as:

  # there's probabaly a shorter way of writing this first line, but
I'm shooting for clarity :)
  my %hash = ($pair.key => $pair.value);
  foo( *%hash );

> >   sub returns_a_hash { ... }
> >   foo( *%{returns_a_hash} );  # call the sub, view the result as a
> > hash, and splat that
>
> isn't that what hash() is for? what is returns_a_hash really returning,
> a list or a hash ref because you can't return a hash (or can you this
> week? :).

My sloppy language again--the idea was that &returns_a_hash produces a
hashref, which is dereffed by %{}, and finally splatted.

> >   sub returns_an_array { ... }
> >   foo( [EMAIL PROTECTED] );  # call the sub, view the result as a
> > hash, and splat that
>
> where is the hash? wouldn't you want %{} in your system? and %{} derefs
> a hash and doesn't convert a list to a hash. that is what hash() does.

Sorry, that was meant to say "view the result as an array".  (I swear
I proofread that post!)

In summary:
1) I believe all my suggestions use currently-existing syntax (modulo
errors on my part)
2) Yes, the extra sigils/derefs are redundant, but sometimes we want
to be explicit
3) I don't particularly object to `foo( *$pair )` also working; this
is meant as an extra option


Stuart

Reply via email to