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 Need Help??

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

Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11155492]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2024-02-24 03:50 GMT
Voting Booth?
My favourite way to spend a leap day ...

Results (22 votes). Check out past polls.