Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: How to find out if an argument is an LVALUE ?

by ikegami (Patriarch)
on Nov 09, 2023 at 06:46 UTC ( [id://11155492] : note . print w/replies, xml ) Need Help??


in reply to How to find out if an argument is an LVALUE ? (TinyPerl)

An LVALUE is a value that is a literal like a "constant," therefore it cannot be modified.

An lvalue is something that can be used on the "l"eft of an assignment operator. It pretty much means "something modifiable", the opposite of what you said.

ref returns LVALUE for references to PVLV scalars. PVLV scalars are returned by some operations which need to produce a scalar whose modification has side-effects. Those operators are keys, pos, substr and vec. (They only return a PVLV when used in an lvalue context.)

For example,

my $x = "def"; say ref( \substr( $x, 0, 0 ) ); # LVALUE substr( $x, 0, 0 ) = "abc"; say $x; # abcdef
my $x = "def"; my $r = \substr( $x, 0, 0 ); say ref( $r ); # LVALUE $$r = "abc"; say $x; # abcdef

Because we can't just do ref($_[0]) because it's not a reference

True, but contrary to what you said, $REF = \$_[0]; ref( $REF ) does work.

$ perl -Mv5.014 -e' sub f { my $REF = \$_[0]; say ref( $REF ); } f substr( "", 0, 0 ); ' LVALUE

That said, all you need is ref( \$_[0] ).

$ perl -Mv5.014 -e' sub f { say ref( \$_[0] ); } f substr( "", 0, 0 ); ' LVALUE