> On Thu, Aug 17, 2000 at 10:48:25PM -0500, David L. Nicol wrote:
> > Lets use hats again then.
> >
> > %ws{
> > print ^$height; #prints $ws{height}
> > print $height; # perl5 visibility rules
> > };
>
> But no $ for the keys of %ws.
>
> %ws {
> print ^height; # prints $ws{height}
> print $height; # prints $height
> }
And a keyword (C<with>) please!
> BTW, if we define C<with> to map keys of a hash to named place holders
> in a curried expression, this might be a good thing:
>
> with %person {
> print "Howdy, ", ^firstname, " ", ^lastname;
> }
>
> # becomes
> sub {
> print "Howdy, ", $person{$_[0]}, " ", $person{$_[1]};
> }->('firstname', 'lastname');
>
> # becomes
> print "Howdy, ", $person{'firstname'}, " ", $person{'lastname'};
Aha! How about this...which would give us your desired C<with> functionality
*and* solve a nagging problem with named arguments:
Suppose C<with> were a built-in function with parameter list:
sub with (\%; ^&) {...}
That is, C<with> takes an explicit hash and -- optionally -- a block, sub ref,
or higher order function.
When called with just the hash, it converts the entries of the hash to a
list of named arguments, suitable for passing to a subroutine with named
parameters. So, rather than the tedious:
namedargfunc( name: $args{name}, rank: $args{rank}, snum: $args{snum} );
we can write:
namedargfunc( with %args );
If C<with> is called with *both* a hash and a block/sub ref/h.o.f. as
arguments, it still converts the hash contents to a named list, but then
invokes its second argument on that list of arguments. So:
with %args {
print "Howdy, ", ^rank, " ", ^name;
};
# is really:
(print "Howdy, ", ^rank, " ", ^name)->(with %args);
# is really:
$tmp = sub ($name, $rank) {print "Howdy, ", $rank, " ", $name};
$temp->( name: $args{name}, rank: $args{rank});
which works exactly as you want.
Oh yes, I like that very much.
(It's fine if the rest of you don't: I'll just add it in to RFC 128 :-)
Damian