in reply to Re: Subroutine as an lvalue?
in thread Subroutine as an lvalue?

Unfortunately, the above example doesn't really do what the comments claim. $a and $b are never modified.

Replies are listed 'Best First'.
RE: RE: Re: Subroutine as an lvalue?
by mdillon (Priest) on May 28, 2000 at 00:54 UTC
    strange. the following code, which seems equivalent to me, does work as expected:
    my ($a, $b) = (10, 20); sub mysub : lvalue { $_[0] > 0 ? $a : $b } mysub(2) = 15; mysub(-1) = 9; printf "\$a: %d, \$b: %d, mysub(2): %d, mysub(-1): %d\n", $a, $b, mysub(2), mysub(-1);
    Outputs:
    $a: 15, $b: 9, mysub(2): 15, mysub(-1): 9

    the only difference that i could see mattering is the lack of 'return' statements in my version.

      Aha. That's it. The explicit return seems to make the sub return the value of the rhs, but the variable is never modified. That's what "experimental" means.

      This works as expected:

      #!/usr/bin/perl -wl use strict; { my $count = 0; sub counter : lvalue { ++$count } } print counter() for 1..5; print counter() = -5; print counter() for 1..5;