Clear questions and runnable code
get the best and fastest answer
Re^3: Setting end position for the regexp engine using LVALUEby ikegami (Pope)
|on Sep 19, 2013 at 23:31 UTC||Need Help??|
ref returns LVALUE for a reference to a PVLV, a scalar subtype. You can read about it a bit here. It's basically a scalar that's going to have magic associated with it, and it has a few extra fields for storage the magic can use.
Magic, among other things, allows one to attach a getter and fetcher to a variable.
You might think that
gets converted to
by the compiler, but it isn't. substr is truly called as is. This allows the following to work
That means substr must return a magical scalar.
There are more than one type of scalar. Some can hold an integer, some can hold a string, some can hold both. Perl automatically upgrades a scalar when necessary.
Magical scalars require extra fields to store information about what magic is attached to the scalar. The most basic scalar subtype capable of being magical is the PVMG, but there is also PVLV. The PVLV is a PVMG with four extra fields: TYPE, TARGOFF, TARGLEN and TARG.
A instance of substr in an lvalue context returns a PVLV. It uses TARG, TARGOFF and TARGLEN to store the three arguments passed to substr.
When you try to fetch from $$ref, the associated get magic first does something like $$ref = substr($$TARG, $TARGOFF, $TARGLEN);.
After you assign to $$ref, the associated "set" magic effectively does substr($$TARG, $TARGOFF, $TARGLEN, $$ref);.