On Wednesday 03 July 2002 12:54 pm, Thom Boyer wrote:
> To get a better feel for the weirdness that happens with pass-by-name,
> consider this example:
>   sub check_it_out {
>     $_[0] = 0;  #step 1
>     $_[1] = 7;  #step 2
>   }
>
>   my @a = (0,1,2,3,4);
>   my $i = 2;
>   check_it_out($i, $a[$i]);
>   print join '', @a;
>
> This prints '01734' under Perl 5, because the arguments ($i and $a[2]) are
> passed by reference.
>
> If the arguments to check_it_out() were passed by name instead, the result
> would be '71234',
> because step 1 would change $i to have the value 0, and *then* $a[$i] would
> be evaluated in
> step 2 (using $i's new value), causing the 7 to be assigned to $a[0] (not
> $a[2]).

Creepy. Here's my creepy thought for the day: is there a possibility for a 
prototype which would implicitly wrap a sub{} around a passed-in argument? 
i.e. lazy evaluation via sub prototype?

sub check_it_out (&$idx is rw, &$val is rw) {
    $idx = 0;
    $val = 7;
}

check_it_out($i, $a[$i]);
# really means:
check_it_out(sub is rw { $i }, sub is rw { $a[$i] });

I would guess parser tricks and tied scalars would allow it somehow, if not 
out of the box.

Still creepy.

Ashley Winters

Reply via email to