in reply to How do you tell if a sub argument is an lvalue?
That is a very interesting question!
I don't know if this is the best answer, but my first thought was to try assigning to the variable and see what happens:
use strict; use warnings; my $a = "123"; foo($a, "456"); sub foo { for (my $i = 0; $i < @_; $i++) { my $type = eval { $_[$i] = "variable" } || "constant"; printf "Arg #%d is $type\n", $i + 1; } }
The output of this is:
Arg #1 is variable Arg #2 is constant
Since the write operation is wrapped in an eval, it returns undef if the assignment failed (constant), otherwise the result of the assignment.
|
|---|