Dave Storrs wrote:
> On Thu, 17 Aug 2000, Jonathan Scott Duff wrote:
> > 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');
You're breaking the halting rules for figuring out the bounds of
a curried expression.
Your original code should have become:
with %person {
print "Howdy, ", sub { $_[0] }, " ", sub { $_[0] };
}
It gets worse with longer examples because each line is a separate
statement that defines a boundary for the curry.
IMHO, curries have nothing to do with this. All "with" really does is
create a dynamic scope from the contents of the hash and evaluate its
block in that scope.
my %person = { name => 'John Doe', age => 47 };
with %person {
print "$name is $age years old\n";
}
becomes
{
my $env = $CORE::CURRENT_SCOPE;
while (my($k, $v) = each(%person)) {
$env->bind_scalar($k, $v);
}
print "$name is $age years old\n";
}
Where $CORE::CURRENT_SCOPE is some sort of magical variable that provides
access to the symbol table for the current scope. The bind_scalar() method
creates a new value binding. The code block of the "with" is evaluated
with these new bindings in effect.
The simplest way of doing this is to turn off warnings inside the "with"
block and force those variable names into the local scope at compile time.
Then at run-time the bind_scalar() can re-bind the variables to the values
in the hash.
- Ken